test-data-factory.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { DataSource } from 'typeorm';
  2. import { UserEntityMt } from '@d8d/user-module-mt';
  3. import { JWTUtil } from '@d8d/shared-utils';
  4. import { FeiePrinterMt, FeiePrintTaskMt, FeieConfigMt } from '../../src/entities';
  5. /**
  6. * 飞鹅打印模块测试数据工厂类
  7. */
  8. export class FeieTestDataFactory {
  9. /**
  10. * 创建测试用户数据
  11. */
  12. static createUserData(overrides: Partial<UserEntityMt> = {}): Partial<UserEntityMt> {
  13. const timestamp = Math.floor(Math.random() * 100000);
  14. return {
  15. username: `test_user_${timestamp}`,
  16. password: 'test_password',
  17. nickname: '测试用户',
  18. registrationSource: 'web',
  19. isDisabled: 0,
  20. ...overrides
  21. };
  22. }
  23. /**
  24. * 在数据库中创建测试用户
  25. */
  26. static async createTestUser(dataSource: DataSource, tenantId: number, overrides: Partial<UserEntityMt> = {}): Promise<UserEntityMt> {
  27. const userData = this.createUserData({ tenantId, ...overrides });
  28. const userRepository = dataSource.getRepository(UserEntityMt);
  29. const user = userRepository.create(userData);
  30. return await userRepository.save(user);
  31. }
  32. /**
  33. * 创建测试打印机
  34. */
  35. static async createTestPrinter(dataSource: DataSource, tenantId: number, overrides: Partial<FeiePrinterMt> = {}): Promise<FeiePrinterMt> {
  36. const printerRepository = dataSource.getRepository(FeiePrinterMt);
  37. const timestamp = Math.floor(Math.random() * 100000);
  38. const printer = printerRepository.create({
  39. tenantId,
  40. printerSn: `TEST_SN_${timestamp}`,
  41. printerKey: `TEST_KEY_${timestamp}`,
  42. printerName: '测试打印机',
  43. printerType: '58mm',
  44. printerStatus: 'ACTIVE',
  45. isDefault: 0,
  46. ...overrides
  47. });
  48. return await printerRepository.save(printer);
  49. }
  50. /**
  51. * 创建测试打印任务
  52. */
  53. static async createTestPrintTask(dataSource: DataSource, tenantId: number, printerSn: string, overrides: Partial<FeiePrintTaskMt> = {}): Promise<FeiePrintTaskMt> {
  54. const taskRepository = dataSource.getRepository(FeiePrintTaskMt);
  55. const timestamp = Date.now();
  56. const task = taskRepository.create({
  57. tenantId,
  58. taskId: `TASK_${timestamp}`,
  59. printerSn,
  60. content: '<CB>测试打印内容</CB><BR>',
  61. printType: 'RECEIPT',
  62. printStatus: 'PENDING',
  63. retryCount: 0,
  64. ...overrides
  65. });
  66. return await taskRepository.save(task);
  67. }
  68. /**
  69. * 创建测试飞鹅配置
  70. */
  71. static async createTestFeieConfig(dataSource: DataSource, tenantId: number, overrides: Partial<FeieConfigMt> = {}): Promise<FeieConfigMt> {
  72. const configRepository = dataSource.getRepository(FeieConfigMt);
  73. const config = configRepository.create({
  74. tenantId,
  75. configKey: 'feie.api.user',
  76. configValue: 'test_user',
  77. configType: 'STRING',
  78. description: '飞鹅API用户',
  79. ...overrides
  80. });
  81. return await configRepository.save(config);
  82. }
  83. /**
  84. * 创建完整的飞鹅API配置
  85. */
  86. static async createFullFeieConfig(dataSource: DataSource, tenantId: number): Promise<void> {
  87. const configRepository = dataSource.getRepository(FeieConfigMt);
  88. const configs = [
  89. {
  90. tenantId,
  91. configKey: 'feie.api.user',
  92. configValue: 'test_user',
  93. configType: 'STRING',
  94. description: '飞鹅API用户'
  95. },
  96. {
  97. tenantId,
  98. configKey: 'feie.api.ukey',
  99. configValue: 'test_ukey',
  100. configType: 'STRING',
  101. description: '飞鹅API密钥'
  102. },
  103. {
  104. tenantId,
  105. configKey: 'feie.api.base_url',
  106. configValue: 'https://api.feieyun.cn/Api/Open/',
  107. configType: 'STRING',
  108. description: '飞鹅API基础URL'
  109. },
  110. {
  111. tenantId,
  112. configKey: 'feie.api.timeout',
  113. configValue: '10000',
  114. configType: 'NUMBER',
  115. description: 'API超时时间'
  116. },
  117. {
  118. tenantId,
  119. configKey: 'feie.api.max_retries',
  120. configValue: '3',
  121. configType: 'NUMBER',
  122. description: '最大重试次数'
  123. }
  124. ];
  125. for (const config of configs) {
  126. await configRepository.save(configRepository.create(config));
  127. }
  128. }
  129. /**
  130. * 创建完整的测试环境数据
  131. */
  132. static async createCompleteTestData(dataSource: DataSource, tenantId: number = 1): Promise<{
  133. user: UserEntityMt;
  134. printer: FeiePrinterMt;
  135. task: FeiePrintTaskMt;
  136. }> {
  137. // 创建用户
  138. const user = await this.createTestUser(dataSource, tenantId);
  139. // 创建打印机
  140. const printer = await this.createTestPrinter(dataSource, tenantId);
  141. // 创建打印任务
  142. const task = await this.createTestPrintTask(dataSource, tenantId, printer.printerSn);
  143. // 创建配置
  144. await this.createFullFeieConfig(dataSource, tenantId);
  145. return {
  146. user,
  147. printer,
  148. task
  149. };
  150. }
  151. /**
  152. * 为测试用户生成JWT token
  153. */
  154. static generateUserToken(user: UserEntityMt): string {
  155. return JWTUtil.generateToken({
  156. id: user.id,
  157. username: user.username,
  158. tenantId: user.tenantId
  159. });
  160. }
  161. /**
  162. * 为管理员生成JWT token
  163. */
  164. static generateAdminToken(tenantId: number): string {
  165. return JWTUtil.generateToken({
  166. id: 1,
  167. username: 'admin',
  168. tenantId
  169. });
  170. }
  171. }