WMS_MAIN/test/app/service/systems/store_warehouse.queryPrintQuantityLogs.test.js

197 lines
5.0 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const StoreWarehouseService = require('../../../../app/service/systems/store_warehouse');
const { Op } = require('sequelize');
const { parseWhereConditions } = require('../../../../app/lib/query_condition_parser');
function createHarness(options = {}) {
const {
currentSubjectStoreId = 100000,
currentSubjectProject = 'warehouse',
count = 3,
rows = [],
} = options;
const calls = {
findAndCountAllArgs: null,
};
const defaultRows = rows.length ? rows : [
{
id: 11,
contract_id: 88,
store_id: 1000001,
warehouse_id: 100000,
change_type: 'manual_adjust',
business_type: 'order_express',
business_id: 123,
change_quantity: -5,
balance_before: 20,
balance_after: 15,
reason: '测试扣减',
operator_id: 9001,
created_by: 9001,
created_at: '2026-05-20 10:00:00',
updated_at: '2026-05-20 10:00:00',
},
];
const ctx = {
app: {
config: {},
Sequelize: {
Op,
},
},
user: {
store_id: currentSubjectStoreId,
id: 10001,
},
model: {
Store: {
async findOne({ where }) {
if (Number(where.id) !== Number(currentSubjectStoreId)) {
return null;
}
return {
id: currentSubjectStoreId,
company_name: '当前主体',
project: currentSubjectProject,
status: 1,
};
},
},
StoreWarehousePrintQuantityLog: {
async findAndCountAll(args = {}) {
calls.findAndCountAllArgs = args;
return {
rows: defaultRows,
count,
};
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
};
ctx.app.model = ctx.model;
return {
service: new StoreWarehouseService(ctx),
calls,
};
}
describe('test/app/service/systems/store_warehouse.queryPrintQuantityLogs.test.js', () => {
it('should query print quantity logs by store_id with pagination for warehouse subject', async () => {
const { service, calls } = createHarness({
currentSubjectStoreId: 100000,
currentSubjectProject: 'warehouse',
count: 3,
});
const result = await service.queryPrintQuantityLogs({
store_id: 1000001,
pagination: {
page: 2,
page_size: 1,
},
});
assert.deepEqual(calls.findAndCountAllArgs.where, {
store_id: 1000001,
warehouse_id: 100000,
});
assert.deepEqual(calls.findAndCountAllArgs.order, [[ 'created_at', 'DESC' ], [ 'id', 'DESC' ]]);
assert.equal(calls.findAndCountAllArgs.offset, 1);
assert.equal(calls.findAndCountAllArgs.limit, 1);
assert.equal(result.code, 200);
assert.equal(result.data.pagination.total, 3);
assert.equal(result.data.pagination.page, 2);
assert.equal(result.data.pagination.pageSize, 1);
assert.equal(result.data.pagination.totalPages, 3);
assert.equal(result.data.pagination.hasNext, true);
assert.equal(result.data.pagination.hasPrevious, true);
assert.equal(result.data.list.length, 1);
assert.equal(result.data.list[0].id, 11);
assert.equal(result.data.list[0].store_id, 1000001);
});
it('should merge whereParsed dynamic conditions into findAndCountAll where', async () => {
const { service, calls } = createHarness({
currentSubjectStoreId: 100000,
currentSubjectProject: 'warehouse',
count: 1,
});
const where = {
created_at: {
operator: 'between',
type: 'daterange',
value: [ '2026-05-01', '2026-05-20' ],
},
};
service.ctx.whereParsed = parseWhereConditions({
where,
app: service.ctx.app,
}).result;
await service.queryPrintQuantityLogs({
store_id: 1000001,
where,
pagination: {
page: 1,
page_size: 20,
},
});
assert.deepEqual(calls.findAndCountAllArgs.where, {
store_id: 1000001,
warehouse_id: 100000,
[Op.and]: [
{
created_at: {
[Op.between]: [ '2026-05-01 00:00:00', '2026-05-20 23:59:59' ],
},
},
],
});
});
it('should ignore top-level page fields and fallback to default pagination', async () => {
const { service, calls } = createHarness({
currentSubjectStoreId: 100000,
currentSubjectProject: 'warehouse',
count: 1,
});
await service.queryPrintQuantityLogs({
store_id: 1000001,
page: 5,
page_size: 30,
});
assert.equal(calls.findAndCountAllArgs.offset, 0);
assert.equal(calls.findAndCountAllArgs.limit, 20);
});
it('should reject when store_id is invalid', async () => {
const { service } = createHarness({
currentSubjectStoreId: 100000,
currentSubjectProject: 'warehouse',
});
await assert.rejects(async () => {
await service.queryPrintQuantityLogs({
store_id: 0,
});
}, /store_id 参数不能为空/);
});
});