integration-test-utils.ts 6.7 KB

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