Parcourir la source

✨ feat(geo-areas): 增强管理地区API测试功能

- 添加@d8d/file-module依赖以支持文件相关功能
- 扩展测试数据库钩子,添加UserEntity、File和Role实体支持
- 修复子树验证的潜在空值问题,添加subTree存在性检查
- 添加测试数据工厂类,支持创建测试用户数据和用户实体

🔧 chore(geo-areas): 更新依赖关系

- 在package.json中添加@d8d/file-module依赖
- 更新pnpm-lock.yaml以反映最新的依赖关系
yourname il y a 3 semaines
Parent
commit
1b23207593

+ 1 - 0
packages/geo-areas/package.json

@@ -50,6 +50,7 @@
   "dependencies": {
     "@d8d/auth-module": "workspace:*",
     "@d8d/user-module": "workspace:*",
+    "@d8d/file-module": "workspace:*",
     "@d8d/shared-types": "workspace:*",
     "@d8d/shared-utils": "workspace:*",
     "@d8d/shared-crud": "workspace:*",

+ 5 - 2
packages/geo-areas/tests/integration/admin-areas.integration.test.ts

@@ -7,13 +7,15 @@ import {
 import { IntegrationTestAssertions } from '../utils/integration-test-utils';
 import adminAreaRoutes from '../../src/api/admin/areas';
 import { AreaEntity, AreaLevel } from '../../src/modules/areas/area.entity';
+import { Role, UserEntity } from '@d8d/user-module';
+import { File } from '@d8d/file-module';
 import { DisabledStatus } from '@d8d/shared-types';
 import { TestDataFactory } from '../utils/test-data-factory';
 import { AuthService } from '@d8d/auth-module';
 import { UserService } from '@d8d/user-module';
 
 // 设置集成测试钩子
-setupIntegrationDatabaseHooksWithEntities([AreaEntity])
+setupIntegrationDatabaseHooksWithEntities([AreaEntity, UserEntity, File, Role])
 
 describe('管理地区API集成测试 (使用hono/testing)', () => {
   let client: ReturnType<typeof testClient<typeof adminAreaRoutes>>;
@@ -348,8 +350,9 @@ describe('管理地区API集成测试 (使用hono/testing)', () => {
 
         // 验证子树包含城市
         const subTree = responseData.data;
+        expect(subTree).toBeDefined();
         expect(subTree).toHaveProperty('children');
-        if (subTree.children) {
+        if (subTree && subTree.children) {
           const cityNames = subTree.children.map((city: any) => city.name);
           expect(cityNames).toContain('朝阳区');
           expect(cityNames).toContain('海淀区');

+ 1 - 0
packages/geo-areas/tests/utils/integration-test-utils.ts

@@ -1,5 +1,6 @@
 import { DataSource } from 'typeorm';
 import { AreaEntity } from '../../src/modules/areas/area.entity';
+import { expect } from 'vitest';
 
 export class IntegrationTestAssertions {
   /**

+ 33 - 0
packages/geo-areas/tests/utils/test-data-factory.ts

@@ -46,4 +46,37 @@ export class TestDataFactory {
     const area = areaRepository.create(areaData);
     return await areaRepository.save(area);
   }
+
+  /**
+   * 创建测试用户数据(用于认证测试)
+   */
+  static createUserData(overrides: any = {}): any {
+    const timestamp = Date.now();
+    return {
+      id: Math.floor(Math.random() * 10000) + 1,
+      username: `testuser_${timestamp}`,
+      password: 'TestPassword123!',
+      email: `test_${timestamp}@example.com`,
+      phone: `138${timestamp.toString().slice(-8)}`,
+      nickname: `Test User ${timestamp}`,
+      name: `Test Name ${timestamp}`,
+      isDisabled: 0,
+      isDeleted: 0,
+      ...overrides
+    };
+  }
+
+  /**
+   * 在数据库中创建测试用户(用于认证测试)
+   */
+  static async createTestUser(dataSource: DataSource, overrides: any = {}): Promise<any> {
+    const userData = this.createUserData(overrides);
+
+    // 导入 UserEntity
+    const { UserEntity } = await import('@d8d/user-module');
+    const userRepository = dataSource.getRepository(UserEntity);
+
+    const user = userRepository.create(userData);
+    return await userRepository.save(user);
+  }
 }

+ 6 - 0
pnpm-lock.yaml

@@ -338,6 +338,9 @@ importers:
       '@d8d/auth-module':
         specifier: workspace:*
         version: link:../auth-module
+      '@d8d/file-module':
+        specifier: workspace:*
+        version: link:../file-module
       '@d8d/shared-crud':
         specifier: workspace:*
         version: link:../shared-crud
@@ -347,6 +350,9 @@ importers:
       '@d8d/shared-utils':
         specifier: workspace:*
         version: link:../shared-utils
+      '@d8d/user-module':
+        specifier: workspace:*
+        version: link:../user-module
       '@hono/zod-openapi':
         specifier: 1.0.2
         version: 1.0.2(hono@4.8.5)(zod@4.1.12)