| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import { IntegrationTestDatabase } from './integration-test-db';
- import { UserEntity } from '@d8d/server/modules/users/user.entity';
- import { ActivityEntity } from '@d8d/server/modules/activities/activity.entity';
- import { RouteEntity } from '@d8d/server/modules/routes/route.entity';
- import { LocationEntity } from '@d8d/server/modules/locations/location.entity';
- import { Passenger } from '@d8d/server/modules/passengers/passenger.entity';
- import { Order } from '@d8d/server/modules/orders/order.entity';
- /**
- * 集成测试断言工具
- */
- 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 expectUserToExist(username: string): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const userRepository = dataSource.getRepository(UserEntity);
- const user = await userRepository.findOne({ where: { username } });
- if (!user) {
- throw new Error(`Expected user ${username} to exist in database`);
- }
- }
- /**
- * 断言用户不存在于数据库中
- */
- static async expectUserNotToExist(username: string): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const userRepository = dataSource.getRepository(UserEntity);
- const user = await userRepository.findOne({ where: { username } });
- if (user) {
- throw new Error(`Expected user ${username} not to exist in database`);
- }
- }
- /**
- * 断言活动存在于数据库中
- */
- static async expectActivityToExist(activityId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const activityRepository = dataSource.getRepository(ActivityEntity);
- const activity = await activityRepository.findOne({ where: { id: activityId } });
- if (!activity) {
- throw new Error(`Expected activity ${activityId} to exist in database`);
- }
- }
- /**
- * 断言活动不存在于数据库中
- */
- static async expectActivityNotToExist(activityId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const activityRepository = dataSource.getRepository(ActivityEntity);
- const activity = await activityRepository.findOne({ where: { id: activityId } });
- if (activity) {
- throw new Error(`Expected activity ${activityId} not to exist in database`);
- }
- }
- /**
- * 断言路线存在于数据库中
- */
- static async expectRouteToExist(routeId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const routeRepository = dataSource.getRepository(RouteEntity);
- const route = await routeRepository.findOne({ where: { id: routeId } });
- if (!route) {
- throw new Error(`Expected route ${routeId} to exist in database`);
- }
- }
- /**
- * 断言路线不存在于数据库中
- */
- static async expectRouteNotToExist(routeId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const routeRepository = dataSource.getRepository(RouteEntity);
- const route = await routeRepository.findOne({ where: { id: routeId } });
- if (route) {
- throw new Error(`Expected route ${routeId} not to exist in database`);
- }
- }
- /**
- * 断言地点存在于数据库中
- */
- static async expectLocationToExist(locationId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const locationRepository = dataSource.getRepository(LocationEntity);
- const location = await locationRepository.findOne({ where: { id: locationId } });
- if (!location) {
- throw new Error(`Expected location ${locationId} to exist in database`);
- }
- }
- /**
- * 断言地点不存在于数据库中
- */
- static async expectLocationNotToExist(locationId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const locationRepository = dataSource.getRepository(LocationEntity);
- const location = await locationRepository.findOne({ where: { id: locationId } });
- if (location) {
- throw new Error(`Expected location ${locationId} not to exist in database`);
- }
- }
- /**
- * 断言乘客存在于数据库中
- */
- static async expectPassengerToExist(passengerId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const passengerRepository = dataSource.getRepository(Passenger);
- const passenger = await passengerRepository.findOne({ where: { id: passengerId } });
- if (!passenger) {
- throw new Error(`Expected passenger ${passengerId} to exist in database`);
- }
- }
- /**
- * 断言乘客不存在于数据库中
- */
- static async expectPassengerNotToExist(passengerId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const passengerRepository = dataSource.getRepository(Passenger);
- const passenger = await passengerRepository.findOne({ where: { id: passengerId } });
- if (passenger) {
- throw new Error(`Expected passenger ${passengerId} not to exist in database`);
- }
- }
- /**
- * 断言订单存在于数据库中
- */
- static async expectOrderToExist(orderId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const orderRepository = dataSource.getRepository(Order);
- const order = await orderRepository.findOne({ where: { id: orderId } });
- if (!order) {
- throw new Error(`Expected order ${orderId} to exist in database`);
- }
- }
- /**
- * 断言订单不存在于数据库中
- */
- static async expectOrderNotToExist(orderId: number): Promise<void> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const orderRepository = dataSource.getRepository(Order);
- const order = await orderRepository.findOne({ where: { id: orderId } });
- if (order) {
- throw new Error(`Expected order ${orderId} not to exist in database`);
- }
- }
- /**
- * 根据ID获取订单
- */
- static async getOrderById(orderId: number): Promise<Order | null> {
- const dataSource = await IntegrationTestDatabase.getDataSource();
- if (!dataSource) {
- throw new Error('Database not initialized');
- }
- const orderRepository = dataSource.getRepository(Order);
- return await orderRepository.findOne({ where: { id: orderId } });
- }
- }
|