- 模型 type 默认值 all -> location,注释同步为 location/store/goods - 迁移 20261027010000:有商品明细的 all 任务归 goods(保留原 mode),其余归 location 且 mode=blind;列默认值与注释同步更新 - 补充盘点集成测试与分区编码唯一性集成测试(默认跳过,需 DISPATCH_FACTORY_TEST_MYSQL=1 才连接本地开发库) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
131 lines
4.8 KiB
JavaScript
131 lines
4.8 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 分区编码唯一性(真实库集成测试)。
|
||
*
|
||
* 覆盖:同仓库同 zone_code 只允许一条未归档分区(归档分区不占用编码)。
|
||
* 背景:uk_warehouse_zone_code 含 deleted_at,未删除行 deleted_at 为 NULL 不参与去重,
|
||
* 同 zone_code 并存会导致其下库位 space_code 重复(如 E02-06-01-01 多条),
|
||
* 盘点/库存按编码定位会命中历史行。
|
||
*
|
||
* 注意:本文件会启动 egg-mock app,需单独运行(与其它同样启动 app 的用例在同一 mocha 进程内
|
||
* 会触发 egg loader "can't overwrite property" 冲突,为仓库既有约束,如 return_received 系列)。
|
||
*/
|
||
|
||
const assert = require('node:assert/strict');
|
||
const mm = require('egg-mock');
|
||
|
||
const REAL_DB_ENABLED = process.env.DISPATCH_FACTORY_TEST_MYSQL === '1';
|
||
const runner = REAL_DB_ENABLED ? describe : describe.skip;
|
||
|
||
const TEST_WAREHOUSE_ID = 100000;
|
||
|
||
runner('test/app/service/systems/location_zone.zoneCodeUnique.test.js(分区编码唯一性)', function() {
|
||
this.timeout(120000);
|
||
|
||
let app;
|
||
let ctx;
|
||
const createdZoneIds = [];
|
||
|
||
before(async () => {
|
||
app = mm.app({
|
||
baseDir: process.cwd(),
|
||
env: 'local',
|
||
clean: false,
|
||
cache: false,
|
||
});
|
||
await app.ready();
|
||
ctx = app.createAnonymousContext();
|
||
ctx.user = { id: 4, store_id: TEST_WAREHOUSE_ID, employees_name: '阿猫' };
|
||
});
|
||
|
||
after(async () => {
|
||
for (const zoneId of createdZoneIds) {
|
||
await ctx.model.LocationZone.destroy({ where: { id: zoneId }, force: true });
|
||
}
|
||
await app.close();
|
||
});
|
||
|
||
afterEach(() => {
|
||
mm.restore();
|
||
});
|
||
|
||
/**
|
||
* 构造创建仓库类型分区的入参(warehouse 类型无需父元素,编码必填)。
|
||
*
|
||
* @param {string} zoneCode 分区编码(1-5 位大写字母/数字)。
|
||
* @return {Object} createZone 入参。
|
||
*/
|
||
function buildWarehouseZonePayload(zoneCode) {
|
||
return {
|
||
zone_type: 'warehouse',
|
||
zone_name: `测试仓_${zoneCode}`,
|
||
zone_code: zoneCode,
|
||
parent_id: 0,
|
||
x: 0,
|
||
y: 0,
|
||
z: 0,
|
||
length: 100,
|
||
width: 100,
|
||
height: 100,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成当前仓库未占用的随机编码(1-5 位大写字母/数字)。
|
||
*
|
||
* @return {string} 分区编码。
|
||
*/
|
||
function uniqueZoneCode() {
|
||
const suffix = String(Date.now()).slice(-4);
|
||
return `Z${suffix}`.slice(0, 5).toUpperCase();
|
||
}
|
||
|
||
it('同仓库同 zone_code 重复创建报错;归档后编码可复用', async () => {
|
||
const zoneCode = uniqueZoneCode();
|
||
|
||
const created = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(zoneCode));
|
||
assert.equal(created.success, true);
|
||
const firstZoneId = Number(created.data.id);
|
||
assert.ok(firstZoneId > 0, '前置:分区应创建成功');
|
||
createdZoneIds.push(firstZoneId);
|
||
|
||
// 同编码再次创建 → 拒绝
|
||
await assert.rejects(async () => {
|
||
await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(zoneCode));
|
||
}, error => /分区编码已存在/.test(error.message));
|
||
|
||
// 编码格式校验(沿用规则:1-5 位大写字母/数字)
|
||
await assert.rejects(async () => {
|
||
await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload('bad-code'));
|
||
}, error => /编码必须是1-5位大写字母或数字/.test(error.message));
|
||
|
||
// 归档(软删除)后同编码可再次创建:归档行不占用编码
|
||
await ctx.model.LocationZone.destroy({ where: { id: firstZoneId } });
|
||
const recreated = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(zoneCode));
|
||
assert.equal(recreated.success, true);
|
||
assert.notEqual(Number(recreated.data.id), firstZoneId, '归档后新建应为新关系');
|
||
createdZoneIds.push(Number(recreated.data.id));
|
||
});
|
||
|
||
it('更新分区编码:改为已占用编码报错,改回自身编码不报错', async () => {
|
||
const codeA = uniqueZoneCode();
|
||
const zoneA = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(codeA));
|
||
createdZoneIds.push(Number(zoneA.data.id));
|
||
|
||
// 另建一个分区占位
|
||
const codeB = `Y${String(Date.now()).slice(-4)}`.slice(0, 5).toUpperCase();
|
||
const zoneB = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(codeB));
|
||
createdZoneIds.push(Number(zoneB.data.id));
|
||
|
||
// 把 A 改成 B 的编码 → 报错
|
||
await assert.rejects(async () => {
|
||
await ctx.service.systems.locationZone.updateZone(Number(zoneA.data.id), { zone_code: codeB });
|
||
}, error => /分区编码已存在/.test(error.message));
|
||
|
||
// 改回自身编码 → 正常
|
||
const updated = await ctx.service.systems.locationZone.updateZone(Number(zoneA.data.id), { zone_code: codeA });
|
||
assert.ok(updated, '自身编码不应被判为冲突');
|
||
});
|
||
});
|