test-data-factory.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 createSpecificTestPrinter(
  54. dataSource: DataSource,
  55. tenantId: number,
  56. printerSn: string,
  57. printerKey: string,
  58. isDefault: number = 1,
  59. overrides: Partial<FeiePrinterMt> = {}
  60. ): Promise<FeiePrinterMt> {
  61. const printerRepository = dataSource.getRepository(FeiePrinterMt);
  62. const printer = printerRepository.create({
  63. tenantId,
  64. printerSn,
  65. printerKey,
  66. printerName: `打印机 ${printerSn}`,
  67. printerType: '58mm',
  68. printerStatus: 'ACTIVE',
  69. isDefault,
  70. ...overrides
  71. });
  72. return await printerRepository.save(printer);
  73. }
  74. /**
  75. * 创建测试打印任务
  76. */
  77. static async createTestPrintTask(dataSource: DataSource, tenantId: number, printerSn: string, overrides: Partial<FeiePrintTaskMt> = {}): Promise<FeiePrintTaskMt> {
  78. const taskRepository = dataSource.getRepository(FeiePrintTaskMt);
  79. const timestamp = Date.now();
  80. const task = taskRepository.create({
  81. tenantId,
  82. taskId: `TASK_${timestamp}`,
  83. printerSn,
  84. content: '<CB>测试打印内容</CB><BR>',
  85. printType: 'RECEIPT',
  86. printStatus: 'PENDING',
  87. retryCount: 0,
  88. ...overrides
  89. });
  90. return await taskRepository.save(task);
  91. }
  92. /**
  93. * 创建测试飞鹅配置
  94. */
  95. static async createTestFeieConfig(dataSource: DataSource, tenantId: number, overrides: Partial<FeieConfigMt> = {}): Promise<FeieConfigMt> {
  96. const configRepository = dataSource.getRepository(FeieConfigMt);
  97. const config = configRepository.create({
  98. tenantId,
  99. configKey: 'feie.api.user',
  100. configValue: 'test_user',
  101. configType: 'STRING',
  102. description: '飞鹅API用户',
  103. ...overrides
  104. });
  105. return await configRepository.save(config);
  106. }
  107. /**
  108. * 创建完整的飞鹅API配置
  109. */
  110. static async createFullFeieConfig(dataSource: DataSource, tenantId: number): Promise<void> {
  111. const configRepository = dataSource.getRepository(FeieConfigMt);
  112. const configs = [
  113. {
  114. tenantId,
  115. configKey: 'feie.api.user',
  116. configValue: 'test_user',
  117. configType: 'STRING',
  118. description: '飞鹅API用户'
  119. },
  120. {
  121. tenantId,
  122. configKey: 'feie.api.ukey',
  123. configValue: 'test_ukey',
  124. configType: 'STRING',
  125. description: '飞鹅API密钥'
  126. },
  127. {
  128. tenantId,
  129. configKey: 'feie.api.base_url',
  130. configValue: 'https://api.feieyun.cn/Api/Open/',
  131. configType: 'STRING',
  132. description: '飞鹅API基础URL'
  133. },
  134. {
  135. tenantId,
  136. configKey: 'feie.api.timeout',
  137. configValue: '10000',
  138. configType: 'NUMBER',
  139. description: 'API超时时间'
  140. },
  141. {
  142. tenantId,
  143. configKey: 'feie.api.max_retries',
  144. configValue: '3',
  145. configType: 'NUMBER',
  146. description: '最大重试次数'
  147. }
  148. ];
  149. for (const config of configs) {
  150. await configRepository.save(configRepository.create(config));
  151. }
  152. }
  153. /**
  154. * 创建完整的测试环境数据
  155. */
  156. static async createCompleteTestData(dataSource: DataSource, tenantId: number = 1): Promise<{
  157. user: UserEntityMt;
  158. printer: FeiePrinterMt;
  159. task: FeiePrintTaskMt;
  160. }> {
  161. // 创建用户
  162. const user = await this.createTestUser(dataSource, tenantId);
  163. // 创建打印机
  164. const printer = await this.createTestPrinter(dataSource, tenantId);
  165. // 创建打印任务
  166. const task = await this.createTestPrintTask(dataSource, tenantId, printer.printerSn);
  167. // 创建配置
  168. await this.createFullFeieConfig(dataSource, tenantId);
  169. return {
  170. user,
  171. printer,
  172. task
  173. };
  174. }
  175. /**
  176. * 为测试用户生成JWT token
  177. */
  178. static generateUserToken(user: UserEntityMt): string {
  179. return JWTUtil.generateToken({
  180. id: user.id,
  181. username: user.username,
  182. tenantId: user.tenantId
  183. });
  184. }
  185. /**
  186. * 为管理员生成JWT token
  187. */
  188. static generateAdminToken(tenantId: number): string {
  189. return JWTUtil.generateToken({
  190. id: 1,
  191. username: 'admin',
  192. tenantId
  193. });
  194. }
  195. }