goods-test-factory.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { DataSource } from 'typeorm';
  2. import { UserEntityMt } from '@d8d/user-module-mt';
  3. import { SupplierMt } from '@d8d/supplier-module-mt';
  4. import { MerchantMt } from '@d8d/merchant-module-mt';
  5. import { FileMt } from '@d8d/file-module-mt';
  6. import { GoodsMt, GoodsCategoryMt } from '../../src/entities/index.mt';
  7. export class GoodsTestFactory {
  8. private dataSource: DataSource;
  9. constructor(dataSource: DataSource) {
  10. this.dataSource = dataSource;
  11. }
  12. /**
  13. * 创建测试用户
  14. */
  15. async createTestUser(tenantId: number = 1, overrides: Partial<UserEntityMt> = {}): Promise<UserEntityMt> {
  16. const userRepository = this.dataSource.getRepository(UserEntityMt);
  17. const user = userRepository.create({
  18. tenantId,
  19. username: `test_user_${Math.floor(Math.random() * 100000)}`,
  20. password: 'test_password',
  21. nickname: '测试用户',
  22. registrationSource: 'web',
  23. ...overrides
  24. });
  25. return await userRepository.save(user);
  26. }
  27. /**
  28. * 创建测试管理员用户
  29. */
  30. async createTestAdmin(overrides: Partial<UserEntityMt> = {}): Promise<UserEntityMt> {
  31. const userRepository = this.dataSource.getRepository(UserEntityMt);
  32. const admin = userRepository.create({
  33. tenantId: 1,
  34. username: `test_admin_${Math.floor(Math.random() * 100000)}`,
  35. password: 'admin_password',
  36. nickname: '测试管理员',
  37. registrationSource: 'web',
  38. ...overrides
  39. });
  40. return await userRepository.save(admin);
  41. }
  42. /**
  43. * 创建测试供应商
  44. */
  45. async createTestSupplier(createdBy: number, overrides: Partial<SupplierMt> = {}): Promise<SupplierMt> {
  46. const supplierRepository = this.dataSource.getRepository(SupplierMt);
  47. const supplier = supplierRepository.create({
  48. tenantId: 1,
  49. name: '测试供应商',
  50. username: `test_supplier_${Math.floor(Math.random() * 100000)}`,
  51. password: 'password123',
  52. phone: '13800138000',
  53. realname: '测试供应商',
  54. state: 1,
  55. createdBy,
  56. ...overrides
  57. });
  58. return await supplierRepository.save(supplier);
  59. }
  60. /**
  61. * 创建测试商户
  62. */
  63. async createTestMerchant(createdBy: number, overrides: Partial<MerchantMt> = {}): Promise<MerchantMt> {
  64. const merchantRepository = this.dataSource.getRepository(MerchantMt);
  65. const merchant = merchantRepository.create({
  66. tenantId: 1,
  67. name: '测试商户',
  68. username: `test_merchant_${Math.floor(Math.random() * 100000)}`,
  69. password: 'password123',
  70. phone: '13800138001',
  71. realname: '测试商户',
  72. state: 1,
  73. createdBy,
  74. ...overrides
  75. });
  76. return await merchantRepository.save(merchant);
  77. }
  78. /**
  79. * 创建测试商品分类
  80. */
  81. async createTestCategory(createdBy: number, overrides: Partial<GoodsCategoryMt> = {}): Promise<GoodsCategoryMt> {
  82. const categoryRepository = this.dataSource.getRepository(GoodsCategoryMt);
  83. const category = categoryRepository.create({
  84. tenantId: 1,
  85. name: `测试分类_${Math.floor(Math.random() * 100000)}`,
  86. parentId: 0,
  87. level: 1,
  88. state: 1,
  89. createdBy,
  90. ...overrides
  91. });
  92. return await categoryRepository.save(category);
  93. }
  94. /**
  95. * 创建测试商品
  96. */
  97. async createTestGoods(
  98. createdBy: number,
  99. overrides: Partial<GoodsMt> = {}
  100. ): Promise<GoodsMt> {
  101. const goodsRepository = this.dataSource.getRepository(GoodsMt);
  102. // 从overrides中获取tenantId,默认为1
  103. const tenantId = overrides.tenantId || 1;
  104. // 创建默认的分类、供应商、商户用于测试
  105. const testCategory = await this.createTestCategory(createdBy, { tenantId });
  106. const testSupplier = await this.createTestSupplier(createdBy, { tenantId });
  107. const testMerchant = await this.createTestMerchant(createdBy, { tenantId });
  108. const goods = goodsRepository.create({
  109. tenantId,
  110. name: `测试商品_${Math.floor(Math.random() * 100000)}`,
  111. price: 100.00,
  112. costPrice: 80.00,
  113. categoryId1: testCategory.id,
  114. categoryId2: testCategory.id,
  115. categoryId3: testCategory.id,
  116. goodsType: 1,
  117. supplierId: testSupplier.id,
  118. merchantId: testMerchant.id,
  119. state: 1,
  120. stock: 100,
  121. lowestBuy: 1,
  122. createdBy,
  123. ...overrides
  124. });
  125. return await goodsRepository.save(goods);
  126. }
  127. /**
  128. * 创建测试文件
  129. */
  130. async createTestFile(uploadUserId: number, overrides: Partial<FileMt> = {}): Promise<FileMt> {
  131. const fileRepository = this.dataSource.getRepository(FileMt);
  132. const file = fileRepository.create({
  133. tenantId: 1,
  134. name: `test_file_${Math.floor(Math.random() * 100000)}.jpg`,
  135. originalName: 'test.jpg',
  136. path: '/uploads/test.jpg',
  137. mimeType: 'image/jpeg',
  138. size: 1024,
  139. uploadUserId,
  140. uploadTime: new Date(),
  141. ...overrides
  142. });
  143. return await fileRepository.save(file);
  144. }
  145. /**
  146. * 创建完整的测试环境(用户、供应商、商户、分类、商品)
  147. */
  148. async createFullTestEnvironment() {
  149. const testUser = await this.createTestUser();
  150. const testSupplier = await this.createTestSupplier(testUser.id);
  151. const testMerchant = await this.createTestMerchant(testUser.id);
  152. const testCategory = await this.createTestCategory(testUser.id);
  153. const testGoods = await this.createTestGoods(testUser.id);
  154. return {
  155. testUser,
  156. testSupplier,
  157. testMerchant,
  158. testCategory,
  159. testGoods
  160. };
  161. }
  162. /**
  163. * 清理测试数据
  164. */
  165. async cleanup() {
  166. const repositories = {
  167. goods: this.dataSource.getRepository(GoodsMt),
  168. category: this.dataSource.getRepository(GoodsCategoryMt),
  169. supplier: this.dataSource.getRepository(SupplierMt),
  170. merchant: this.dataSource.getRepository(MerchantMt),
  171. user: this.dataSource.getRepository(UserEntityMt),
  172. file: this.dataSource.getRepository(FileMt)
  173. };
  174. // 按依赖关系顺序删除数据
  175. await repositories.goods.delete({});
  176. await repositories.category.delete({});
  177. await repositories.supplier.delete({});
  178. await repositories.merchant.delete({});
  179. await repositories.file.delete({});
  180. await repositories.user.delete({});
  181. }
  182. }