2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
|
2016-04-01 22:25:16 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2018-12-07 14:54:29 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const should = require('should');
|
2015-08-24 12:41:04 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const includeUtils = require('../lib/include_utils');
|
2015-08-24 12:41:04 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('include_util', function() {
|
|
|
|
describe('#buildOneToOneIdentityMapWithOrigKeys', function() {
|
|
|
|
it('should return an object with keys', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2018-06-12 07:13:32 +00:00
|
|
|
{id: 11, letter: 'A'},
|
|
|
|
{id: 22, letter: 'B'},
|
2015-08-24 12:41:04 +00:00
|
|
|
];
|
2018-12-07 14:54:29 +00:00
|
|
|
const result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
2015-10-30 15:36:50 +00:00
|
|
|
result.get(11).should.be.ok;
|
|
|
|
result.get(22).should.be.ok;
|
2015-08-24 12:41:04 +00:00
|
|
|
});
|
|
|
|
|
2018-10-18 17:32:16 +00:00
|
|
|
it('should report errors if id is missing', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2018-10-18 17:32:16 +00:00
|
|
|
{letter: 'A'},
|
|
|
|
{id: 22, letter: 'B'},
|
|
|
|
];
|
|
|
|
function build() {
|
|
|
|
includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
|
|
|
}
|
|
|
|
build.should.throw(/ID property "id" is missing/);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should overwrite keys in case of collision', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: 11, letter: 'A'},
|
|
|
|
{id: 22, letter: 'B'},
|
|
|
|
{id: 33, letter: 'C'},
|
|
|
|
{id: 11, letter: 'HA!'},
|
2016-04-01 13:23:42 +00:00
|
|
|
];
|
2015-08-24 12:41:04 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
2016-04-01 11:48:17 +00:00
|
|
|
result.getKeys().should.containEql(11);
|
|
|
|
result.getKeys().should.containEql(22);
|
|
|
|
result.getKeys().should.containEql(33);
|
|
|
|
result.get(11)['letter'].should.equal('HA!');
|
|
|
|
result.get(33)['letter'].should.equal('C');
|
2015-08-24 12:41:04 +00:00
|
|
|
});
|
2018-06-12 07:13:32 +00:00
|
|
|
|
|
|
|
it('should return an object with no additional keys', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: 11, letter: 'A'},
|
|
|
|
{id: 22, letter: 'B'},
|
2015-10-10 14:21:06 +00:00
|
|
|
];
|
2018-12-07 14:54:29 +00:00
|
|
|
const result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
2018-06-12 07:13:32 +00:00
|
|
|
result.getKeys().should.eql([11, 22]); // no additional properties
|
2015-10-10 14:21:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-12 07:13:32 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('#buildOneToManyIdentityMap', function() {
|
|
|
|
it('should return an object with keys', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{id: 11, letter: 'A'},
|
|
|
|
{id: 22, letter: 'B'},
|
2016-04-01 13:23:42 +00:00
|
|
|
];
|
2018-12-07 14:54:29 +00:00
|
|
|
const result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'id');
|
2016-04-01 13:23:42 +00:00
|
|
|
result.exist(11).should.be.true;
|
|
|
|
result.exist(22).should.be.true;
|
|
|
|
});
|
2015-08-24 12:41:04 +00:00
|
|
|
|
2018-10-18 17:32:16 +00:00
|
|
|
it('should report errors if id is missing', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2018-10-18 17:32:16 +00:00
|
|
|
{letter: 'A'},
|
|
|
|
{id: 22, letter: 'B'},
|
|
|
|
];
|
|
|
|
function build() {
|
|
|
|
includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'id');
|
|
|
|
}
|
|
|
|
build.should.throw(/ID property "id" is missing/);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should collect keys in case of collision', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const objs = [
|
2016-08-19 17:46:59 +00:00
|
|
|
{'fk_id': 11, letter: 'A'},
|
|
|
|
{'fk_id': 22, letter: 'B'},
|
|
|
|
{'fk_id': 33, letter: 'C'},
|
|
|
|
{'fk_id': 11, letter: 'HA!'},
|
2016-04-01 13:23:42 +00:00
|
|
|
];
|
2015-08-24 12:41:04 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'fk_id');
|
2016-04-01 13:23:42 +00:00
|
|
|
result.get(11)[0]['letter'].should.equal('A');
|
|
|
|
result.get(11)[1]['letter'].should.equal('HA!');
|
|
|
|
result.get(33)[0]['letter'].should.equal('C');
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-08-24 12:41:04 +00:00
|
|
|
});
|
2015-10-30 15:15:48 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('KVMap', function() {
|
|
|
|
it('should allow to set and get value with key string', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const map = new includeUtils.KVMap();
|
2015-10-30 15:15:48 +00:00
|
|
|
map.set('name', 'Alex');
|
|
|
|
map.set('gender', true);
|
|
|
|
map.set('age', 25);
|
|
|
|
map.get('name').should.be.equal('Alex');
|
|
|
|
map.get('gender').should.be.equal(true);
|
|
|
|
map.get('age').should.be.equal(25);
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to set and get value with arbitrary key type', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const map = new includeUtils.KVMap();
|
2015-10-30 15:15:48 +00:00
|
|
|
map.set('name', 'Alex');
|
|
|
|
map.set(true, 'male');
|
|
|
|
map.set(false, false);
|
2016-08-19 17:46:59 +00:00
|
|
|
map.set({isTrue: 'yes'}, 25);
|
2015-10-30 15:15:48 +00:00
|
|
|
map.get('name').should.be.equal('Alex');
|
|
|
|
map.get(true).should.be.equal('male');
|
|
|
|
map.get(false).should.be.equal(false);
|
2016-08-19 17:46:59 +00:00
|
|
|
map.get({isTrue: 'yes'}).should.be.equal(25);
|
2015-10-30 15:15:48 +00:00
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not allow to get values with [] operator', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const map = new includeUtils.KVMap();
|
2015-10-30 15:15:48 +00:00
|
|
|
map.set('name', 'Alex');
|
|
|
|
(map['name'] === undefined).should.be.equal(true);
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should provide .exist() method for checking if key presented', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const map = new includeUtils.KVMap();
|
2015-10-30 15:15:48 +00:00
|
|
|
map.set('one', 1);
|
|
|
|
map.set(2, 'two');
|
|
|
|
map.set(true, 'true');
|
|
|
|
map.exist('one').should.be.true;
|
|
|
|
map.exist(2).should.be.true;
|
|
|
|
map.exist(true).should.be.true;
|
|
|
|
map.exist('two').should.be.false;
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should return array of original keys with .getKeys()', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const map = new includeUtils.KVMap();
|
2015-10-30 15:15:48 +00:00
|
|
|
map.set('one', 1);
|
|
|
|
map.set(2, 'two');
|
|
|
|
map.set(true, 'true');
|
2018-12-07 14:54:29 +00:00
|
|
|
const keys = map.getKeys();
|
2015-10-30 15:15:48 +00:00
|
|
|
keys.should.containEql('one');
|
|
|
|
keys.should.containEql(2);
|
|
|
|
keys.should.containEql(true);
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to store and fetch arrays', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const map = new includeUtils.KVMap();
|
2015-10-30 15:15:48 +00:00
|
|
|
map.set(1, [1, 2, 3]);
|
|
|
|
map.set(2, [2, 3, 4]);
|
2018-12-07 14:54:29 +00:00
|
|
|
const valueOne = map.get(1);
|
2015-10-30 15:15:48 +00:00
|
|
|
valueOne.should.be.eql([1, 2, 3]);
|
|
|
|
valueOne.push(99);
|
|
|
|
map.set(1, valueOne);
|
2018-12-07 14:54:29 +00:00
|
|
|
const valueOneUpdated = map.get(1);
|
2015-10-30 15:15:48 +00:00
|
|
|
valueOneUpdated.should.be.eql([1, 2, 3, 99]);
|
|
|
|
});
|
|
|
|
});
|