| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078 |
- import { describe, it, expect, beforeEach } from 'vitest';
- import { testClient } from 'hono/testing';
- import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
- import { JWTUtil } from '@d8d/shared-utils';
- import { UserEntityMt, RoleMt } from '@d8d/user-module-mt';
- import { FileMt } from '@d8d/file-module-mt';
- import { SupplierMt } from '@d8d/supplier-module-mt';
- import { MerchantMt } from '@d8d/merchant-module-mt';
- import { adminGoodsRoutesMt } from '../../src/routes/index.mt';
- import { GoodsMt, GoodsCategoryMt } from '../../src/entities/index.mt';
- import { GoodsTestFactory } from '../factories/goods-test-factory';
- // 设置集成测试钩子
- setupIntegrationDatabaseHooksWithEntities([
- UserEntityMt, RoleMt, GoodsMt, GoodsCategoryMt, FileMt, SupplierMt, MerchantMt
- ])
- describe('管理员商品管理API集成测试', () => {
- let client: ReturnType<typeof testClient<typeof adminGoodsRoutesMt>>;
- let adminToken: string;
- let testUser: UserEntityMt;
- let testAdmin: UserEntityMt;
- let testCategory: GoodsCategoryMt;
- let testSupplier: SupplierMt;
- let testMerchant: MerchantMt;
- let testFactory: GoodsTestFactory;
- beforeEach(async () => {
- // 创建测试客户端
- client = testClient(adminGoodsRoutesMt);
- // 获取数据源并创建测试工厂
- const dataSource = await IntegrationTestDatabase.getDataSource();
- testFactory = new GoodsTestFactory(dataSource);
- // 使用测试工厂创建测试数据
- testUser = await testFactory.createTestUser();
- testAdmin = await testFactory.createTestAdmin();
- testCategory = await testFactory.createTestCategory(testUser.id);
- testSupplier = await testFactory.createTestSupplier(testUser.id);
- testMerchant = await testFactory.createTestMerchant(testUser.id);
- // 生成测试管理员的token
- adminToken = JWTUtil.generateToken({
- id: testAdmin.id,
- username: testAdmin.username,
- roles: [{name:'admin'}]
- });
- });
- describe('GET /goods', () => {
- it('应该返回所有商品列表', async () => {
- // 创建多个用户的商品
- const userGoods1 = await testFactory.createTestGoods(testUser.id, {
- name: '用户商品1',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 100
- });
- const userGoods2 = await testFactory.createTestGoods(testAdmin.id, {
- name: '用户商品2',
- price: 200.00,
- costPrice: 160.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 50
- });
- const response = await client.index.$get({
- query: {}
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('管理员商品列表响应状态:', response.status);
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data).toHaveProperty('data');
- expect(Array.isArray(data.data)).toBe(true);
- // 验证返回所有用户的商品(管理员可以访问所有数据)
- const userGoodsCount = data.data.filter((goods: any) => goods.createdBy === testUser.id).length;
- const adminGoodsCount = data.data.filter((goods: any) => goods.createdBy === testAdmin.id).length;
- expect(userGoodsCount).toBeGreaterThan(0);
- expect(adminGoodsCount).toBeGreaterThan(0);
- }
- });
- it('应该拒绝未认证用户的访问', async () => {
- const response = await client.index.$get({
- query: {}
- });
- expect(response.status).toBe(401);
- });
- });
- describe('POST /goods', () => {
- it('应该成功创建商品并可以指定权限', async () => {
- const createData = {
- name: '管理员创建商品',
- price: 150.00,
- costPrice: 120.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 80,
- lowestBuy: 1,
- createdBy: testUser.id // 管理员可以指定创建人
- };
- const response = await client.index.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('管理员创建商品响应状态:', response.status);
- if (response.status !== 201) {
- const errorData = await response.json();
- console.debug('管理员创建商品错误响应:', errorData);
- }
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const data = await response.json();
- expect(data).toHaveProperty('id');
- expect(data.name).toBe(createData.name);
- expect(data.price).toBe(Number(createData.price));
- expect(data.createdBy).toBe(testUser.id); // 验证可以指定创建人
- }
- });
- it('应该验证创建商品的必填字段', async () => {
- const invalidData = {
- // 缺少必填字段
- name: '',
- price: -1,
- categoryId1: -1
- };
- const response = await client.index.$post({
- json: invalidData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(400);
- });
- });
- describe('GET /goods/:id', () => {
- it('应该返回指定商品的详情', async () => {
- // 先创建一个商品
- const testGoods = await testFactory.createTestGoods(testUser.id, {
- name: '测试商品详情',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 100
- });
- const response = await client[':id'].$get({
- param: { id: testGoods.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('管理员商品详情响应状态:', response.status);
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.id).toBe(testGoods.id);
- expect(data.name).toBe(testGoods.name);
- expect(data.createdBy).toBe(testUser.id);
- }
- });
- it('应该处理不存在的商品', async () => {
- const response = await client[':id'].$get({
- param: { id: 999999 }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(404);
- });
- });
- describe('PUT /goods/:id', () => {
- it('应该成功更新任何商品', async () => {
- // 先创建一个商品
- const testGoods = await testFactory.createTestGoods(testUser.id, {
- name: '测试更新商品',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 100
- });
- const updateData = {
- name: '管理员更新后的商品名称',
- price: 120.00,
- state: 2,
- updatedBy: testAdmin.id // 管理员可以指定更新人
- };
- const response = await client[':id'].$put({
- param: { id: testGoods.id },
- json: updateData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('管理员更新商品响应状态:', response.status);
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.name).toBe(updateData.name);
- expect(data.price).toBe(Number(updateData.price));
- expect(data.state).toBe(updateData.state);
- expect(data.updatedBy).toBe(testAdmin.id); // 验证可以指定更新人
- }
- });
- });
- describe('DELETE /goods/:id', () => {
- it('应该成功删除任何商品', async () => {
- // 先创建一个商品
- const testGoods = await testFactory.createTestGoods(testUser.id, {
- name: '测试删除商品',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 100
- });
- const response = await client[':id'].$delete({
- param: { id: testGoods.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('管理员删除商品响应状态:', response.status);
- expect(response.status).toBe(204);
- });
- });
- describe('商品状态管理测试', () => {
- it('应该正确处理商品状态变更', async () => {
- // 创建可用状态的商品
- const activeGoods = await testFactory.createTestGoods(testUser.id, {
- name: '可用商品',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 100
- });
- // 创建不可用状态的商品
- const inactiveGoods = await testFactory.createTestGoods(testUser.id, {
- name: '不可用商品',
- price: 200.00,
- costPrice: 160.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 2,
- stock: 50
- });
- // 验证状态过滤
- const response = await client.index.$get({
- query: { filters: JSON.stringify({ state: 1 }) }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- // 类型检查确保data属性存在
- if ('data' in data && Array.isArray(data.data)) {
- // 应该只返回可用状态的商品
- const activeGoodsInResponse = data.data.filter((goods: any) => goods.state === 1);
- const inactiveGoodsInResponse = data.data.filter((goods: any) => goods.state === 2);
- expect(activeGoodsInResponse.length).toBeGreaterThan(0);
- expect(inactiveGoodsInResponse.length).toBe(0);
- } else {
- // 如果响应是错误格式,应该失败
- expect(data).toHaveProperty('data');
- }
- });
- });
- describe('商品库存管理测试', () => {
- it('应该正确处理商品库存更新', async () => {
- // 创建一个商品
- const testGoods = await testFactory.createTestGoods(testUser.id, {
- name: '库存测试商品',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 100
- });
- // 更新库存
- const updateData = {
- stock: 50
- };
- const response = await client[':id'].$put({
- param: { id: testGoods.id },
- json: updateData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.stock).toBe(Number(updateData.stock));
- }
- });
- });
- describe('管理员权限验证测试', () => {
- it('应该验证管理员可以访问所有数据', async () => {
- // 创建多个用户的商品
- const userGoods = await testFactory.createTestGoods(testUser.id, {
- name: '用户商品',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 100
- });
- const adminGoods = await testFactory.createTestGoods(testAdmin.id, {
- name: '管理员商品',
- price: 200.00,
- costPrice: 160.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- stock: 50
- });
- // 使用管理员token获取列表
- const response = await client.index.$get({
- query: {}
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- // 类型检查确保data属性存在
- if ('data' in data && Array.isArray(data.data)) {
- // 验证返回所有用户的商品
- const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id);
- const adminGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testAdmin.id);
- expect(userGoodsInResponse.length).toBeGreaterThan(0);
- expect(adminGoodsInResponse.length).toBeGreaterThan(0);
- } else {
- // 如果响应是错误格式,应该失败
- expect(data).toHaveProperty('data');
- }
- });
- });
- describe('父子商品配置功能测试 (故事006.001)', () => {
- it('应该成功创建父商品 (spuId=0)', async () => {
- const createData = {
- name: '父商品测试',
- price: 200.00,
- costPrice: 150.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 100,
- lowestBuy: 1,
- spuId: 0, // 父商品spuId=0
- spuName: null // 父商品spuName为null
- };
- const response = await client.index.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('创建父商品响应状态:', response.status);
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const data = await response.json();
- expect(data).toHaveProperty('id');
- expect(data.name).toBe(createData.name);
- expect(data.spuId).toBe(0); // 验证spuId=0
- expect(data.spuName).toBeUndefined(); // 验证spuName不再返回(已从API响应中移除)
- }
- });
- it('应该成功创建子商品并关联父商品', async () => {
- // 先创建父商品
- const parentGoods = await testFactory.createTestGoods(testUser.id, {
- name: '父商品-用于子商品测试',
- price: 300.00,
- spuId: 0,
- spuName: null
- });
- const createData = {
- name: '子商品测试',
- price: 150.00,
- costPrice: 120.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 50,
- lowestBuy: 1,
- spuId: parentGoods.id, // 子商品spuId=父商品ID
- spuName: parentGoods.name // 子商品spuName=父商品名称
- };
- const response = await client.index.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('创建子商品响应状态:', response.status);
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const data = await response.json();
- expect(data).toHaveProperty('id');
- expect(data.name).toBe(createData.name);
- expect(data.spuId).toBe(parentGoods.id); // 验证spuId=父商品ID
- // spuName字段已从API响应中移除,改为通过parent对象获取父商品名称
- // expect(data.spuName).toBe(parentGoods.name); // 验证spuName=父商品名称
- }
- });
- it('应该成功更新商品的父子关系', async () => {
- // 创建父商品
- const parentGoods = await testFactory.createTestGoods(testUser.id, {
- name: '父商品-用于更新测试',
- price: 400.00,
- spuId: 0,
- spuName: null
- });
- // 创建子商品
- const childGoods = await testFactory.createTestGoods(testUser.id, {
- name: '子商品-用于更新测试',
- price: 200.00,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- });
- // 更新子商品信息
- const updateData = {
- name: '更新后的子商品名称',
- price: 250.00,
- spuId: parentGoods.id, // 保持父子关系
- spuName: '更新后的父商品名称' // 更新spuName
- };
- const response = await client[':id'].$put({
- param: { id: childGoods.id },
- json: updateData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('更新子商品响应状态:', response.status);
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.name).toBe(updateData.name);
- expect(data.price).toBe(Number(updateData.price));
- expect(data.spuId).toBe(parentGoods.id); // 验证父子关系保持
- // spuName字段已从API响应中移除,改为通过parent对象获取父商品名称
- // expect(data.spuName).toBe(updateData.spuName); // 验证spuName更新
- }
- });
- it('应该成功获取商品详情并包含父子关系字段', async () => {
- // 创建父商品
- const parentGoods = await testFactory.createTestGoods(testUser.id, {
- name: '父商品-用于详情测试',
- price: 500.00,
- spuId: 0,
- spuName: null
- });
- // 获取父商品详情
- const response = await client[':id'].$get({
- param: { id: parentGoods.id }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('获取父商品详情响应状态:', response.status);
- expect(response.status).toBe(200);
- if (response.status === 200) {
- const data = await response.json();
- expect(data.id).toBe(parentGoods.id);
- expect(data.name).toBe(parentGoods.name);
- expect(data.spuId).toBe(0); // 验证父商品spuId=0
- expect(data.spuName).toBeUndefined(); // 验证父商品spuName不再返回(已从API响应中移除)
- }
- });
- it('应该验证父子商品关系约束', async () => {
- // 测试1: spuId必须是非负数 - Schema验证
- const invalidData1 = {
- name: '测试商品-无效spuId',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 100,
- lowestBuy: 1,
- spuId: -1 // 无效的spuId,Schema应该验证失败
- };
- const response1 = await client.index.$post({
- json: invalidData1
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('无效spuId测试响应状态:', response1.status);
- expect(response1.status).toBe(400);
- // 测试2: 创建商品时spuId不能指向不存在的商品
- const invalidData2 = {
- name: '测试商品-无效父商品',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 100,
- lowestBuy: 1,
- spuId: 999999 // 不存在的商品ID
- };
- const response2 = await client.index.$post({
- json: invalidData2
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('无效父商品测试响应状态:', response2.status);
- // 可能返回201(创建成功)或400(验证失败),取决于业务逻辑
- // 这里我们只记录状态,不进行断言
- console.debug('创建指向不存在父商品的商品状态:', response2.status);
- });
- it('应该支持批量创建父子商品', async () => {
- // 创建父商品
- const parentGoods = await testFactory.createTestGoods(testUser.id, {
- name: '父商品-批量测试',
- price: 600.00,
- spuId: 0,
- spuName: null
- });
- // 批量创建3个子商品
- const childGoodsData = [
- {
- name: '子商品-规格1',
- price: 300.00,
- costPrice: 240.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 30,
- lowestBuy: 1,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- },
- {
- name: '子商品-规格2',
- price: 320.00,
- costPrice: 260.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 40,
- lowestBuy: 1,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- },
- {
- name: '子商品-规格3',
- price: 350.00,
- costPrice: 280.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 50,
- lowestBuy: 1,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- }
- ];
- // 逐个创建子商品
- const createdChildIds = [];
- for (const childData of childGoodsData) {
- const response = await client.index.$post({
- json: childData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(201);
- const data = await response.json();
- createdChildIds.push(data.id);
- // 验证每个子商品都正确关联到父商品
- expect(data.spuId).toBe(parentGoods.id);
- // spuName字段已从API响应中移除,改为通过parent对象获取父商品名称
- // expect(data.spuName).toBe(parentGoods.name);
- }
- console.debug(`批量创建了 ${createdChildIds.length} 个子商品`);
- expect(createdChildIds).toHaveLength(3);
- });
- it('应该验证子商品继承父商品的分类信息', async () => {
- // 创建父商品,设置特定的分类信息
- const parentGoods = await testFactory.createTestGoods(testUser.id, {
- name: '父商品-分类继承测试',
- price: 700.00,
- spuId: 0,
- spuName: null,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 2, // 虚拟产品
- supplierId: testSupplier.id,
- merchantId: testMerchant.id
- });
- // 创建子商品,从父商品继承分类信息
- const createData = {
- name: '子商品-分类继承测试',
- price: 350.00,
- costPrice: 280.00,
- // 从父商品继承分类ID
- categoryId1: parentGoods.categoryId1,
- categoryId2: parentGoods.categoryId2,
- categoryId3: parentGoods.categoryId3,
- goodsType: 1, // 可以覆盖父商品的商品类型
- supplierId: null, // 可以设置为null
- merchantId: null, // 可以设置为null
- state: 1,
- stock: 60,
- lowestBuy: 1,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- };
- const response = await client.index.$post({
- json: createData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('分类继承测试响应状态:', response.status);
- if (response.status !== 201) {
- const errorData = await response.json();
- console.debug('分类继承测试错误响应:', errorData);
- }
- expect(response.status).toBe(201);
- if (response.status === 201) {
- const data = await response.json();
- expect(data.name).toBe(createData.name);
- expect(data.spuId).toBe(parentGoods.id);
- // spuName字段已从API响应中移除,改为通过parent对象获取父商品名称
- // expect(data.spuName).toBe(parentGoods.name);
- // 验证子商品使用了父商品的分类信息
- expect(data.categoryId1).toBe(parentGoods.categoryId1);
- expect(data.categoryId2).toBe(parentGoods.categoryId2);
- expect(data.categoryId3).toBe(parentGoods.categoryId3);
- // 验证商品类型可以被覆盖
- expect(data.goodsType).toBe(createData.goodsType); // 子商品使用自己的商品类型
- expect(data.supplierId).toBeNull(); // 子商品可以设置为null
- expect(data.merchantId).toBeNull(); // 子商品可以设置为null
- }
- });
- it('应该验证创建子商品时必须提供有效的分类ID', async () => {
- // 创建父商品
- const parentGoods = await testFactory.createTestGoods(testUser.id, {
- name: '父商品-分类验证测试',
- price: 800.00,
- spuId: 0,
- spuName: null,
- categoryId1: testCategory.id, // 父商品有有效的分类ID
- categoryId2: testCategory.id,
- categoryId3: testCategory.id
- });
- // 测试1: 创建子商品时不指定分类ID(应该使用默认值0,但会导致外键约束错误)
- const invalidData1 = {
- name: '子商品-无效分类测试',
- price: 400.00,
- costPrice: 320.00,
- // 不指定categoryId1/2/3,默认值为0
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 70,
- lowestBuy: 1,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- };
- const response1 = await client.index.$post({
- json: invalidData1
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('无效分类测试响应状态:', response1.status);
- // 由于外键约束,可能会返回500错误
- // 这里我们只记录状态,不进行断言,因为行为取决于业务逻辑
- console.debug('创建子商品不指定分类ID的状态:', response1.status);
- // 测试2: 创建子商品时指定有效的分类ID(应该成功)
- const validData = {
- name: '子商品-有效分类测试',
- price: 420.00,
- costPrice: 340.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- goodsType: 1,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- stock: 80,
- lowestBuy: 1,
- spuId: parentGoods.id,
- spuName: parentGoods.name
- };
- const response2 = await client.index.$post({
- json: validData
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- console.debug('有效分类测试响应状态:', response2.status);
- expect(response2.status).toBe(201);
- if (response2.status === 201) {
- const data = await response2.json();
- expect(data.categoryId1).toBe(testCategory.id);
- expect(data.categoryId2).toBe(testCategory.id);
- expect(data.categoryId3).toBe(testCategory.id);
- }
- });
- });
- describe('spuId过滤功能测试 (故事006.004)', () => {
- let parentGoods1: GoodsMt;
- let parentGoods2: GoodsMt;
- let childGoods1: GoodsMt;
- let childGoods2: GoodsMt;
- beforeEach(async () => {
- // 创建测试数据:2个父商品,每个父商品有1个子商品
- parentGoods1 = await testFactory.createTestGoods(testUser.id, {
- name: '父商品1',
- price: 100.00,
- costPrice: 80.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- spuId: 0,
- spuName: null
- });
- parentGoods2 = await testFactory.createTestGoods(testUser.id, {
- name: '父商品2',
- price: 200.00,
- costPrice: 160.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- spuId: 0,
- spuName: null
- });
- childGoods1 = await testFactory.createTestGoods(testUser.id, {
- name: '子商品1 - 红色',
- price: 110.00,
- costPrice: 85.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- spuId: parentGoods1.id,
- spuName: parentGoods1.name
- });
- childGoods2 = await testFactory.createTestGoods(testUser.id, {
- name: '子商品2 - 蓝色',
- price: 220.00,
- costPrice: 165.00,
- categoryId1: testCategory.id,
- categoryId2: testCategory.id,
- categoryId3: testCategory.id,
- supplierId: testSupplier.id,
- merchantId: testMerchant.id,
- state: 1,
- spuId: parentGoods2.id,
- spuName: parentGoods2.name
- });
- });
- it('应该支持通过filters参数过滤只显示父商品 (spuId=0)', async () => {
- const response = await client.index.$get({
- query: {
- page: 1,
- pageSize: 10,
- filters: JSON.stringify({ spuId: 0 })
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- // 应该只返回父商品
- expect(data.pagination.total).toBe(2);
- expect(data.data).toHaveLength(2);
- // 验证返回的是父商品
- const returnedIds = data.data.map((item: any) => item.id);
- expect(returnedIds).toContain(parentGoods1.id);
- expect(returnedIds).toContain(parentGoods2.id);
- expect(returnedIds).not.toContain(childGoods1.id);
- expect(returnedIds).not.toContain(childGoods2.id);
- // 验证所有返回商品的spuId为0
- data.data.forEach((item: any) => {
- expect(item.spuId).toBe(0);
- });
- });
- it('应该支持通过filters参数过滤显示指定父商品的子商品 (spuId>0)', async () => {
- const response = await client.index.$get({
- query: {
- page: 1,
- pageSize: 10,
- filters: JSON.stringify({ spuId: parentGoods1.id })
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- // 应该只返回parentGoods1的子商品
- expect(data.pagination.total).toBe(1);
- expect(data.data).toHaveLength(1);
- // 验证返回的是childGoods1
- expect(data.data[0].id).toBe(childGoods1.id);
- expect(data.data[0].spuId).toBe(parentGoods1.id);
- // spuName字段已从API响应中移除,改为通过parent对象获取父商品名称
- // expect(data.data[0].spuName).toBe(parentGoods1.name);
- });
- it('应该支持通过filters参数组合过滤', async () => {
- const response = await client.index.$get({
- query: {
- page: 1,
- pageSize: 10,
- filters: JSON.stringify({
- spuId: 0,
- state: 1
- })
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- // 应该只返回可用状态的父商品
- expect(data.pagination.total).toBe(2);
- data.data.forEach((item: any) => {
- expect(item.spuId).toBe(0);
- expect(item.state).toBe(1);
- });
- });
- it('管理员商品列表应该默认显示所有商品(无spuId过滤)', async () => {
- const response = await client.index.$get({
- query: {
- page: 1,
- pageSize: 10
- }
- }, {
- headers: {
- 'Authorization': `Bearer ${adminToken}`
- }
- });
- expect(response.status).toBe(200);
- const data = await response.json();
- // 应该返回所有4个商品
- expect(data.pagination.total).toBe(4);
- expect(data.data).toHaveLength(4);
- // 验证包含所有商品
- const returnedIds = data.data.map((item: any) => item.id);
- expect(returnedIds).toContain(parentGoods1.id);
- expect(returnedIds).toContain(parentGoods2.id);
- expect(returnedIds).toContain(childGoods1.id);
- expect(returnedIds).toContain(childGoods2.id);
- });
- });
- });
|