integration-test-db.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { DataSource, EntityTarget, ObjectLiteral } from 'typeorm';
  2. import { beforeEach, afterEach } from 'vitest';
  3. import { AppDataSource, initializeDataSource } from '@d8d/shared-utils';
  4. /**
  5. * 集成测试数据库工具类 - 使用真实PostgreSQL数据库
  6. * 通用版本,不依赖特定实体
  7. */
  8. export class IntegrationTestDatabase {
  9. /**
  10. * 清理集成测试数据库
  11. */
  12. static async cleanup(): Promise<void> {
  13. if (AppDataSource.isInitialized) {
  14. await AppDataSource.destroy();
  15. }
  16. }
  17. /**
  18. * 获取当前数据源
  19. */
  20. static async getDataSource(): Promise<DataSource> {
  21. if (!AppDataSource.isInitialized) {
  22. await AppDataSource.initialize();
  23. }
  24. return AppDataSource;
  25. }
  26. /**
  27. * 使用特定实体初始化数据源
  28. */
  29. static async initializeWithEntities(entities: EntityTarget<ObjectLiteral>[]): Promise<DataSource> {
  30. initializeDataSource(entities);
  31. return await this.getDataSource();
  32. }
  33. /**
  34. * 清理特定表的数据
  35. */
  36. static async cleanupTable<T extends ObjectLiteral>(
  37. entity: EntityTarget<T>,
  38. condition?: Partial<T>
  39. ): Promise<void> {
  40. const dataSource = await this.getDataSource();
  41. const repository = dataSource.getRepository(entity);
  42. if (condition) {
  43. await repository.delete(condition);
  44. } else {
  45. await repository.clear();
  46. }
  47. }
  48. /**
  49. * 创建测试数据
  50. */
  51. static async createTestData<T extends ObjectLiteral>(
  52. entity: EntityTarget<T>,
  53. data: Partial<T>
  54. ): Promise<T> {
  55. const dataSource = await this.getDataSource();
  56. const repository = dataSource.getRepository(entity);
  57. const entityInstance = repository.create(data);
  58. return await repository.save(entityInstance);
  59. }
  60. /**
  61. * 查找测试数据
  62. */
  63. static async findTestData<T extends ObjectLiteral>(
  64. entity: EntityTarget<T>,
  65. condition: Partial<T>
  66. ): Promise<T | null> {
  67. const dataSource = await this.getDataSource();
  68. const repository = dataSource.getRepository(entity);
  69. return await repository.findOne({ where: condition });
  70. }
  71. /**
  72. * 删除测试数据
  73. */
  74. static async deleteTestData<T extends ObjectLiteral>(
  75. entity: EntityTarget<T>,
  76. condition: Partial<T>
  77. ): Promise<void> {
  78. const dataSource = await this.getDataSource();
  79. const repository = dataSource.getRepository(entity);
  80. await repository.delete(condition);
  81. }
  82. }
  83. /**
  84. * 集成测试数据库生命周期钩子
  85. */
  86. export function setupIntegrationDatabaseHooks() {
  87. beforeEach(async () => {
  88. await IntegrationTestDatabase.getDataSource();
  89. });
  90. afterEach(async () => {
  91. await IntegrationTestDatabase.cleanup();
  92. });
  93. }
  94. /**
  95. * 集成测试数据库生命周期钩子(带实体初始化)
  96. */
  97. export function setupIntegrationDatabaseHooksWithEntities(entities: EntityTarget<ObjectLiteral>[]) {
  98. beforeEach(async () => {
  99. await IntegrationTestDatabase.initializeWithEntities(entities);
  100. });
  101. afterEach(async () => {
  102. await IntegrationTestDatabase.cleanup();
  103. });
  104. }