|
@@ -2,46 +2,28 @@ import { DataSource } from 'typeorm';
|
|
|
import { beforeEach, afterEach, afterAll } from 'vitest';
|
|
import { beforeEach, afterEach, afterAll } from 'vitest';
|
|
|
import { UserEntity } from '../modules/users/user.entity';
|
|
import { UserEntity } from '../modules/users/user.entity';
|
|
|
import { Role } from '../modules/users/role.entity';
|
|
import { Role } from '../modules/users/role.entity';
|
|
|
|
|
+import { AppDataSource } from '../data-source';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 集成测试数据库工具类 - 使用真实PostgreSQL数据库
|
|
* 集成测试数据库工具类 - 使用真实PostgreSQL数据库
|
|
|
*/
|
|
*/
|
|
|
export class IntegrationTestDatabase {
|
|
export class IntegrationTestDatabase {
|
|
|
- private static dataSource: DataSource | null = null;
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 初始化集成测试数据库
|
|
* 初始化集成测试数据库
|
|
|
*/
|
|
*/
|
|
|
static async initialize(): Promise<DataSource> {
|
|
static async initialize(): Promise<DataSource> {
|
|
|
- if (this.dataSource?.isInitialized) {
|
|
|
|
|
- return this.dataSource;
|
|
|
|
|
|
|
+ if (!AppDataSource.isInitialized) {
|
|
|
|
|
+ await AppDataSource.initialize();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 使用环境变量中的测试数据库配置
|
|
|
|
|
- const databaseUrl = process.env.TEST_DATABASE_URL ||
|
|
|
|
|
- process.env.DATABASE_URL?.replace('d8dai', 'test_d8dai') ||
|
|
|
|
|
- 'postgresql://postgres:test_password@localhost:5432/test_d8dai';
|
|
|
|
|
-
|
|
|
|
|
- this.dataSource = new DataSource({
|
|
|
|
|
- type: 'postgres',
|
|
|
|
|
- url: databaseUrl,
|
|
|
|
|
- synchronize: true,
|
|
|
|
|
- dropSchema: true, // 每次测试都重新创建schema
|
|
|
|
|
- logging: false,
|
|
|
|
|
- entities: [UserEntity, Role]
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- await this.dataSource.initialize();
|
|
|
|
|
- return this.dataSource;
|
|
|
|
|
|
|
+ return AppDataSource;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 清理集成测试数据库
|
|
* 清理集成测试数据库
|
|
|
*/
|
|
*/
|
|
|
static async cleanup(): Promise<void> {
|
|
static async cleanup(): Promise<void> {
|
|
|
- if (this.dataSource?.isInitialized) {
|
|
|
|
|
- await this.dataSource.destroy();
|
|
|
|
|
- this.dataSource = null;
|
|
|
|
|
|
|
+ if (AppDataSource.isInitialized) {
|
|
|
|
|
+ await AppDataSource.destroy();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -49,23 +31,23 @@ export class IntegrationTestDatabase {
|
|
|
* 获取当前数据源
|
|
* 获取当前数据源
|
|
|
*/
|
|
*/
|
|
|
static getDataSource(): DataSource | null {
|
|
static getDataSource(): DataSource | null {
|
|
|
- return this.dataSource;
|
|
|
|
|
|
|
+ return AppDataSource.isInitialized ? AppDataSource : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 清空所有表数据
|
|
* 清空所有表数据
|
|
|
*/
|
|
*/
|
|
|
static async clearAllData(): Promise<void> {
|
|
static async clearAllData(): Promise<void> {
|
|
|
- if (!this.dataSource?.isInitialized) {
|
|
|
|
|
|
|
+ if (!AppDataSource.isInitialized) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const queryRunner = this.dataSource.createQueryRunner();
|
|
|
|
|
|
|
+ const queryRunner = AppDataSource.createQueryRunner();
|
|
|
await queryRunner.connect();
|
|
await queryRunner.connect();
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
// 获取所有实体
|
|
// 获取所有实体
|
|
|
- const entities = this.dataSource.entityMetadatas;
|
|
|
|
|
|
|
+ const entities = AppDataSource.entityMetadatas;
|
|
|
|
|
|
|
|
// 按依赖关系排序(先删除子表,再删除父表)
|
|
// 按依赖关系排序(先删除子表,再删除父表)
|
|
|
const sortedEntities = entities.sort((a, b) => {
|
|
const sortedEntities = entities.sort((a, b) => {
|
|
@@ -78,10 +60,9 @@ export class IntegrationTestDatabase {
|
|
|
return 0;
|
|
return 0;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // 清空所有表数据
|
|
|
|
|
|
|
+ // 使用TRUNCATE CASCADE来清空所有表数据(包括有外键约束的表)
|
|
|
for (const entity of sortedEntities) {
|
|
for (const entity of sortedEntities) {
|
|
|
- const repository = this.dataSource.getRepository(entity.name);
|
|
|
|
|
- await repository.clear();
|
|
|
|
|
|
|
+ await queryRunner.query(`TRUNCATE TABLE "${entity.tableName}" CASCADE`);
|
|
|
}
|
|
}
|
|
|
} finally {
|
|
} finally {
|
|
|
await queryRunner.release();
|
|
await queryRunner.release();
|