statistics.integration.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { JWTUtil } from '@d8d/shared-utils';
  5. import { UserEntity, Role } from '@d8d/user-module';
  6. import { File } from '@d8d/file-module';
  7. import { DisabledPerson, DisabledBankCard, DisabledPhoto, DisabledRemark, DisabledVisit } from '@d8d/allin-disability-module';
  8. import { BankName } from '@d8d/bank-names-module';
  9. import { Company } from '@d8d/allin-company-module/entities';
  10. import { Platform } from '@d8d/allin-platform-module';
  11. import { DataSource } from 'typeorm';
  12. import statisticsRoutes from '../../src/routes/statistics.routes';
  13. import { EmploymentOrder, OrderPerson, OrderPersonAsset } from '@d8d/allin-order-module/entities';
  14. import { WorkStatus, OrderStatus } from '@d8d/allin-enums';
  15. // 设置集成测试钩子
  16. setupIntegrationDatabaseHooksWithEntities([
  17. UserEntity,
  18. File,
  19. Role,
  20. Platform,
  21. Company,
  22. DisabledPerson,
  23. DisabledBankCard,
  24. DisabledPhoto,
  25. DisabledRemark,
  26. DisabledVisit,
  27. BankName,
  28. EmploymentOrder,
  29. OrderPerson,
  30. OrderPersonAsset
  31. ])
  32. describe('数据统计API集成测试', () => {
  33. let client: ReturnType<typeof testClient<typeof statisticsRoutes>>;
  34. let testToken: string;
  35. let testUser: UserEntity;
  36. let testCompany: Company;
  37. let testDisabledPerson: DisabledPerson;
  38. let testOrder: EmploymentOrder;
  39. let testOrderPerson: OrderPerson;
  40. let dataSource: DataSource;
  41. beforeEach(async () => {
  42. // 创建测试客户端
  43. client = testClient(statisticsRoutes);
  44. // 获取数据源
  45. dataSource = await IntegrationTestDatabase.getDataSource();
  46. // 创建测试用户
  47. const userRepository = dataSource.getRepository(UserEntity);
  48. testUser = userRepository.create({
  49. username: `test_user_${Date.now()}`,
  50. password: 'test_password',
  51. nickname: '测试用户',
  52. registrationSource: 'web'
  53. });
  54. await userRepository.save(testUser);
  55. // 生成测试用户的token
  56. testToken = JWTUtil.generateToken({
  57. id: testUser.id,
  58. username: testUser.username,
  59. roles: [{name:'user'}]
  60. });
  61. // 创建测试公司
  62. const companyRepository = dataSource.getRepository(Company);
  63. testCompany = companyRepository.create({
  64. companyName: '测试公司',
  65. contactPerson: '测试联系人',
  66. contactPhone: '13800138000',
  67. status: 1
  68. });
  69. await companyRepository.save(testCompany);
  70. // 创建测试文件
  71. const fileRepository = dataSource.getRepository(File);
  72. const testFile = fileRepository.create({
  73. name: 'test_file.pdf',
  74. type: 'application/pdf',
  75. size: 1024,
  76. path: `test/${Date.now()}_test_file.pdf`,
  77. uploadUserId: testUser.id,
  78. uploadTime: new Date(),
  79. createdAt: new Date(),
  80. updatedAt: new Date()
  81. });
  82. await fileRepository.save(testFile);
  83. // 创建测试银行名称记录
  84. const bankNameRepository = dataSource.getRepository(BankName);
  85. const testBankName = bankNameRepository.create({
  86. name: '测试银行',
  87. code: 'TEST001',
  88. remark: '测试银行',
  89. createdBy: testUser.id,
  90. updatedBy: testUser.id,
  91. status: 1
  92. });
  93. await bankNameRepository.save(testBankName);
  94. // 创建测试残疾人记录
  95. const disabledPersonRepository = dataSource.getRepository(DisabledPerson);
  96. testDisabledPerson = disabledPersonRepository.create({
  97. name: '测试残疾人',
  98. gender: '男',
  99. idCard: `tid_${Date.now() % 1000000}`,
  100. disabilityType: '视力残疾',
  101. disabilityLevel: '三级',
  102. province: '北京市',
  103. city: '北京市',
  104. jobStatus: 1,
  105. birthDate: new Date('1990-01-01'),
  106. idAddress: '测试地址',
  107. phone: '13800138000',
  108. canDirectContact: 1,
  109. disabilityId: `CJZ${Date.now() % 1000000}`
  110. });
  111. await disabledPersonRepository.save(testDisabledPerson);
  112. // 创建测试平台
  113. const platformRepository = dataSource.getRepository(Platform);
  114. const testPlatform = platformRepository.create({
  115. platformName: '测试平台',
  116. contactPerson: '测试联系人',
  117. contactPhone: '13800138000',
  118. status: 1
  119. });
  120. await platformRepository.save(testPlatform);
  121. // 创建测试订单
  122. const orderRepository = dataSource.getRepository(EmploymentOrder);
  123. testOrder = orderRepository.create({
  124. orderName: '测试订单',
  125. platformId: testPlatform.id,
  126. companyId: testCompany.id,
  127. channelId: 1,
  128. expectedStartDate: new Date(),
  129. orderStatus: OrderStatus.CONFIRMED,
  130. workStatus: WorkStatus.WORKING
  131. });
  132. await orderRepository.save(testOrder);
  133. // 创建测试订单人员
  134. const orderPersonRepository = dataSource.getRepository(OrderPerson);
  135. testOrderPerson = orderPersonRepository.create({
  136. orderId: testOrder.id,
  137. personId: testDisabledPerson.id,
  138. joinDate: new Date(),
  139. salaryDetail: 5000,
  140. workStatus: WorkStatus.WORKING
  141. });
  142. await orderPersonRepository.save(testOrderPerson);
  143. // 创建测试订单人员资产(打卡视频)
  144. const orderPersonAssetRepository = dataSource.getRepository(OrderPersonAsset);
  145. const testAsset = orderPersonAssetRepository.create({
  146. orderId: testOrder.id,
  147. personId: testDisabledPerson.id,
  148. assetType: 'checkin_video',
  149. assetFileType: 'video',
  150. fileId: testFile.id
  151. });
  152. await orderPersonAssetRepository.save(testAsset);
  153. });
  154. describe('GET /statistics/disability-type-distribution', () => {
  155. it('应该返回正确的残疾类型分布统计', async () => {
  156. // 测试实现待补充
  157. expect(true).toBe(true);
  158. });
  159. });
  160. describe('GET /statistics/gender-distribution', () => {
  161. it('应该返回正确的性别分布统计', async () => {
  162. // 测试实现待补充
  163. expect(true).toBe(true);
  164. });
  165. });
  166. describe('GET /statistics/age-distribution', () => {
  167. it('应该基于birth_date字段返回正确的年龄分布统计', async () => {
  168. // 测试实现待补充
  169. expect(true).toBe(true);
  170. });
  171. });
  172. describe('GET /statistics/household-distribution', () => {
  173. it('应该返回正确的户籍分布统计', async () => {
  174. // 测试实现待补充
  175. expect(true).toBe(true);
  176. });
  177. });
  178. describe('GET /statistics/job-status-distribution', () => {
  179. it('应该返回正确的在职状态分布统计', async () => {
  180. // 测试实现待补充
  181. expect(true).toBe(true);
  182. });
  183. });
  184. describe('GET /statistics/salary-distribution', () => {
  185. it('应该基于salary_detail字段返回正确的薪资分布统计', async () => {
  186. // 测试实现待补充
  187. expect(true).toBe(true);
  188. });
  189. });
  190. describe('企业数据隔离', () => {
  191. it('不同企业用户只能看到自己企业的统计', async () => {
  192. // 测试实现待补充
  193. expect(true).toBe(true);
  194. });
  195. });
  196. });