321 lines
8.8 KiB
JavaScript
321 lines
8.8 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const StockStoreService = require('../../../../app/service/systems/stock_store');
|
|
|
|
function createTestContext(options = {}) {
|
|
const calls = {
|
|
countArgs: null,
|
|
findArgs: null,
|
|
barcodeCalls: [],
|
|
fileRefArgs: null,
|
|
};
|
|
|
|
const Op = {
|
|
and: '$and',
|
|
or: '$or',
|
|
ne: '$ne',
|
|
like: '$like',
|
|
gt: '$gt',
|
|
lt: '$lt',
|
|
gte: '$gte',
|
|
lte: '$lte',
|
|
in: '$in',
|
|
notIn: '$notIn',
|
|
between: '$between',
|
|
notBetween: '$notBetween',
|
|
is: '$is',
|
|
not: '$not',
|
|
regexp: '$regexp',
|
|
notRegexp: '$notRegexp',
|
|
};
|
|
|
|
const rows = options.rows || [ {
|
|
id: 1,
|
|
item_code: 'SKU-1',
|
|
goods_code: 'G-1',
|
|
item_name: '商品A',
|
|
quantity: 10,
|
|
locked_quantity: 2,
|
|
create_at: '2026-01-01 10:00:00',
|
|
update_at: '2026-01-01 10:00:00',
|
|
} ];
|
|
|
|
const model = {
|
|
StoreWarehouse: {
|
|
buildStoreStockInclude(params = {}) {
|
|
return {
|
|
as: 'store_stock',
|
|
required: Boolean(params.required),
|
|
attributes: params.attributes || [],
|
|
where: {
|
|
warehouse_id: params.warehouseId,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
ProhibitedType: {},
|
|
StockWarehouse: {
|
|
async count(args) {
|
|
calls.countArgs = args;
|
|
return options.total == null ? rows.length : options.total;
|
|
},
|
|
async findAll(args) {
|
|
calls.findArgs = args;
|
|
return rows;
|
|
},
|
|
},
|
|
StockBarcode: {
|
|
async findAll(args) {
|
|
calls.barcodeCalls.push(args);
|
|
const attrs = Array.isArray(args.attributes) ? args.attributes : [];
|
|
if (attrs.includes('stock_id')) {
|
|
if (options.matchedBarcodeRows) {
|
|
return options.matchedBarcodeRows;
|
|
}
|
|
return [ { stock_id: 1 } ];
|
|
}
|
|
if (options.barcodes) {
|
|
return options.barcodes;
|
|
}
|
|
return [ { stock_id: 1, barcode: 'BC-1' } ];
|
|
},
|
|
},
|
|
FileReference: {
|
|
async findAll(args) {
|
|
calls.fileRefArgs = args;
|
|
return options.images || [];
|
|
},
|
|
},
|
|
};
|
|
|
|
const app = {
|
|
Sequelize: { Op },
|
|
model,
|
|
logger: {
|
|
debug() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
config: {
|
|
cos: {
|
|
cdnurl: '',
|
|
},
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app,
|
|
model,
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
logger: {
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
return {
|
|
app,
|
|
ctx,
|
|
model,
|
|
calls,
|
|
Op,
|
|
};
|
|
}
|
|
|
|
function createServiceHarness(options = {}) {
|
|
const harness = createTestContext(options);
|
|
const service = new StockStoreService(harness.ctx);
|
|
return {
|
|
...harness,
|
|
service,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/stock_store.stockWarehouse.conditions.test.js', () => {
|
|
it('should build Sequelize condition for all supported operators', () => {
|
|
const { service, app, Op } = createServiceHarness();
|
|
|
|
const cases = [
|
|
[ 'eq', { field: 'item_code', value: 'SKU' }, { item_code: 'SKU' } ],
|
|
[ 'ne', { value: 'SKU' }, { item_code: { [Op.ne]: 'SKU' } } ],
|
|
[ 'like', { value: 'SKU' }, { item_code: { [Op.like]: '%SKU%' } } ],
|
|
[ 'contains', { value: 'SKU' }, { item_code: { [Op.like]: '%SKU%' } } ],
|
|
[ 'startswith', { value: 'SKU' }, { item_code: { [Op.like]: 'SKU%' } } ],
|
|
[ 'endswith', { value: 'SKU' }, { item_code: { [Op.like]: '%SKU' } } ],
|
|
[ 'gt', { value: 10 }, { item_code: { [Op.gt]: 10 } } ],
|
|
[ 'lt', { value: 10 }, { item_code: { [Op.lt]: 10 } } ],
|
|
[ 'ge', { value: 10 }, { item_code: { [Op.gte]: 10 } } ],
|
|
[ 'le', { value: 10 }, { item_code: { [Op.lte]: 10 } } ],
|
|
[ 'in', { value: [ 'A', 'B' ] }, { item_code: { [Op.in]: [ 'A', 'B' ] } } ],
|
|
[ 'notin', { value: [ 'A', 'B' ] }, { item_code: { [Op.notIn]: [ 'A', 'B' ] } } ],
|
|
[ 'between', { value: [ 1, 2 ] }, { item_code: { [Op.between]: [ 1, 2 ] } } ],
|
|
[ 'notbetween', { value: [ 1, 2 ] }, { item_code: { [Op.notBetween]: [ 1, 2 ] } } ],
|
|
[ 'isnull', { value: null }, { item_code: { [Op.is]: null } } ],
|
|
[ 'isnotnull', { value: null }, { item_code: { [Op.not]: null } } ],
|
|
[ 'regexp', { value: '^SKU' }, { item_code: { [Op.regexp]: '^SKU' } } ],
|
|
[ 'notregexp', { value: '^SKU' }, { item_code: { [Op.notRegexp]: '^SKU' } } ],
|
|
];
|
|
|
|
for (const [ operator, input, expected ] of cases) {
|
|
const actual = service.buildSequelizeCondition('item_code', {
|
|
operator,
|
|
...input,
|
|
}, app);
|
|
assert.deepEqual(actual, expected, `operator=${operator}`);
|
|
}
|
|
});
|
|
|
|
it('should normalize boolean, number and text values', () => {
|
|
const { service } = createServiceHarness();
|
|
|
|
assert.equal(service.processConditionValue(true, 'boolean'), 'Y');
|
|
assert.equal(service.processConditionValue('false', 'boolean'), 'N');
|
|
assert.equal(service.processConditionValue('12.5', 'number'), 12.5);
|
|
assert.deepEqual(service.processConditionValue('A,B,C', 'text'), [ 'A', 'B', 'C' ]);
|
|
});
|
|
|
|
it('should support date and daterange condition extraction', () => {
|
|
const { service, app, Op } = createServiceHarness();
|
|
|
|
const dateEq = service.buildSequelizeCondition('create_at', {
|
|
operator: 'eq',
|
|
type: 'date',
|
|
value: '2026-03-25',
|
|
}, app);
|
|
assert.ok(dateEq.create_at);
|
|
|
|
const between = service.buildSequelizeCondition('create_at', {
|
|
operator: 'between',
|
|
type: 'daterange',
|
|
value: [ '2026-03-01', '2026-03-31' ],
|
|
}, app);
|
|
|
|
assert.deepEqual(between, {
|
|
create_at: {
|
|
[Op.between]: [ '2026-03-01 00:00:00', '2026-03-31 23:59:59' ],
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should parse where and return list with barcode/prohibited branches', async () => {
|
|
const { service, calls, Op } = createServiceHarness();
|
|
|
|
const result = await service.getWarehouseStock({
|
|
item_code: {
|
|
operator: 'like',
|
|
value: 'SKU',
|
|
},
|
|
barcode: {
|
|
operator: 'like',
|
|
value: 'BC',
|
|
},
|
|
prohibited_type: {
|
|
operator: 'eq',
|
|
value: 2,
|
|
},
|
|
quantity: {
|
|
operator: 'ge',
|
|
type: 'number',
|
|
value: '2',
|
|
},
|
|
}, {
|
|
page: 1,
|
|
pageSize: 10,
|
|
}, {
|
|
sortField: 'quantity',
|
|
sortOrder: 'ASC',
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.equal(result.message, '查询成功');
|
|
assert.equal(result.data.pagination.page, 1);
|
|
assert.equal(result.data.pagination.pageSize, 10);
|
|
assert.equal(result.data.queryInfo.hasBarcodeSearch, true);
|
|
|
|
assert.ok(calls.countArgs);
|
|
assert.ok(calls.findArgs);
|
|
assert.ok(Array.isArray(calls.barcodeCalls));
|
|
assert.ok(calls.barcodeCalls.length >= 2, '应包含条码匹配查询和条码回填查询');
|
|
|
|
const stockWhere = calls.findArgs.where;
|
|
assert.ok(stockWhere[Op.and], '应存在 AND 组合条件');
|
|
|
|
const includeList = calls.findArgs.include;
|
|
const prohibitedInclude = includeList.find(item => item.as === 'prohibited');
|
|
assert.ok(prohibitedInclude, '应包含 prohibited 关联');
|
|
assert.equal(prohibitedInclude.required, true, '含 prohibited_type 时应为必需关联');
|
|
});
|
|
|
|
it('should short-circuit when barcode has no matched stock ids', async () => {
|
|
const { service, calls } = createServiceHarness({
|
|
matchedBarcodeRows: [],
|
|
rows: [ {
|
|
id: 99,
|
|
} ],
|
|
});
|
|
|
|
const result = await service.getWarehouseStock({
|
|
barcode: {
|
|
operator: 'eq',
|
|
value: 'NOT-FOUND',
|
|
},
|
|
}, {
|
|
page: 1,
|
|
pageSize: 20,
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data.list, []);
|
|
assert.equal(result.data.pagination.total, 0);
|
|
assert.equal(calls.countArgs, null);
|
|
assert.equal(calls.findArgs, null);
|
|
});
|
|
|
|
it('should ignore empty where values and fallback invalid sort field', async () => {
|
|
const { service, calls } = createServiceHarness();
|
|
|
|
const result = await service.getWarehouseStock({
|
|
item_name: {
|
|
operator: 'like',
|
|
value: ' ',
|
|
},
|
|
store_id: {
|
|
operator: 'in',
|
|
value: [],
|
|
},
|
|
item_code: {
|
|
operator: 'like',
|
|
value: 'SKU-1',
|
|
},
|
|
}, {
|
|
page: 2,
|
|
pageSize: 5,
|
|
}, {
|
|
sortField: 'unknown_field',
|
|
sortOrder: 'UNKNOWN',
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(calls.findArgs.order, [ [ 'create_at', 'DESC' ] ]);
|
|
assert.equal(result.data.pagination.page, 2);
|
|
assert.equal(result.data.pagination.pageSize, 5);
|
|
});
|
|
|
|
it('should return failure reason when where deconstruction is invalid', async () => {
|
|
const { service } = createServiceHarness();
|
|
|
|
const result = await service.getWarehouseStock({
|
|
item_code: 'SKU-1',
|
|
}, {
|
|
page: 1,
|
|
pageSize: 20,
|
|
});
|
|
|
|
assert.equal(result.code, 400);
|
|
assert.match(result.message, /筛选条件解析失败/);
|
|
assert.match(result.message, /字段 item_code 条件必须是对象/);
|
|
});
|
|
});
|