test-data-factory.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { DataSource } from 'typeorm';
  2. import { EmploymentOrder, OrderPerson, OrderPersonAsset, AssetType, AssetFileType } from '../../src/entities';
  3. import { OrderStatus, WorkStatus } from '@d8d/allin-enums';
  4. import { File } from '@d8d/file-module';
  5. /**
  6. * 订单模块测试数据工厂类
  7. * 用于生成测试数据,避免测试代码中的重复
  8. */
  9. export class OrderTestDataFactory {
  10. /**
  11. * 创建测试用工订单数据
  12. */
  13. static createEmploymentOrderData(overrides: Partial<EmploymentOrder> = {}): Partial<EmploymentOrder> {
  14. const timestamp = Math.floor(Math.random() * 100000);
  15. return {
  16. orderName: `测试订单_${timestamp}`,
  17. platformId: 1,
  18. companyId: 1,
  19. channelId: 1,
  20. expectedStartDate: new Date('2025-01-01'),
  21. orderStatus: OrderStatus.DRAFT,
  22. workStatus: WorkStatus.NOT_WORKING,
  23. ...overrides
  24. };
  25. }
  26. /**
  27. * 在数据库中创建测试用工订单
  28. */
  29. static async createTestEmploymentOrder(
  30. dataSource: DataSource,
  31. overrides: Partial<EmploymentOrder> = {}
  32. ): Promise<EmploymentOrder> {
  33. const orderData = this.createEmploymentOrderData(overrides);
  34. const orderRepository = dataSource.getRepository(EmploymentOrder);
  35. const order = new EmploymentOrder(orderData);
  36. return await orderRepository.save(order);
  37. }
  38. /**
  39. * 创建测试订单人员数据
  40. */
  41. static createOrderPersonData(
  42. orderId: number,
  43. personId: number,
  44. overrides: Partial<OrderPerson> = {}
  45. ): Partial<OrderPerson> {
  46. return {
  47. orderId,
  48. personId,
  49. joinDate: new Date('2025-01-15'),
  50. salaryDetail: 5000.00,
  51. ...overrides
  52. };
  53. }
  54. /**
  55. * 在数据库中创建测试订单人员
  56. */
  57. static async createTestOrderPerson(
  58. dataSource: DataSource,
  59. orderId: number,
  60. personId: number,
  61. overrides: Partial<OrderPerson> = {}
  62. ): Promise<OrderPerson> {
  63. const personData = this.createOrderPersonData(orderId, personId, overrides);
  64. const personRepository = dataSource.getRepository(OrderPerson);
  65. const person = new OrderPerson(personData);
  66. return await personRepository.save(person);
  67. }
  68. /**
  69. * 创建测试订单人员资产数据
  70. */
  71. static createOrderPersonAssetData(
  72. orderId: number,
  73. personId: number,
  74. fileId: number,
  75. overrides: Partial<OrderPersonAsset> = {}
  76. ): Partial<OrderPersonAsset> {
  77. const timestamp = Math.floor(Math.random() * 100000);
  78. return {
  79. orderId,
  80. personId,
  81. fileId,
  82. assetType: AssetType.DISABILITY_CERT,
  83. assetFileType: AssetFileType.IMAGE,
  84. ...overrides
  85. };
  86. }
  87. /**
  88. * 在数据库中创建测试订单人员资产
  89. */
  90. static async createTestOrderPersonAsset(
  91. dataSource: DataSource,
  92. orderId: number,
  93. personId: number,
  94. fileId: number,
  95. overrides: Partial<OrderPersonAsset> = {}
  96. ): Promise<OrderPersonAsset> {
  97. const assetData = this.createOrderPersonAssetData(orderId, personId, fileId, overrides);
  98. const assetRepository = dataSource.getRepository(OrderPersonAsset);
  99. const asset = new OrderPersonAsset(assetData);
  100. return await assetRepository.save(asset);
  101. }
  102. /**
  103. * 创建测试文件数据
  104. * 注意:这里只是创建File实体的数据,实际文件上传需要调用文件模块的API
  105. */
  106. static createFileData(overrides: Partial<File> = {}): Partial<File> {
  107. const timestamp = Math.floor(Math.random() * 100000);
  108. return {
  109. name: `test_file_${timestamp}.jpg`, // 注意:File实体使用name字段,不是filename
  110. type: 'image/jpeg',
  111. size: 1024,
  112. path: `test/${timestamp}_test_file.jpg`,
  113. uploadUserId: 1,
  114. uploadTime: new Date(),
  115. createdAt: new Date(),
  116. updatedAt: new Date(),
  117. ...overrides
  118. };
  119. }
  120. /**
  121. * 在数据库中创建测试文件
  122. * 注意:需要文件模块的File实体已注册到DataSource中
  123. */
  124. static async createTestFile(
  125. dataSource: DataSource,
  126. overrides: Partial<File> = {}
  127. ): Promise<File> {
  128. const fileData = this.createFileData(overrides);
  129. const fileRepository = dataSource.getRepository(File);
  130. const file = fileRepository.create(fileData);
  131. return await fileRepository.save(file);
  132. }
  133. /**
  134. * 创建完整的测试订单环境(包含订单、人员、资产、文件)
  135. */
  136. static async createCompleteOrderEnvironment(
  137. dataSource: DataSource,
  138. options: {
  139. orderOverrides?: Partial<EmploymentOrder>;
  140. personId?: number;
  141. personOverrides?: Partial<OrderPerson>;
  142. fileOverrides?: Partial<File>;
  143. assetOverrides?: Partial<OrderPersonAsset>;
  144. } = {}
  145. ): Promise<{
  146. order: EmploymentOrder;
  147. orderPerson: OrderPerson;
  148. file: File;
  149. orderPersonAsset: OrderPersonAsset;
  150. }> {
  151. // 创建订单
  152. const order = await this.createTestEmploymentOrder(dataSource, options.orderOverrides);
  153. // 创建文件
  154. const file = await this.createTestFile(dataSource, options.fileOverrides);
  155. // 创建订单人员
  156. const personId = options.personId || 1;
  157. const orderPerson = await this.createTestOrderPerson(
  158. dataSource,
  159. order.id,
  160. personId,
  161. options.personOverrides
  162. );
  163. // 创建订单人员资产
  164. const orderPersonAsset = await this.createTestOrderPersonAsset(
  165. dataSource,
  166. order.id,
  167. personId,
  168. file.id,
  169. options.assetOverrides
  170. );
  171. return { order, orderPerson, file, orderPersonAsset };
  172. }
  173. /**
  174. * 创建批量添加人员的测试数据
  175. */
  176. static createBatchAddPersonsData(
  177. orderId: number,
  178. count: number = 3
  179. ): Array<{ personId: number; joinDate: string; salaryDetail: number }> {
  180. const persons: Array<{ personId: number; joinDate: string; salaryDetail: number }> = [];
  181. for (let i = 1; i <= count; i++) {
  182. persons.push({
  183. personId: i,
  184. joinDate: `2025-01-${10 + i}`,
  185. salaryDetail: 4000 + i * 500
  186. });
  187. }
  188. return persons;
  189. }
  190. /**
  191. * 创建不同订单状态的测试数据
  192. */
  193. static createOrderWithStatus(
  194. status: OrderStatus,
  195. overrides: Partial<EmploymentOrder> = {}
  196. ): Partial<EmploymentOrder> {
  197. const baseData = this.createEmploymentOrderData(overrides);
  198. const result: Partial<EmploymentOrder> = {
  199. ...baseData,
  200. orderStatus: status
  201. };
  202. if (status === OrderStatus.IN_PROGRESS) {
  203. result.actualStartDate = new Date();
  204. } else if (status === OrderStatus.COMPLETED) {
  205. result.actualStartDate = new Date('2025-01-01');
  206. result.actualEndDate = new Date('2025-12-31');
  207. }
  208. return result;
  209. }
  210. /**
  211. * 创建不同工作状态的测试数据
  212. */
  213. static createOrderWithWorkStatus(
  214. workStatus: WorkStatus,
  215. overrides: Partial<EmploymentOrder> = {}
  216. ): Partial<EmploymentOrder> {
  217. const baseData = this.createEmploymentOrderData(overrides);
  218. return {
  219. ...baseData,
  220. workStatus,
  221. ...(workStatus === WorkStatus.WORKING && { actualStartDate: new Date() }),
  222. ...(workStatus === WorkStatus.RESIGNED && {
  223. actualStartDate: new Date('2025-01-01'),
  224. actualEndDate: new Date('2025-06-30')
  225. })
  226. };
  227. }
  228. /**
  229. * 创建不同资产类型的测试数据
  230. */
  231. static createAssetWithType(
  232. orderId: number,
  233. assetType: AssetType,
  234. assetFileType: AssetFileType,
  235. overrides: Partial<OrderPersonAsset> = {}
  236. ): Partial<OrderPersonAsset> {
  237. return {
  238. orderId,
  239. personId: 1,
  240. fileId: 1,
  241. assetType,
  242. assetFileType,
  243. ...overrides
  244. };
  245. }
  246. /**
  247. * 生成随机ID(用于测试中的personId等)
  248. */
  249. static generateRandomId(min: number = 1, max: number = 1000): number {
  250. return Math.floor(Math.random() * (max - min + 1)) + min;
  251. }
  252. /**
  253. * 生成随机日期字符串(YYYY-MM-DD格式)
  254. */
  255. static generateRandomDate(startYear: number = 2024, endYear: number = 2025): string {
  256. const year = Math.floor(Math.random() * (endYear - startYear + 1)) + startYear;
  257. const month = Math.floor(Math.random() * 12) + 1;
  258. const day = Math.floor(Math.random() * 28) + 1; // 简单处理,避免日期无效
  259. return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
  260. }
  261. /**
  262. * 生成随机金额(保留2位小数)
  263. */
  264. static generateRandomAmount(min: number = 1000, max: number = 10000): number {
  265. const amount = Math.random() * (max - min) + min;
  266. return Math.round(amount * 100) / 100;
  267. }
  268. }