test-data-factory.ts 9.5 KB

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