feat: 添加测试用例

This commit is contained in:
Pan Qiancheng 2026-01-01 11:23:16 +08:00
parent e0a447adc0
commit aa49a12cb0
8 changed files with 5275 additions and 4306 deletions

View File

@ -12,7 +12,10 @@
"scripts": {
"test": "mocha",
"make:test:domain": "ts-node script/makeTestDomain.ts",
"build": "tsc"
"build": "tsc",
"build:test": "tsc -p tsconfig.test.json",
"test:mysql": "node ./test-dist/testMySQLStore.js",
"test:postgres": "node ./test-dist/testPostgresStore.js"
},
"dependencies": {
"lodash": "^4.17.21",

View File

@ -10,7 +10,7 @@ export interface Schema extends EntityShape {
area: Area;
owner: User;
dd: Array<ExtraFile>;
size: Float<4, 2>;
size: Float<8, 4>;
};
const locale: LocaleDef<Schema, '', '', {}> = {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
import { describe, it } from 'mocha';
import { MySqlTranslator } from '../src/MySQL/translator';
import { MySqlTranslator } from '../lib/MySQL/translator';
import { EntityDict, storageSchema } from './test-app-domain';
describe('test MysqlTranslator', function () {

5117
test/testcase/index.ts Normal file

File diff suppressed because it is too large Load Diff

108
test/utils/test.ts Normal file
View File

@ -0,0 +1,108 @@
type AsyncFunc = () => Promise<void>;
type SyncFunc = () => void;
const beforeFuncs: AsyncFunc[] = [];
const itFuncs: { desc: string; fn: AsyncFunc }[] = [];
const afterFuncs: SyncFunc[] = [];
const stats = {
total: 0,
passed: 0,
failed: 0,
errors: [] as {
type: 'before' | 'it' | 'after';
desc?: string;
error: unknown;
}[],
};
export const before = (func: AsyncFunc) => {
beforeFuncs.push(func);
return func;
};
export const it = (desc: string, func: AsyncFunc) => {
itFuncs.push({ desc, fn: func });
};
export const after = (func: SyncFunc) => {
afterFuncs.push(func);
return func;
};
export const describe = async (desc: string, func: () => Promise<void> | void) => {
console.log(desc);
// 注册阶段
await func();
// before
for (const f of beforeFuncs) {
try {
await f();
} catch (err) {
stats.failed++;
stats.errors.push({
type: 'before',
error: err,
});
console.error(' ✗ before failed:', err);
}
}
// it
for (const { desc, fn } of itFuncs) {
stats.total++;
try {
console.log(' ' + desc);
await fn();
stats.passed++;
console.log(' ✓ passed');
} catch (err) {
stats.failed++;
stats.errors.push({
type: 'it',
desc,
error: err,
});
console.error(' ✗ failed:', err);
}
}
// after无论如何都执行
for (const f of afterFuncs) {
try {
f();
} catch (err) {
stats.failed++;
stats.errors.push({
type: 'after',
error: err,
});
console.error(' ✗ after failed:', err);
}
}
// 统计输出
printSummary();
};
const printSummary = () => {
console.log('\n====== Test Summary ======');
console.log(`Total: ${stats.total}`);
console.log(`Passed: ${stats.passed}`);
console.log(`Failed: ${stats.failed}`);
if (stats.errors.length) {
console.log('\nErrors:');
for (const e of stats.errors) {
console.log(
`- [${e.type}]${e.desc ? ' ' + e.desc : ''}`,
'\n ',
e.error
);
}
}
console.log('==========================');
};

32
tsconfig.test.json Normal file
View File

@ -0,0 +1,32 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"declaration": true,
"allowJs": false,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"strict": true,
"skipLibCheck": true,
"importHelpers": true,
"lib": [
"ESNext",
"DOM"
],
"outDir": "test-dist", /* Redirect output structure to the directory. */
"rootDir": "test", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"types": [
"node",
"mocha"
],
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"resolveJsonModule": true
},
"include": [
"test/**/*.ts"
],
"exclude": [
"node_modules"
]
}