数据库
This commit is contained in:
+22
-3
@@ -1,17 +1,20 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { rm } from 'node:fs/promises';
|
||||
import { readFile, rm } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
import assert from 'node:assert/strict';
|
||||
import { relationalTables } from '../database.mjs';
|
||||
|
||||
const root = resolve(process.cwd());
|
||||
const port = 4182;
|
||||
const baseUrl = `http://127.0.0.1:${port}`;
|
||||
const testDb = resolve(root, 'data', 'test-db.json');
|
||||
const testDb = resolve(root, 'data', 'test-db.sqlite');
|
||||
await rm(testDb, { force: true });
|
||||
await rm(`${testDb}-shm`, { force: true });
|
||||
await rm(`${testDb}-wal`, { force: true });
|
||||
|
||||
const server = spawn(process.execPath, ['server.mjs'], {
|
||||
cwd: root,
|
||||
env: { ...process.env, PORT: String(port), EXAM_DB_PATH: testDb },
|
||||
env: { ...process.env, NODE_ENV: 'test', DATABASE_CLIENT: 'sqlite', PORT: String(port), SQLITE_PATH: testDb },
|
||||
stdio: ['ignore', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
@@ -54,6 +57,19 @@ const anonymous = createClient();
|
||||
try {
|
||||
await waitForServer();
|
||||
|
||||
const sqliteFile = await readFile(testDb);
|
||||
assert.equal(sqliteFile.subarray(0, 16).toString(), 'SQLite format 3\0', '测试持久化文件必须是真实 SQLite 数据库');
|
||||
const { DatabaseSync } = await import('node:sqlite');
|
||||
const inspector = new DatabaseSync(testDb, { readOnly: true });
|
||||
const tableNames = inspector.prepare(`
|
||||
SELECT name FROM sqlite_master
|
||||
WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
|
||||
ORDER BY name
|
||||
`).all().map(row => row.name);
|
||||
inspector.close();
|
||||
assert.deepEqual(tableNames, [...relationalTables].sort(), '业务数据必须按关系模型分表存储');
|
||||
assert.ok(!tableNames.includes('app_state'), '不得使用单表 JSON 状态存储');
|
||||
|
||||
const publicHome = await anonymous.request('/api/public/home');
|
||||
assert.equal(publicHome.response.status, 200);
|
||||
assert.ok(publicHome.data.notices.length >= 3, '公开首页应返回通知');
|
||||
@@ -164,6 +180,7 @@ try {
|
||||
assert.ok(results.data.results.some(item => item.score === 126 && item.subjectName === '语文'), '已发布成绩应在考生端可查询');
|
||||
|
||||
console.log('✓ 公开首页与通知读取');
|
||||
console.log(`✓ SQLite 关系型数据库初始化(${relationalTables.length} 张分表)`);
|
||||
console.log('✓ 考生自主注册、完整资料维护与管理员审核');
|
||||
console.log('✓ 多科目考试创建与考生自主选科报名');
|
||||
console.log('✓ 报名审核、准考证生成、开放期下载与窗口限制');
|
||||
@@ -173,4 +190,6 @@ try {
|
||||
server.kill('SIGTERM');
|
||||
await new Promise(resolveWait => server.once('exit', resolveWait));
|
||||
await rm(testDb, { force: true });
|
||||
await rm(`${testDb}-shm`, { force: true });
|
||||
await rm(`${testDb}-wal`, { force: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user