integration-test-db.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { DataSource } from 'typeorm';
  2. import { beforeEach, afterEach } from 'vitest';
  3. import { UserEntity } from '../../src/entities/user.entity';
  4. import { Role } from '../../src/entities/role.entity';
  5. import { AppDataSource, initializeDataSource } 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) {
  23. initializeDataSource([UserEntity, Role]);
  24. }
  25. if (!AppDataSource.isInitialized) {
  26. await AppDataSource.initialize();
  27. }
  28. return AppDataSource;
  29. }
  30. }
  31. /**
  32. * 测试数据工厂类
  33. */
  34. export class TestDataFactory {
  35. /**
  36. * 创建测试用户数据
  37. */
  38. static createUserData(overrides: Partial<UserEntity> = {}): Partial<UserEntity> {
  39. const timestamp = Date.now();
  40. return {
  41. username: `testuser_${timestamp}`,
  42. password: 'TestPassword123!',
  43. email: `test_${timestamp}@example.com`,
  44. phone: `138${timestamp.toString().slice(-8)}`,
  45. nickname: `Test User ${timestamp}`,
  46. name: `Test Name ${timestamp}`,
  47. isDisabled: 0,
  48. isDeleted: 0,
  49. ...overrides
  50. };
  51. }
  52. /**
  53. * 创建测试角色数据
  54. */
  55. static createRoleData(overrides: Partial<Role> = {}): Partial<Role> {
  56. const timestamp = Date.now();
  57. return {
  58. name: `test_role_${timestamp}`,
  59. description: `Test role description ${timestamp}`,
  60. ...overrides
  61. };
  62. }
  63. /**
  64. * 在数据库中创建测试用户
  65. */
  66. static async createTestUser(dataSource: DataSource, overrides: Partial<UserEntity> = {}): Promise<UserEntity> {
  67. const userData = this.createUserData(overrides);
  68. const userRepository = dataSource.getRepository(UserEntity);
  69. const user = userRepository.create(userData);
  70. return await userRepository.save(user);
  71. }
  72. /**
  73. * 在数据库中创建测试角色
  74. */
  75. static async createTestRole(dataSource: DataSource, overrides: Partial<Role> = {}): Promise<Role> {
  76. const roleData = this.createRoleData(overrides);
  77. const roleRepository = dataSource.getRepository(Role);
  78. const role = roleRepository.create(roleData);
  79. return await roleRepository.save(role);
  80. }
  81. }
  82. /**
  83. * 集成测试数据库生命周期钩子
  84. */
  85. export function setupIntegrationDatabaseHooks() {
  86. beforeEach(async () => {
  87. await IntegrationTestDatabase.getDataSource();
  88. });
  89. afterEach(async () => {
  90. await IntegrationTestDatabase.cleanup();
  91. });
  92. }