integration-test-utils.ts 5.5 KB

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