|
|
@@ -1,5 +1,5 @@
|
|
|
import { DataSource } from 'typeorm';
|
|
|
-import { beforeEach, afterEach, afterAll } from 'vitest';
|
|
|
+import { beforeEach, afterEach } from 'vitest';
|
|
|
import { UserEntity } from '../modules/users/user.entity';
|
|
|
import { Role } from '../modules/users/role.entity';
|
|
|
import { AppDataSource } from '../data-source';
|
|
|
@@ -8,37 +8,11 @@ import { AppDataSource } from '../data-source';
|
|
|
* 集成测试数据库工具类 - 使用真实PostgreSQL数据库
|
|
|
*/
|
|
|
export class IntegrationTestDatabase {
|
|
|
- // private static initializationPromise: Promise<DataSource> | null = null;
|
|
|
- // /**
|
|
|
- // * 初始化集成测试数据库
|
|
|
- // */
|
|
|
- // static async initialize(): Promise<DataSource> {
|
|
|
- // // if (!AppDataSource.isInitialized) {
|
|
|
- // // if (!this.initializationPromise) {
|
|
|
- // // this.initializationPromise = (async () => {
|
|
|
- // // try {
|
|
|
- // // await AppDataSource.initialize();
|
|
|
- // // return AppDataSource;
|
|
|
- // // } catch (error) {
|
|
|
- // // this.initializationPromise = null;
|
|
|
- // // throw error;
|
|
|
- // // }
|
|
|
- // // })();
|
|
|
- // // }
|
|
|
- // // return await this.initializationPromise;
|
|
|
- // // }
|
|
|
- // // if(!AppDataSource.isInitialized) {
|
|
|
- // // await AppDataSource.initialize();
|
|
|
- // // }
|
|
|
- // return AppDataSource;
|
|
|
- // }
|
|
|
-
|
|
|
/**
|
|
|
* 清理集成测试数据库
|
|
|
*/
|
|
|
static async cleanup(): Promise<void> {
|
|
|
if (AppDataSource.isInitialized) {
|
|
|
- console.debug('测试数据库断开')
|
|
|
await AppDataSource.destroy();
|
|
|
}
|
|
|
}
|
|
|
@@ -48,46 +22,10 @@ export class IntegrationTestDatabase {
|
|
|
*/
|
|
|
static async getDataSource(): Promise<DataSource> {
|
|
|
if(!AppDataSource.isInitialized) {
|
|
|
- console.debug('测试数据库初始化')
|
|
|
await AppDataSource.initialize();
|
|
|
}
|
|
|
return AppDataSource
|
|
|
}
|
|
|
-
|
|
|
- // /**
|
|
|
- // * 清空所有表数据
|
|
|
- // */
|
|
|
- // static async clearAllData(): Promise<void> {
|
|
|
- // if (!AppDataSource.isInitialized) {
|
|
|
- // return;
|
|
|
- // }
|
|
|
-
|
|
|
- // const queryRunner = AppDataSource.createQueryRunner();
|
|
|
- // await queryRunner.connect();
|
|
|
-
|
|
|
- // try {
|
|
|
- // // 获取所有实体
|
|
|
- // const entities = AppDataSource.entityMetadatas;
|
|
|
-
|
|
|
- // // 按依赖关系排序(先删除子表,再删除父表)
|
|
|
- // const sortedEntities = entities.sort((a, b) => {
|
|
|
- // if (a.foreignKeys.some(fk => fk.referencedEntityMetadata.name === b.name)) {
|
|
|
- // return 1; // a 依赖于 b,a 应该排在后面
|
|
|
- // }
|
|
|
- // if (b.foreignKeys.some(fk => fk.referencedEntityMetadata.name === a.name)) {
|
|
|
- // return -1; // b 依赖于 a,a 应该排在前面
|
|
|
- // }
|
|
|
- // return 0;
|
|
|
- // });
|
|
|
-
|
|
|
- // // 使用TRUNCATE CASCADE来清空所有表数据(包括有外键约束的表)
|
|
|
- // for (const entity of sortedEntities) {
|
|
|
- // await queryRunner.query(`TRUNCATE TABLE "${entity.tableName}" CASCADE`);
|
|
|
- // }
|
|
|
- // } finally {
|
|
|
- // await queryRunner.release();
|
|
|
- // }
|
|
|
- // }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -155,12 +93,7 @@ export function setupIntegrationDatabaseHooks() {
|
|
|
await IntegrationTestDatabase.getDataSource();
|
|
|
});
|
|
|
|
|
|
- // afterEach(async () => {
|
|
|
- // await IntegrationTestDatabase.clearAllData();
|
|
|
- // });
|
|
|
-
|
|
|
afterEach(async () => {
|
|
|
- console.debug('测试数据库断开')
|
|
|
await IntegrationTestDatabase.cleanup();
|
|
|
});
|
|
|
}
|