| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { DataSource } from 'typeorm';
- import { AreaEntity } from '../../src/modules/areas/area.entity';
- export class IntegrationTestAssertions {
- /**
- * 断言响应状态码
- */
- static expectStatus(response: any, expectedStatus: number): void {
- expect(response.status).toBe(expectedStatus);
- }
- /**
- * 断言地区存在
- */
- static async expectAreaToExist(areaName: string): Promise<void> {
- const dataSource = await this.getDataSource();
- const areaRepository = dataSource.getRepository(AreaEntity);
- const area = await areaRepository.findOne({
- where: { name: areaName }
- });
- expect(area).toBeDefined();
- expect(area?.name).toBe(areaName);
- }
- /**
- * 断言地区不存在
- */
- static async expectAreaNotToExist(areaName: string): Promise<void> {
- const dataSource = await this.getDataSource();
- const areaRepository = dataSource.getRepository(AreaEntity);
- const area = await areaRepository.findOne({
- where: { name: areaName }
- });
- expect(area).toBeNull();
- }
- /**
- * 获取数据源
- */
- private static async getDataSource(): Promise<DataSource> {
- const { IntegrationTestDatabase } = await import('@d8d/shared-test-util');
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- return dataSource;
- }
- }
|