integration-test-db.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { DataSource } from 'typeorm';
  2. import { beforeEach, afterEach } from 'vitest';
  3. import { UserEntityMt, RoleMt } from '@d8d/user-module-mt';
  4. import { FileMt } from '@d8d/file-module-mt';
  5. import { AppDataSource } from '@d8d/shared-utils';
  6. /**
  7. * 集成测试数据库工具类 - 使用真实PostgreSQL数据库
  8. */
  9. export class IntegrationTestDatabase {
  10. /**
  11. * 清理集成测试数据库
  12. */
  13. static async cleanup(): Promise<void> {
  14. if (AppDataSource.isInitialized) {
  15. await AppDataSource.destroy();
  16. }
  17. }
  18. /**
  19. * 获取当前数据源
  20. */
  21. static async getDataSource(): Promise<DataSource> {
  22. if(!AppDataSource.isInitialized) {
  23. await AppDataSource.initialize();
  24. }
  25. return AppDataSource
  26. }
  27. }
  28. /**
  29. * 测试数据工厂类
  30. */
  31. export class TestDataFactory {
  32. /**
  33. * 创建测试用户数据
  34. */
  35. static createUserData(overrides: Partial<UserEntityMt> = {}): Partial<UserEntityMt> {
  36. const timestamp = Date.now();
  37. return {
  38. username: `testuser_${timestamp}`,
  39. password: 'TestPassword123!',
  40. email: `test_${timestamp}@example.com`,
  41. phone: `138${timestamp.toString().slice(-8)}`,
  42. nickname: `Test User ${timestamp}`,
  43. name: `Test Name ${timestamp}`,
  44. tenantId: 1, // 多租户需要tenant_id
  45. isDisabled: 0,
  46. isDeleted: 0,
  47. ...overrides
  48. };
  49. }
  50. /**
  51. * 创建测试角色数据
  52. */
  53. static createRoleData(overrides: Partial<RoleMt> = {}): Partial<RoleMt> {
  54. const timestamp = Date.now();
  55. return {
  56. name: `test_role_${timestamp}`,
  57. description: `Test role description ${timestamp}`,
  58. tenantId: 1, // 多租户需要tenant_id
  59. ...overrides
  60. };
  61. }
  62. /**
  63. * 在数据库中创建测试用户
  64. */
  65. static async createTestUser(dataSource: DataSource, overrides: Partial<UserEntityMt> = {}): Promise<UserEntityMt> {
  66. const userData = this.createUserData(overrides);
  67. const userRepository = dataSource.getRepository(UserEntityMt);
  68. const user = userRepository.create(userData);
  69. return await userRepository.save(user);
  70. }
  71. /**
  72. * 在数据库中创建测试角色
  73. */
  74. static async createTestRole(dataSource: DataSource, overrides: Partial<RoleMt> = {}): Promise<RoleMt> {
  75. const roleData = this.createRoleData(overrides);
  76. const roleRepository = dataSource.getRepository(RoleMt);
  77. const role = roleRepository.create(roleData);
  78. return await roleRepository.save(role);
  79. }
  80. /**
  81. * 创建测试文件数据
  82. */
  83. static createFileData(overrides: Partial<FileMt> = {}): Partial<FileMt> {
  84. const timestamp = Date.now();
  85. return {
  86. name: `testfile_${timestamp}.txt`,
  87. type: 'text/plain',
  88. size: 1024,
  89. path: `/uploads/testfile_${timestamp}.txt`,
  90. description: `Test file ${timestamp}`,
  91. tenantId: 1, // 多租户需要tenant_id
  92. uploadUserId: 1,
  93. uploadTime: new Date(),
  94. ...overrides
  95. };
  96. }
  97. /**
  98. * 在数据库中创建测试文件
  99. */
  100. static async createTestFile(dataSource: DataSource, overrides: Partial<FileMt> = {}): Promise<FileMt> {
  101. const fileData = this.createFileData(overrides);
  102. const fileRepository = dataSource.getRepository(FileMt);
  103. const file = fileRepository.create(fileData);
  104. return await fileRepository.save(file);
  105. }
  106. }
  107. /**
  108. * 集成测试数据库生命周期钩子
  109. */
  110. export function setupIntegrationDatabaseHooks() {
  111. beforeEach(async () => {
  112. await IntegrationTestDatabase.getDataSource();
  113. });
  114. afterEach(async () => {
  115. await IntegrationTestDatabase.cleanup();
  116. });
  117. }