test-data-factory.ts 10 KB

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