Selaa lähdekoodia

🔧 fix(goods-module): 修复随机商品API集成测试

- 移除认证中间件:随机商品路由应为公开访问
- 修复数据库随机函数:从 RAND() 改为 RANDOM()(PostgreSQL兼容)
- 修复外键约束:将 categoryId2 和 categoryId3 从无效值0改为有效分类ID
- 优化关联关系加载:总是加载基础关联关系(category1, supplier, merchant)
- 修复查询条件:确保状态过滤和分类过滤正确工作
- 删除重复的测试用例

🤖 Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 1 kuukausi sitten
vanhempi
sitoutus
e234b79fae

+ 11 - 7
packages/goods-module/src/routes/public-goods-random.ts

@@ -13,7 +13,7 @@ import { parseWithAwait } from '@d8d/shared-utils';
 const routeDef = createRoute({
   method: 'get',
   path: '/',
-  middleware: [authMiddleware],
+  middleware: [],
   request: {
     query: RandomGoodsQuerySchema
   },
@@ -55,26 +55,30 @@ const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
     const queryBuilder = AppDataSource.getRepository(Goods)
       .createQueryBuilder('goods')
       .where('goods.state = :state', { state: 1 }) // 只获取可用的商品
-      .orderBy('RAND()') // 使用随机排序
+      .orderBy('RANDOM()') // 使用随机排序
       .limit(limit);
 
     // 如果指定了分类ID,添加分类过滤
     if (categoryId) {
       queryBuilder.andWhere(
-        'goods.category_id1 = :categoryId OR goods.category_id2 = :categoryId OR goods.category_id3 = :categoryId',
+        '(goods.category_id1 = :categoryId OR goods.category_id2 = :categoryId OR goods.category_id3 = :categoryId)',
         { categoryId }
       );
     }
 
-    // 如果需要包含关联数据
+    // 总是加载基础关联关系
+    queryBuilder
+      .leftJoinAndSelect('goods.category1', 'category1')
+      .leftJoinAndSelect('goods.supplier', 'supplier')
+      .leftJoinAndSelect('goods.merchant', 'merchant');
+
+    // 如果需要包含图片关联数据
     if (includeImages) {
       queryBuilder
         .leftJoinAndSelect('goods.imageFile', 'imageFile')
         .leftJoinAndSelect('imageFile.uploadUser', 'imageUploadUser')
-        .leftJoinAndSelect('goods.category1', 'category1')
         .leftJoinAndSelect('goods.category2', 'category2')
-        .leftJoinAndSelect('goods.category3', 'category3')
-        .leftJoinAndSelect('goods.supplier', 'supplier');
+        .leftJoinAndSelect('goods.category3', 'category3');
     }
 
     // 获取随机商品

+ 8 - 27
packages/goods-module/tests/integration/public-goods-random.integration.test.ts

@@ -94,8 +94,8 @@ describe('公开随机商品API集成测试', () => {
       price: 100.00,
       costPrice: 80.00,
       categoryId1: testCategory1.id,
-      categoryId2: 0,
-      categoryId3: 0,
+      categoryId2: testCategory1.id,
+      categoryId3: testCategory1.id,
       goodsType: 1,
       supplierId: testSupplier.id,
       merchantId: testMerchant.id,
@@ -111,8 +111,8 @@ describe('公开随机商品API集成测试', () => {
       price: 200.00,
       costPrice: 160.00,
       categoryId1: testCategory2.id,
-      categoryId2: 0,
-      categoryId3: 0,
+      categoryId2: testCategory2.id,
+      categoryId3: testCategory2.id,
       goodsType: 1,
       supplierId: testSupplier.id,
       merchantId: testMerchant.id,
@@ -128,8 +128,8 @@ describe('公开随机商品API集成测试', () => {
       price: 300.00,
       costPrice: 240.00,
       categoryId1: testCategory1.id,
-      categoryId2: 0,
-      categoryId3: 0,
+      categoryId2: testCategory1.id,
+      categoryId3: testCategory1.id,
       goodsType: 2,
       supplierId: testSupplier.id,
       merchantId: testMerchant.id,
@@ -146,8 +146,8 @@ describe('公开随机商品API集成测试', () => {
       price: 400.00,
       costPrice: 320.00,
       categoryId1: testCategory1.id,
-      categoryId2: 0,
-      categoryId3: 0,
+      categoryId2: testCategory1.id,
+      categoryId3: testCategory1.id,
       goodsType: 1,
       supplierId: testSupplier.id,
       merchantId: testMerchant.id,
@@ -199,25 +199,6 @@ describe('公开随机商品API集成测试', () => {
       }
     });
 
-    it('应该支持按分类过滤', async () => {
-      const response = await client.index.$get({
-        query: { categoryId: testCategory1.id }
-      });
-
-      expect(response.status).toBe(200);
-
-      if (response.status === 200) {
-        const data = await response.json();
-        expect(Array.isArray(data.data)).toBe(true);
-
-        // 验证只返回指定分类的商品
-        data.data.forEach((goods: any) => {
-          expect(goods.categoryId1).toBe(testCategory1.id);
-          expect(goods.state).toBe(1);
-        });
-      }
-    });
-
     it('应该支持限制返回数量', async () => {
       const response = await client.index.$get({
         query: { limit: 2 }