2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// require('../../../support/test-helper');
|
2016-05-26 17:23:19 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
|
2019-10-07 17:29:07 +08:00
|
|
|
var MapConfigAdapter = require('../../../lib/models/mapconfig/adapter');
|
2016-05-26 17:23:19 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('MapConfigAdapter', function () {
|
2016-05-26 17:23:19 +08:00
|
|
|
var user = 'wadus';
|
2019-10-22 01:07:24 +08:00
|
|
|
function requestMapConfig () {
|
2016-05-26 17:23:19 +08:00
|
|
|
return {
|
|
|
|
val: 0
|
|
|
|
};
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
function params () {
|
2016-05-26 17:23:19 +08:00
|
|
|
return {};
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
function context () {
|
2016-05-26 17:23:19 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createAdapter (valOperatorFn) {
|
|
|
|
return function ValMapConfigAdapter () {
|
|
|
|
this.getMapConfig = function (user, requestMapConfig, params, context, callback) {
|
2016-05-26 17:30:28 +08:00
|
|
|
requestMapConfig.val = valOperatorFn(requestMapConfig.val);
|
|
|
|
return callback(null, requestMapConfig);
|
|
|
|
};
|
2016-05-26 17:23:19 +08:00
|
|
|
};
|
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
var IncValMapConfigAdapter = createAdapter(function (val) { return val + 1; });
|
|
|
|
var Mul2ValMapConfigAdapter = createAdapter(function (val) { return val * 2; });
|
2016-05-26 17:23:19 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function validateMapConfig (adapter, expectedNumAdapters, expectedVal, callback) {
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(adapter.adapters.length, expectedNumAdapters);
|
2019-10-22 01:07:24 +08:00
|
|
|
adapter.getMapConfig(user, requestMapConfig(), params(), context(), function (err, mapConfig) {
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(mapConfig.val, expectedVal);
|
2016-05-26 17:23:19 +08:00
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('works with no adapters', function (done) {
|
2016-05-26 17:23:19 +08:00
|
|
|
var adapter = new MapConfigAdapter();
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 0, 0, done);
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('works with no adapters as empty array', function (done) {
|
2016-05-26 17:23:19 +08:00
|
|
|
var adapter = new MapConfigAdapter([]);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 0, 0, done);
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('works with basic adapter', function (done) {
|
2016-05-26 17:23:19 +08:00
|
|
|
var adapter = new MapConfigAdapter(new IncValMapConfigAdapter());
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 1, 1, done);
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('works with basic adapter as array', function (done) {
|
2016-05-26 17:23:19 +08:00
|
|
|
var adapter = new MapConfigAdapter([new IncValMapConfigAdapter()]);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 1, 1, done);
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('works with several adapters', function (done) {
|
2016-05-26 17:23:19 +08:00
|
|
|
var adapter = new MapConfigAdapter(new IncValMapConfigAdapter(), new IncValMapConfigAdapter());
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 2, 2, done);
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('works with several adapters as array', function (done) {
|
2016-05-26 17:23:19 +08:00
|
|
|
var adapter = new MapConfigAdapter([new IncValMapConfigAdapter(), new IncValMapConfigAdapter()]);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 2, 2, done);
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|
2016-05-26 17:30:28 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should execute in order 1', function (done) {
|
2016-05-26 17:30:28 +08:00
|
|
|
var adapter = new MapConfigAdapter([new Mul2ValMapConfigAdapter(), new IncValMapConfigAdapter()]);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 2, 1, done);
|
2016-05-26 17:30:28 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should execute in order 2', function (done) {
|
2016-05-26 17:30:28 +08:00
|
|
|
var adapter = new MapConfigAdapter([new IncValMapConfigAdapter(), new Mul2ValMapConfigAdapter()]);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 2, 2, done);
|
2016-05-26 17:30:28 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should execute in order 3', function (done) {
|
2016-05-26 17:30:28 +08:00
|
|
|
var adapter = new MapConfigAdapter([new Mul2ValMapConfigAdapter(), new Mul2ValMapConfigAdapter()]);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 2, 0, done);
|
2016-05-26 17:30:28 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should execute in order 4', function (done) {
|
|
|
|
var Mul5ValMapConfigAdapter = createAdapter(function (val) { return val * 5; });
|
2016-05-26 17:30:28 +08:00
|
|
|
var adapter = new MapConfigAdapter(
|
|
|
|
new IncValMapConfigAdapter(),
|
|
|
|
new Mul2ValMapConfigAdapter(),
|
|
|
|
new Mul5ValMapConfigAdapter()
|
|
|
|
);
|
2016-05-26 17:36:03 +08:00
|
|
|
validateMapConfig(adapter, 3, 10, done);
|
2016-05-26 17:30:28 +08:00
|
|
|
});
|
2016-05-26 17:23:19 +08:00
|
|
|
});
|