integration-test-utils.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { DataSource } from 'typeorm';
  2. import { AreaEntity } from '../../src/modules/areas/area.entity';
  3. import { expect } from 'vitest';
  4. export class IntegrationTestAssertions {
  5. /**
  6. * 断言响应状态码
  7. */
  8. static expectStatus(response: any, expectedStatus: number): void {
  9. expect(response.status).toBe(expectedStatus);
  10. }
  11. /**
  12. * 断言地区存在
  13. */
  14. static async expectAreaToExist(areaName: string): Promise<void> {
  15. const dataSource = await this.getDataSource();
  16. const areaRepository = dataSource.getRepository(AreaEntity);
  17. const area = await areaRepository.findOne({
  18. where: { name: areaName }
  19. });
  20. expect(area).toBeDefined();
  21. expect(area?.name).toBe(areaName);
  22. }
  23. /**
  24. * 断言地区不存在
  25. */
  26. static async expectAreaNotToExist(areaName: string): Promise<void> {
  27. const dataSource = await this.getDataSource();
  28. const areaRepository = dataSource.getRepository(AreaEntity);
  29. const area = await areaRepository.findOne({
  30. where: { name: areaName }
  31. });
  32. expect(area).toBeNull();
  33. }
  34. /**
  35. * 获取数据源
  36. */
  37. private static async getDataSource(): Promise<DataSource> {
  38. const { IntegrationTestDatabase } = await import('@d8d/shared-test-util');
  39. const dataSource = await IntegrationTestDatabase.getDataSource();
  40. if (!dataSource) {
  41. throw new Error('Database not initialized');
  42. }
  43. return dataSource;
  44. }
  45. }