test-data-factory.ts 8.3 KB

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