| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import { IntegrationTestDatabase } from '@d8d/shared-test-util';
- import { File } from '../../src/entities';
- /**
- * 集成测试断言工具
- */
- export class IntegrationTestAssertions {
- /**
- * 断言响应状态码
- */
- static expectStatus(response: { status: number }, expectedStatus: number): void {
- if (response.status !== expectedStatus) {
- throw new Error(`Expected status ${expectedStatus}, but got ${response.status}`);
- }
- }
- /**
- * 断言响应包含特定字段
- */
- static expectResponseToHave(response: { data: any }, expectedFields: Record<string, any>): void {
- for (const [key, value] of Object.entries(expectedFields)) {
- if (response.data[key] !== value) {
- throw new Error(`Expected field ${key} to be ${value}, but got ${response.data[key]}`);
- }
- }
- }
- /**
- * 断言响应包含特定结构
- */
- static expectResponseStructure(response: { data: any }, structure: Record<string, any>): void {
- for (const key of Object.keys(structure)) {
- if (!(key in response.data)) {
- throw new Error(`Expected response to have key: ${key}`);
- }
- }
- }
- /**
- * 断言文件存在于数据库中
- */
- static async expectFileToExist(name: string): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const fileRepository = dataSource.getRepository(File);
- const file = await fileRepository.findOne({ where: { name } });
- if (!file) {
- throw new Error(`Expected file ${name} to exist in database`);
- }
- }
- /**
- * 断言文件不存在于数据库中
- */
- static async expectFileNotToExist(name: string): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const fileRepository = dataSource.getRepository(File);
- const file = await fileRepository.findOne({ where: { name } });
- if (file) {
- throw new Error(`Expected file ${name} not to exist in database`);
- }
- }
- /**
- * 断言文件存在于数据库中(通过ID)
- */
- static async expectFileToExistById(id: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const fileRepository = dataSource.getRepository(File);
- const file = await fileRepository.findOne({ where: { id } });
- if (!file) {
- throw new Error(`Expected file with ID ${id} to exist in database`);
- }
- }
- /**
- * 断言文件不存在于数据库中(通过ID)
- */
- static async expectFileNotToExistById(id: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const fileRepository = dataSource.getRepository(File);
- const file = await fileRepository.findOne({ where: { id } });
- if (file) {
- throw new Error(`Expected file with ID ${id} not to exist in database`);
- }
- }
- }
|