98 lines
4.2 KiB
JavaScript
98 lines
4.2 KiB
JavaScript
const request = require('supertest');
|
|||
|
|
const app = require('../final-backend');
|
||
|
|
|
||
|
|
describe('质保金数据正确性测试 - TDD Red/Green', () => {
|
||
|
|
describe('项目详情API - 质保金数据', () => {
|
||
|
|
test('GET /api/projects/:id - 应该返回正确的质保金比例(从合同表读取)', async () => {
|
||
|
|
const response = await request(app).get('/api/projects/1');
|
||
|
|
|
||
|
|
expect(response.status).toBe(200);
|
||
|
|
expect(response.body.success).toBe(true);
|
||
|
|
expect(response.body.data).toHaveProperty('warranty_percent');
|
||
|
|
|
||
|
|
// 质保比例应该从 project_contracts 表的 warranty_deposit_percentage 字段读取
|
||
|
|
// 而不是硬编码为 5
|
||
|
|
const warrantyPercent = parseFloat(response.body.data.warranty_percent);
|
||
|
|
expect(warrantyPercent).toBeGreaterThan(0);
|
||
|
|
expect(warrantyPercent).toBeLessThanOrEqual(100);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('GET /api/projects/:id - 质保金金额应该根据合同金额和比例正确计算', async () => {
|
||
|
|
const response = await request(app).get('/api/projects/1');
|
||
|
|
|
||
|
|
expect(response.status).toBe(200);
|
||
|
|
expect(response.body.success).toBe(true);
|
||
|
|
|
||
|
|
const contractAmount = parseFloat(response.body.data.contract_amount || '0');
|
||
|
|
const warrantyPercent = parseFloat(response.body.data.warranty_percent || '0');
|
||
|
|
const warrantyAmount = parseFloat(response.body.data.warranty_amount || '0');
|
||
|
|
|
||
|
|
// 质保金金额 = 合同金额 * 质保比例 / 100
|
||
|
|
const expectedWarrantyAmount = Math.round(contractAmount * warrantyPercent / 100);
|
||
|
|
|
||
|
|
// 允许1元的四舍五入误差
|
||
|
|
expect(Math.abs(warrantyAmount - expectedWarrantyAmount)).toBeLessThanOrEqual(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('GET /api/projects/:id - 质保期限应该从合同表读取', async () => {
|
||
|
|
const response = await request(app).get('/api/projects/1');
|
||
|
|
|
||
|
|
expect(response.status).toBe(200);
|
||
|
|
expect(response.body.success).toBe(true);
|
||
|
|
expect(response.body.data).toHaveProperty('warranty_months');
|
||
|
|
|
||
|
|
// 质保期限应该是数字且大于0
|
||
|
|
const warrantyMonths = parseInt(response.body.data.warranty_months);
|
||
|
|
expect(warrantyMonths).toBeGreaterThan(0);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('项目质保金列表API', () => {
|
||
|
|
test('GET /api/projects/:id/warranty-deposits - 应该返回质保金记录', async () => {
|
||
|
|
const response = await request(app).get('/api/projects/1/warranty-deposits');
|
||
|
|
|
||
|
|
expect(response.status).toBe(200);
|
||
|
|
expect(response.body.success).toBe(true);
|
||
|
|
expect(Array.isArray(response.body.data)).toBe(true);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('质保金数据字段正确性测试', () => {
|
||
|
|
test('质保金数据应该包含所有必要字段', async () => {
|
||
|
|
const response = await request(app).get('/api/projects/1');
|
||
|
|
|
||
|
|
expect(response.status).toBe(200);
|
||
|
|
expect(response.body.success).toBe(true);
|
||
|
|
|
||
|
|
const data = response.body.data;
|
||
|
|
|
||
|
|
// 检查所有质保金相关字段
|
||
|
|
expect(data).toHaveProperty('has_warranty');
|
||
|
|
expect(data).toHaveProperty('warranty_amount');
|
||
|
|
expect(data).toHaveProperty('warranty_percent');
|
||
|
|
expect(data).toHaveProperty('warranty_months');
|
||
|
|
expect(data).toHaveProperty('warranty_start_date');
|
||
|
|
expect(data).toHaveProperty('warranty_end_date');
|
||
|
|
expect(data).toHaveProperty('warranty_status');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('质保比例和质保期限应该是不同的值', async () => {
|
||
|
|
const response = await request(app).get('/api/projects/1');
|
||
|
|
|
||
|
|
expect(response.status).toBe(200);
|
||
|
|
expect(response.body.success).toBe(true);
|
||
|
|
|
||
|
|
const warrantyPercent = parseFloat(response.body.data.warranty_percent);
|
||
|
|
const warrantyMonths = parseInt(response.body.data.warranty_months);
|
||
|
|
|
||
|
|
// 质保比例(百分比)和质保期限(月数)应该是不同的概念
|
||
|
|
// 比例通常是 0-100 之间的数,期限通常是 12、24 等月数
|
||
|
|
// 它们不应该相等(除非是极端情况,但测试中应该区分)
|
||
|
|
expect(typeof warrantyPercent).toBe('number');
|
||
|
|
expect(typeof warrantyMonths).toBe('number');
|
||
|
|
expect(warrantyPercent).not.toBeNaN();
|
||
|
|
expect(warrantyMonths).not.toBeNaN();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|