statistics.integration.test.ts 7.0 KB

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