| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import { DataSource } from 'typeorm';
- import { AreaEntity } from '../../src/modules/areas/area.entity';
- import { expect } from 'vitest';
- 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;
- }
- }
|