118 lines
3.4 KiB
JavaScript
118 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const ExpressService = require('../../../../app/service/systems/express');
|
|
const IndexMethodService = require('../../../../app/service/systemMethods/index');
|
|
|
|
function createExpressServiceHarness(options = {}) {
|
|
const calls = {
|
|
findAll: 0,
|
|
getCache: 0,
|
|
setCache: 0,
|
|
};
|
|
|
|
const ctx = {
|
|
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: {
|
|
Express: {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.findAllQuery = query;
|
|
return options.rows || [
|
|
{ code: 'STO', name: '顺丰' },
|
|
{ code: 'TB', name: '淘宝快递' },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/express.test.js', () => {
|
|
it('should load express list from database and cache it when cache is empty', async () => {
|
|
const { ctx, calls } = createExpressServiceHarness();
|
|
const service = new ExpressService(ctx);
|
|
|
|
const result = await service.getExpressList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 1);
|
|
assert.equal(calls.setCache, 1);
|
|
assert.equal(calls.cachePayload.cacheKey, 'express:list');
|
|
assert.equal(calls.cachePayload.ttl, 600);
|
|
assert.deepEqual(result, [
|
|
{ code: 'STO', name: '顺丰', value: 'STO', label: '顺丰' },
|
|
{ code: 'TB', name: '淘宝快递', value: 'TB', label: '淘宝快递' },
|
|
]);
|
|
});
|
|
|
|
it('should return cached express list without querying database', async () => {
|
|
const cachedValue = [
|
|
{ code: 'SF', name: '顺丰', value: 'SF', label: '顺丰' },
|
|
];
|
|
const { ctx, calls } = createExpressServiceHarness({
|
|
cachedValue,
|
|
});
|
|
const service = new ExpressService(ctx);
|
|
|
|
const result = await service.getExpressList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 0);
|
|
assert.equal(calls.setCache, 0);
|
|
assert.deepEqual(result, cachedValue);
|
|
});
|
|
|
|
it('should delegate index.getExpressList to the express service', async () => {
|
|
const { ctx } = createExpressServiceHarness();
|
|
let serviceCalls = 0;
|
|
ctx.service = {
|
|
systems: {
|
|
express: {
|
|
async getExpressList() {
|
|
serviceCalls += 1;
|
|
return [
|
|
{ code: 'STO', name: '顺丰', value: 'STO', label: '顺丰' },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const methodService = new IndexMethodService(ctx);
|
|
const result = await methodService.getExpressList();
|
|
|
|
assert.equal(serviceCalls, 1);
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, [
|
|
{ code: 'STO', name: '顺丰', value: 'STO', label: '顺丰' },
|
|
]);
|
|
});
|
|
}); |