2
0

integration-test-utils.ts 4.4 KB

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