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 { 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 { 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 { const { IntegrationTestDatabase } = await import('@d8d/shared-test-util'); const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) { throw new Error('Database not initialized'); } return dataSource; } }