xuất excel

This commit is contained in:
2026-10-14 09:10:37 +07:00
parent dff0d32912
commit 533923076c
19 changed files with 1655 additions and 236 deletions

View File

@@ -3,6 +3,7 @@ const assert = require('node:assert/strict');
const { randomBytes } = require('node:crypto');
const sql = require('mssql');
const { ensureAssetArchiveSchema } = require('../backend/asset-archive-schema');
const { ensureAssetExportDocumentSchema } = require('../backend/asset-export-documents');
// Explicit opt-in to a disposable, local SQL Server. Never read .env or reuse the
// application's database configuration. The runner creates its own database.
@@ -99,6 +100,37 @@ async function historySnapshot(pool) {
`)).recordsets;
}
test('SQL Server document migration preserves legacy rows and saved files across repeated startup and archiving', integrationOptions, async () => {
await withTestDatabase(async pool => {
await pool.request().batch(legacySchema);
await pool.request().query(`
INSERT dbo.AssetInventory (AssetCode, AssetName) VALUES ('TEST', N'Test asset');
INSERT dbo.AssetExportHistory (AssetId, ExportQuantity) VALUES (1, 1);
`);
await ensureAssetArchiveSchema(pool);
await ensureAssetExportDocumentSchema(pool);
const oldRow = (await pool.request().query('SELECT * FROM dbo.AssetExportHistory')).recordset[0];
assert.equal(oldRow.ExportDocumentId, null);
const bytes = Buffer.from('original TEST workbook bytes');
const documentId = 'f13b142b-913b-44b8-b15c-d719bd45d197';
await pool.request().input('id', sql.UniqueIdentifier, documentId).input('bytes', sql.VarBinary(sql.MAX), bytes).query(`
INSERT dbo.AssetExportDocuments (ExportDocumentId, FileName, MimeType, FileContent, SnapshotJson, ExportedDate)
VALUES (@id, N'TEST.xlsx', N'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
@bytes, N'{"items":[]}', DATEADD(HOUR, 7, SYSUTCDATETIME()));
INSERT dbo.AssetExportHistory (AssetId, ExportQuantity, ExportDocumentId) VALUES (1, 2, @id);
UPDATE dbo.AssetInventory SET IsArchived = 1, AssetName = N'Changed asset' WHERE AssetId = 1;
`);
await ensureAssetArchiveSchema(pool);
await ensureAssetExportDocumentSchema(pool);
const saved = (await pool.request().query('SELECT * FROM dbo.AssetExportDocuments')).recordset;
assert.equal(saved.length, 1);
assert.deepEqual(saved[0].FileContent, bytes);
assert.equal(saved[0].SnapshotJson, '{"items":[]}');
assert.equal((await pool.request().query('SELECT COUNT(*) AS total FROM dbo.AssetExportHistory')).recordset[0].total, 2);
await assert.rejects(pool.request().query('DELETE FROM dbo.AssetExportDocuments'), error => error.number === 547);
});
});
test('SQL Server migration preserves asset history across inventory replacement', integrationOptions, async t => {
await withTestDatabase(async pool => {
await pool.request().batch(legacySchema);