165 lines
6.2 KiB
JavaScript
165 lines
6.2 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const PlatformService = require('../../../../app/service/systems/platform');
|
|
const IndexMethodService = require('../../../../app/service/systemMethods/index');
|
|
|
|
/**
|
|
* 创建平台服务测试桩。
|
|
*
|
|
* @param {Object} [options={}] 测试配置。
|
|
* @return {Object} 测试上下文和调用记录。
|
|
*/
|
|
function createPlatformServiceHarness(options = {}) {
|
|
const calls = {
|
|
findAll: 0,
|
|
platformFindAll: 0,
|
|
platformSettingFindAll: 0,
|
|
getCache: 0,
|
|
setCache: 0,
|
|
};
|
|
|
|
const ctx = {
|
|
user: {
|
|
store_id: options.storeId === undefined ? 100000 : options.storeId,
|
|
},
|
|
app: {
|
|
async getCacheAsync(cacheKey) {
|
|
calls.getCache += 1;
|
|
if (typeof options.getCacheAsync === 'function') {
|
|
return await options.getCacheAsync(cacheKey);
|
|
}
|
|
return options.cachedValue === undefined ? null : options.cachedValue;
|
|
},
|
|
async setCacheAsync(cacheKey, value, ttl) {
|
|
calls.setCache += 1;
|
|
calls.cachePayload = { cacheKey, value, ttl };
|
|
if (typeof options.setCacheAsync === 'function') {
|
|
return await options.setCacheAsync(cacheKey, value, ttl);
|
|
}
|
|
},
|
|
},
|
|
model: {
|
|
Platform: {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.platformFindAll += 1;
|
|
calls.platformFindAllQuery = query;
|
|
return options.rows || [
|
|
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API' },
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS' },
|
|
];
|
|
},
|
|
},
|
|
PlatformSetting: {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.platformSettingFindAll += 1;
|
|
calls.platformSettingFindAllQuery = query;
|
|
return options.settingRows || [
|
|
{ id: 11, code: 'JD-STORE', platform_code: 'JD', warehouse_id: 100000 },
|
|
{ id: 12, code: 'TB-STORE', platform_code: 'TB', warehouse_id: 100000 },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/platform.test.js', () => {
|
|
it('should load merged platform list from database and cache it when cache is empty', async () => {
|
|
const { ctx, calls } = createPlatformServiceHarness();
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 2);
|
|
assert.equal(calls.platformFindAll, 1);
|
|
assert.equal(calls.platformSettingFindAll, 1);
|
|
assert.equal(calls.setCache, 1);
|
|
assert.equal(calls.cachePayload.cacheKey, 'platform:list:100000');
|
|
assert.equal(calls.cachePayload.ttl, 600);
|
|
assert.deepEqual(result, [
|
|
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API', value: 'JD', label: '京东平台', platform_code: 'JD', platform_name: '京东平台', is_system_setting: false },
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
{ id: 11, code: 'JD-STORE', name: 'JD-STORE (京东平台)', value: 'JD-STORE', label: 'JD-STORE (京东平台)', is_system_setting: true, alias: '', platform_code: 'JD', platform_name: '京东平台', default_express_interface: 'OPEN_API' },
|
|
{ id: 12, code: 'TB-STORE', name: 'TB-STORE (淘宝平台)', value: 'TB-STORE', label: 'TB-STORE (淘宝平台)', is_system_setting: true, alias: '', platform_code: 'TB', platform_name: '淘宝平台', default_express_interface: 'QM_WMS' },
|
|
]);
|
|
});
|
|
|
|
it('should return cached platform list without querying database', async () => {
|
|
const cachedValue = [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
];
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
cachedValue,
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 0);
|
|
assert.equal(calls.setCache, 0);
|
|
assert.deepEqual(result, cachedValue);
|
|
});
|
|
|
|
it('should merge platform setting code to real platform code and name', async () => {
|
|
const { ctx } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS' },
|
|
],
|
|
settingRows: [
|
|
{ id: 21, code: 'TB-STORE', platform_code: 'TB', warehouse_id: 100000 },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.deepEqual(result, [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
{ id: 21, code: 'TB-STORE', name: 'TB-STORE (淘宝平台)', value: 'TB-STORE', label: 'TB-STORE (淘宝平台)', is_system_setting: true, alias: '', platform_code: 'TB', platform_name: '淘宝平台', default_express_interface: 'QM_WMS' },
|
|
]);
|
|
});
|
|
|
|
it('should delegate index.getPlatformList to the platform service', async () => {
|
|
const { ctx } = createPlatformServiceHarness();
|
|
let serviceCalls = 0;
|
|
ctx.service = {
|
|
systems: {
|
|
platform: {
|
|
async getPlatformList() {
|
|
serviceCalls += 1;
|
|
return [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台' },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const methodService = new IndexMethodService(ctx);
|
|
const result = await methodService.getPlatformList();
|
|
|
|
assert.equal(serviceCalls, 1);
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台' },
|
|
]);
|
|
});
|
|
});
|