integration-test-utils.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { IntegrationTestDatabase } from '@d8d/shared-test-util';
  2. import { File } from '../../src/entities';
  3. /**
  4. * 集成测试断言工具
  5. */
  6. export class IntegrationTestAssertions {
  7. /**
  8. * 断言响应状态码
  9. */
  10. static expectStatus(response: { status: number }, expectedStatus: number): void {
  11. if (response.status !== expectedStatus) {
  12. throw new Error(`Expected status ${expectedStatus}, but got ${response.status}`);
  13. }
  14. }
  15. /**
  16. * 断言响应包含特定字段
  17. */
  18. static expectResponseToHave(response: { data: any }, expectedFields: Record<string, any>): void {
  19. for (const [key, value] of Object.entries(expectedFields)) {
  20. if (response.data[key] !== value) {
  21. throw new Error(`Expected field ${key} to be ${value}, but got ${response.data[key]}`);
  22. }
  23. }
  24. }
  25. /**
  26. * 断言响应包含特定结构
  27. */
  28. static expectResponseStructure(response: { data: any }, structure: Record<string, any>): void {
  29. for (const key of Object.keys(structure)) {
  30. if (!(key in response.data)) {
  31. throw new Error(`Expected response to have key: ${key}`);
  32. }
  33. }
  34. }
  35. /**
  36. * 断言文件存在于数据库中
  37. */
  38. static async expectFileToExist(name: string): Promise<void> {
  39. const dataSource = await IntegrationTestDatabase.getDataSource();
  40. if (!dataSource) {
  41. throw new Error('Database not initialized');
  42. }
  43. const fileRepository = dataSource.getRepository(File);
  44. const file = await fileRepository.findOne({ where: { name } });
  45. if (!file) {
  46. throw new Error(`Expected file ${name} to exist in database`);
  47. }
  48. }
  49. /**
  50. * 断言文件不存在于数据库中
  51. */
  52. static async expectFileNotToExist(name: string): Promise<void> {
  53. const dataSource = await IntegrationTestDatabase.getDataSource();
  54. if (!dataSource) {
  55. throw new Error('Database not initialized');
  56. }
  57. const fileRepository = dataSource.getRepository(File);
  58. const file = await fileRepository.findOne({ where: { name } });
  59. if (file) {
  60. throw new Error(`Expected file ${name} not to exist in database`);
  61. }
  62. }
  63. /**
  64. * 断言文件存在于数据库中(通过ID)
  65. */
  66. static async expectFileToExistById(id: number): Promise<void> {
  67. const dataSource = await IntegrationTestDatabase.getDataSource();
  68. if (!dataSource) {
  69. throw new Error('Database not initialized');
  70. }
  71. const fileRepository = dataSource.getRepository(File);
  72. const file = await fileRepository.findOne({ where: { id } });
  73. if (!file) {
  74. throw new Error(`Expected file with ID ${id} to exist in database`);
  75. }
  76. }
  77. /**
  78. * 断言文件不存在于数据库中(通过ID)
  79. */
  80. static async expectFileNotToExistById(id: number): Promise<void> {
  81. const dataSource = await IntegrationTestDatabase.getDataSource();
  82. if (!dataSource) {
  83. throw new Error('Database not initialized');
  84. }
  85. const fileRepository = dataSource.getRepository(File);
  86. const file = await fileRepository.findOne({ where: { id } });
  87. if (file) {
  88. throw new Error(`Expected file with ID ${id} not to exist in database`);
  89. }
  90. }
  91. }