update quản lí dự án
This commit is contained in:
110
test/asset-project-routes.test.js
Normal file
110
test/asset-project-routes.test.js
Normal file
@@ -0,0 +1,110 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createFixture } = require('./helpers/asset-route-fixture');
|
||||
|
||||
function inventory(overrides = {}) {
|
||||
return { AssetId: 17, AssetCode: 'A', AssetName: 'Drill', Quantity: 10, ImportInPeriod: 2,
|
||||
ExportInPeriod: 2, EndingBalance: 10, NewQuantity: 8, UsedQuantity: 2, Borrower: 'An (2)',
|
||||
ProjectAllocations: JSON.stringify([{ projectName: 'Old', borrowerName: 'An', quantity: 2 }]), ...overrides };
|
||||
}
|
||||
|
||||
function exportFixture(asset = inventory(), failHistory = false) {
|
||||
let historyId = 0;
|
||||
return createFixture(({ query, inputs }) => {
|
||||
if (query.includes('SELECT TOP 1') && query.includes('FROM AssetInventory')) return { recordset: [asset] };
|
||||
if (query.includes('INSERT INTO AssetExportHistory')) {
|
||||
if (failHistory) throw Error('test history failure');
|
||||
return { recordset: [{ ExportHistoryId: ++historyId, ProjectName: inputs.projectName, ExportQuantity: inputs.exportQuantity }] };
|
||||
}
|
||||
return { recordset: [] };
|
||||
});
|
||||
}
|
||||
|
||||
const exportRequest = (allocations, extra = {}) => ({ params: { id: '17' }, body: { borrowerName: 'Binh', allocations, ...extra } });
|
||||
|
||||
test('one atomic export distributes to multiple projects and preserves prior allocations', async () => {
|
||||
const fixture = exportFixture();
|
||||
const response = await fixture.invoke('post', '/api/assets/:id/export', exportRequest([
|
||||
{ projectName: 'A', quantity: 3 }, { projectName: 'B', quantity: 7 }
|
||||
]));
|
||||
assert.equal(response.statusCode, 200);
|
||||
const write = fixture.queries.find(call => call.query.includes('UPDATE AssetInventory'));
|
||||
assert.equal(write.inputs.exportInPeriod, 12);
|
||||
assert.equal(write.inputs.endingBalance, 0);
|
||||
assert.equal(write.inputs.newQuantity + write.inputs.usedQuantity, 0);
|
||||
assert.equal(JSON.parse(write.inputs.projectAllocations).length, 3);
|
||||
assert.doesNotMatch(write.query, /SET Project =/);
|
||||
const histories = fixture.queries.filter(call => call.query.includes('INSERT INTO AssetExportHistory'));
|
||||
assert.deepEqual(histories.map(call => [call.inputs.projectName, call.inputs.exportQuantity]), [['A', 3], ['B', 7]]);
|
||||
assert.ok(fixture.queries.every(call => call.transaction === fixture.transactions[0]));
|
||||
assert.match(fixture.queries[0].query, /UPDLOCK/);
|
||||
assert.equal(fixture.transactions[0].committed, true);
|
||||
});
|
||||
|
||||
test('combined project exports cannot exceed available stock or the inventory total', async () => {
|
||||
for (const asset of [inventory(), inventory({ EndingBalance: 100 })]) {
|
||||
const fixture = exportFixture(asset);
|
||||
const response = await fixture.invoke('post', '/api/assets/:id/export', exportRequest([
|
||||
{ projectName: 'A', quantity: 6 }, { projectName: 'B', quantity: 5 }
|
||||
]));
|
||||
assert.equal(response.statusCode, 400);
|
||||
assert.equal(fixture.queries.length, 1);
|
||||
assert.equal(fixture.transactions[0].rolledBack, true);
|
||||
}
|
||||
});
|
||||
|
||||
test('invalid export rows fail before opening a transaction', async () => {
|
||||
for (const allocations of [[], [{ projectName: '', quantity: 1 }], [{ projectName: 'A', quantity: 1.5 }]]) {
|
||||
const fixture = exportFixture();
|
||||
assert.equal((await fixture.invoke('post', '/api/assets/:id/export', exportRequest(allocations))).statusCode, 400);
|
||||
assert.equal(fixture.transactions.length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test('history insertion failure rolls back the entire multi-project export', async () => {
|
||||
const fixture = exportFixture(inventory(), true);
|
||||
const response = await fixture.invoke('post', '/api/assets/:id/export', exportRequest([{ projectName: 'A', quantity: 1 }]));
|
||||
assert.equal(response.statusCode, 500);
|
||||
assert.equal(fixture.transactions[0].rolledBack, true);
|
||||
assert.equal(fixture.transactions[0].committed, undefined);
|
||||
});
|
||||
|
||||
test('editing uses the locked export ledger and rejects total below already-issued quantity', async () => {
|
||||
for (const quantity of [1, 15]) {
|
||||
const fixture = createFixture(({ query }) => ({ recordset: query.includes('SELECT *') ? [inventory()] : [{ AssetId: 17 }] }));
|
||||
const response = await fixture.invoke('put', '/api/assets/:id', {
|
||||
params: { id: '17' }, body: { assetCode: 'A', model: 'Drill', quantity, exportInPeriod: 0, borrower: '' }
|
||||
});
|
||||
assert.equal(response.statusCode, quantity === 1 ? 400 : 200);
|
||||
const write = fixture.queries.find(call => call.query.includes('UPDATE AssetInventory'));
|
||||
if (quantity === 1) assert.equal(write, undefined);
|
||||
else {
|
||||
assert.equal(write.inputs.exportInPeriod, 2);
|
||||
assert.equal(write.inputs.endingBalance, 13);
|
||||
assert.equal(write.inputs.borrower, 'An (2)');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('spreadsheet import cannot overwrite an active project allocation', async () => {
|
||||
const fixture = createFixture(({ query }) => ({ recordset: query.includes('ProjectAllocations IS NOT NULL') ? [{ AssetId: 17 }] : [] }));
|
||||
const response = await fixture.invoke('post', '/api/assets/import', {
|
||||
body: { rows: [{ assetCode: 'A', assetName: 'Drill', model: 'D', quantity: 1 }] }
|
||||
});
|
||||
assert.equal(response.statusCode, 409);
|
||||
assert.equal(fixture.transactions[0].rolledBack, true);
|
||||
assert.ok(!fixture.queries.some(call => call.query.includes('MERGE AssetInventory')));
|
||||
});
|
||||
|
||||
test('approved partial returns reduce only the borrower’s current project allocations', async () => {
|
||||
const asset = inventory({ BorrowId: 41, RequestType: 'return', RequestStatus: 'pending',
|
||||
BorrowerName: 'An', BorrowQuantity: 1, CreatedBy: 1, IsArchived: false });
|
||||
const fixture = createFixture(({ query }) => ({ recordset: query.includes('WHERE br.BorrowId = @borrowId') ? [asset] : [] }));
|
||||
const response = await fixture.invoke('post', '/api/asset-borrows/:id/process', { params: { id: '41' }, body: { action: 'approved' } });
|
||||
assert.equal(response.statusCode, 200);
|
||||
const write = fixture.queries.find(call => call.query.includes('UPDATE AssetInventory'));
|
||||
assert.equal(JSON.parse(write.inputs.projectAllocations)[0].quantity, 1);
|
||||
assert.equal(write.inputs.exportInPeriod, 1);
|
||||
assert.equal(write.inputs.endingBalance, 11);
|
||||
assert.equal(fixture.transactions[0].committed, true);
|
||||
});
|
||||
Reference in New Issue
Block a user