integration-test-utils.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { IntegrationTestDatabase } from './integration-test-db';
  2. import { UserEntity } from '@d8d/server/modules/users/user.entity';
  3. import { ActivityEntity } from '@d8d/server/modules/activities/activity.entity';
  4. import { RouteEntity } from '@d8d/server/modules/routes/route.entity';
  5. import { LocationEntity } from '@d8d/server/modules/locations/location.entity';
  6. import { Passenger } from '@d8d/server/modules/passengers/passenger.entity';
  7. import { Order } from '@d8d/server/modules/orders/order.entity';
  8. /**
  9. * 集成测试断言工具
  10. */
  11. export class IntegrationTestAssertions {
  12. /**
  13. * 断言响应状态码
  14. */
  15. static expectStatus(response: { status: number }, expectedStatus: number): void {
  16. if (response.status !== expectedStatus) {
  17. throw new Error(`Expected status ${expectedStatus}, but got ${response.status}`);
  18. }
  19. }
  20. /**
  21. * 断言响应包含特定字段
  22. */
  23. static expectResponseToHave(response: { data: any }, expectedFields: Record<string, any>): void {
  24. for (const [key, value] of Object.entries(expectedFields)) {
  25. if (response.data[key] !== value) {
  26. throw new Error(`Expected field ${key} to be ${value}, but got ${response.data[key]}`);
  27. }
  28. }
  29. }
  30. /**
  31. * 断言响应包含特定结构
  32. */
  33. static expectResponseStructure(response: { data: any }, structure: Record<string, any>): void {
  34. for (const key of Object.keys(structure)) {
  35. if (!(key in response.data)) {
  36. throw new Error(`Expected response to have key: ${key}`);
  37. }
  38. }
  39. }
  40. /**
  41. * 断言用户存在于数据库中
  42. */
  43. static async expectUserToExist(username: string): Promise<void> {
  44. const dataSource = await IntegrationTestDatabase.getDataSource();
  45. if (!dataSource) {
  46. throw new Error('Database not initialized');
  47. }
  48. const userRepository = dataSource.getRepository(UserEntity);
  49. const user = await userRepository.findOne({ where: { username } });
  50. if (!user) {
  51. throw new Error(`Expected user ${username} to exist in database`);
  52. }
  53. }
  54. /**
  55. * 断言用户不存在于数据库中
  56. */
  57. static async expectUserNotToExist(username: string): Promise<void> {
  58. const dataSource = await IntegrationTestDatabase.getDataSource();
  59. if (!dataSource) {
  60. throw new Error('Database not initialized');
  61. }
  62. const userRepository = dataSource.getRepository(UserEntity);
  63. const user = await userRepository.findOne({ where: { username } });
  64. if (user) {
  65. throw new Error(`Expected user ${username} not to exist in database`);
  66. }
  67. }
  68. /**
  69. * 断言活动存在于数据库中
  70. */
  71. static async expectActivityToExist(activityId: number): Promise<void> {
  72. const dataSource = await IntegrationTestDatabase.getDataSource();
  73. if (!dataSource) {
  74. throw new Error('Database not initialized');
  75. }
  76. const activityRepository = dataSource.getRepository(ActivityEntity);
  77. const activity = await activityRepository.findOne({ where: { id: activityId } });
  78. if (!activity) {
  79. throw new Error(`Expected activity ${activityId} to exist in database`);
  80. }
  81. }
  82. /**
  83. * 断言活动不存在于数据库中
  84. */
  85. static async expectActivityNotToExist(activityId: number): Promise<void> {
  86. const dataSource = await IntegrationTestDatabase.getDataSource();
  87. if (!dataSource) {
  88. throw new Error('Database not initialized');
  89. }
  90. const activityRepository = dataSource.getRepository(ActivityEntity);
  91. const activity = await activityRepository.findOne({ where: { id: activityId } });
  92. if (activity) {
  93. throw new Error(`Expected activity ${activityId} not to exist in database`);
  94. }
  95. }
  96. /**
  97. * 断言路线存在于数据库中
  98. */
  99. static async expectRouteToExist(routeId: number): Promise<void> {
  100. const dataSource = await IntegrationTestDatabase.getDataSource();
  101. if (!dataSource) {
  102. throw new Error('Database not initialized');
  103. }
  104. const routeRepository = dataSource.getRepository(RouteEntity);
  105. const route = await routeRepository.findOne({ where: { id: routeId } });
  106. if (!route) {
  107. throw new Error(`Expected route ${routeId} to exist in database`);
  108. }
  109. }
  110. /**
  111. * 断言路线不存在于数据库中
  112. */
  113. static async expectRouteNotToExist(routeId: number): Promise<void> {
  114. const dataSource = await IntegrationTestDatabase.getDataSource();
  115. if (!dataSource) {
  116. throw new Error('Database not initialized');
  117. }
  118. const routeRepository = dataSource.getRepository(RouteEntity);
  119. const route = await routeRepository.findOne({ where: { id: routeId } });
  120. if (route) {
  121. throw new Error(`Expected route ${routeId} not to exist in database`);
  122. }
  123. }
  124. /**
  125. * 断言地点存在于数据库中
  126. */
  127. static async expectLocationToExist(locationId: number): Promise<void> {
  128. const dataSource = await IntegrationTestDatabase.getDataSource();
  129. if (!dataSource) {
  130. throw new Error('Database not initialized');
  131. }
  132. const locationRepository = dataSource.getRepository(LocationEntity);
  133. const location = await locationRepository.findOne({ where: { id: locationId } });
  134. if (!location) {
  135. throw new Error(`Expected location ${locationId} to exist in database`);
  136. }
  137. }
  138. /**
  139. * 断言地点不存在于数据库中
  140. */
  141. static async expectLocationNotToExist(locationId: number): Promise<void> {
  142. const dataSource = await IntegrationTestDatabase.getDataSource();
  143. if (!dataSource) {
  144. throw new Error('Database not initialized');
  145. }
  146. const locationRepository = dataSource.getRepository(LocationEntity);
  147. const location = await locationRepository.findOne({ where: { id: locationId } });
  148. if (location) {
  149. throw new Error(`Expected location ${locationId} not to exist in database`);
  150. }
  151. }
  152. /**
  153. * 断言乘客存在于数据库中
  154. */
  155. static async expectPassengerToExist(passengerId: number): Promise<void> {
  156. const dataSource = await IntegrationTestDatabase.getDataSource();
  157. if (!dataSource) {
  158. throw new Error('Database not initialized');
  159. }
  160. const passengerRepository = dataSource.getRepository(Passenger);
  161. const passenger = await passengerRepository.findOne({ where: { id: passengerId } });
  162. if (!passenger) {
  163. throw new Error(`Expected passenger ${passengerId} to exist in database`);
  164. }
  165. }
  166. /**
  167. * 断言乘客不存在于数据库中
  168. */
  169. static async expectPassengerNotToExist(passengerId: number): Promise<void> {
  170. const dataSource = await IntegrationTestDatabase.getDataSource();
  171. if (!dataSource) {
  172. throw new Error('Database not initialized');
  173. }
  174. const passengerRepository = dataSource.getRepository(Passenger);
  175. const passenger = await passengerRepository.findOne({ where: { id: passengerId } });
  176. if (passenger) {
  177. throw new Error(`Expected passenger ${passengerId} not to exist in database`);
  178. }
  179. }
  180. /**
  181. * 断言订单存在于数据库中
  182. */
  183. static async expectOrderToExist(orderId: number): Promise<void> {
  184. const dataSource = await IntegrationTestDatabase.getDataSource();
  185. if (!dataSource) {
  186. throw new Error('Database not initialized');
  187. }
  188. const orderRepository = dataSource.getRepository(Order);
  189. const order = await orderRepository.findOne({ where: { id: orderId } });
  190. if (!order) {
  191. throw new Error(`Expected order ${orderId} to exist in database`);
  192. }
  193. }
  194. /**
  195. * 断言订单不存在于数据库中
  196. */
  197. static async expectOrderNotToExist(orderId: number): Promise<void> {
  198. const dataSource = await IntegrationTestDatabase.getDataSource();
  199. if (!dataSource) {
  200. throw new Error('Database not initialized');
  201. }
  202. const orderRepository = dataSource.getRepository(Order);
  203. const order = await orderRepository.findOne({ where: { id: orderId } });
  204. if (order) {
  205. throw new Error(`Expected order ${orderId} not to exist in database`);
  206. }
  207. }
  208. /**
  209. * 根据ID获取订单
  210. */
  211. static async getOrderById(orderId: number): Promise<Order | null> {
  212. const dataSource = await IntegrationTestDatabase.getDataSource();
  213. if (!dataSource) {
  214. throw new Error('Database not initialized');
  215. }
  216. const orderRepository = dataSource.getRepository(Order);
  217. return await orderRepository.findOne({ where: { id: orderId } });
  218. }
  219. }