integration-test-utils.ts 1.4 KB

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