1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 | const assert = require('assert');
|
8 | const should = require('should');
|
9 |
|
10 | const includeUtils = require('../lib/include_utils');
|
11 |
|
12 | describe('include_util', function() {
|
13 | describe('#buildOneToOneIdentityMapWithOrigKeys', function() {
|
14 | it('should return an object with keys', function() {
|
15 | const objs = [
|
16 | {id: 11, letter: 'A'},
|
17 | {id: 22, letter: 'B'},
|
18 | ];
|
19 | const result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
20 | result.get(11).should.be.ok;
|
21 | result.get(22).should.be.ok;
|
22 | });
|
23 |
|
24 | it('should report errors if id is missing', function() {
|
25 | const objs = [
|
26 | {letter: 'A'},
|
27 | {id: 22, letter: 'B'},
|
28 | ];
|
29 | function build() {
|
30 | includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
31 | }
|
32 | build.should.throw(/ID property "id" is missing/);
|
33 | });
|
34 |
|
35 | it('should overwrite keys in case of collision', function() {
|
36 | const objs = [
|
37 | {id: 11, letter: 'A'},
|
38 | {id: 22, letter: 'B'},
|
39 | {id: 33, letter: 'C'},
|
40 | {id: 11, letter: 'HA!'},
|
41 | ];
|
42 |
|
43 | const result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
44 | result.getKeys().should.containEql(11);
|
45 | result.getKeys().should.containEql(22);
|
46 | result.getKeys().should.containEql(33);
|
47 | result.get(11)['letter'].should.equal('HA!');
|
48 | result.get(33)['letter'].should.equal('C');
|
49 | });
|
50 |
|
51 | it('should return an object with no additional keys', function() {
|
52 | const objs = [
|
53 | {id: 11, letter: 'A'},
|
54 | {id: 22, letter: 'B'},
|
55 | ];
|
56 | const result = includeUtils.buildOneToOneIdentityMapWithOrigKeys(objs, 'id');
|
57 | result.getKeys().should.eql([11, 22]);
|
58 | });
|
59 | });
|
60 |
|
61 | describe('#buildOneToManyIdentityMap', function() {
|
62 | it('should return an object with keys', function() {
|
63 | const objs = [
|
64 | {id: 11, letter: 'A'},
|
65 | {id: 22, letter: 'B'},
|
66 | ];
|
67 | const result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'id');
|
68 | result.exist(11).should.be.true;
|
69 | result.exist(22).should.be.true;
|
70 | });
|
71 |
|
72 | it('should report errors if id is missing', function() {
|
73 | const objs = [
|
74 | {letter: 'A'},
|
75 | {id: 22, letter: 'B'},
|
76 | ];
|
77 | function build() {
|
78 | includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'id');
|
79 | }
|
80 | build.should.throw(/ID property "id" is missing/);
|
81 | });
|
82 |
|
83 | it('should collect keys in case of collision', function() {
|
84 | const objs = [
|
85 | {'fk_id': 11, letter: 'A'},
|
86 | {'fk_id': 22, letter: 'B'},
|
87 | {'fk_id': 33, letter: 'C'},
|
88 | {'fk_id': 11, letter: 'HA!'},
|
89 | ];
|
90 |
|
91 | const result = includeUtils.buildOneToManyIdentityMapWithOrigKeys(objs, 'fk_id');
|
92 | result.get(11)[0]['letter'].should.equal('A');
|
93 | result.get(11)[1]['letter'].should.equal('HA!');
|
94 | result.get(33)[0]['letter'].should.equal('C');
|
95 | });
|
96 | });
|
97 | });
|
98 |
|
99 | describe('KVMap', function() {
|
100 | it('should allow to set and get value with key string', function() {
|
101 | const map = new includeUtils.KVMap();
|
102 | map.set('name', 'Alex');
|
103 | map.set('gender', true);
|
104 | map.set('age', 25);
|
105 | map.get('name').should.be.equal('Alex');
|
106 | map.get('gender').should.be.equal(true);
|
107 | map.get('age').should.be.equal(25);
|
108 | });
|
109 | it('should allow to set and get value with arbitrary key type', function() {
|
110 | const map = new includeUtils.KVMap();
|
111 | map.set('name', 'Alex');
|
112 | map.set(true, 'male');
|
113 | map.set(false, false);
|
114 | map.set({isTrue: 'yes'}, 25);
|
115 | map.get('name').should.be.equal('Alex');
|
116 | map.get(true).should.be.equal('male');
|
117 | map.get(false).should.be.equal(false);
|
118 | map.get({isTrue: 'yes'}).should.be.equal(25);
|
119 | });
|
120 | it('should not allow to get values with [] operator', function() {
|
121 | const map = new includeUtils.KVMap();
|
122 | map.set('name', 'Alex');
|
123 | (map['name'] === undefined).should.be.equal(true);
|
124 | });
|
125 | it('should provide .exist() method for checking if key presented', function() {
|
126 | const map = new includeUtils.KVMap();
|
127 | map.set('one', 1);
|
128 | map.set(2, 'two');
|
129 | map.set(true, 'true');
|
130 | map.exist('one').should.be.true;
|
131 | map.exist(2).should.be.true;
|
132 | map.exist(true).should.be.true;
|
133 | map.exist('two').should.be.false;
|
134 | });
|
135 | it('should return array of original keys with .getKeys()', function() {
|
136 | const map = new includeUtils.KVMap();
|
137 | map.set('one', 1);
|
138 | map.set(2, 'two');
|
139 | map.set(true, 'true');
|
140 | const keys = map.getKeys();
|
141 | keys.should.containEql('one');
|
142 | keys.should.containEql(2);
|
143 | keys.should.containEql(true);
|
144 | });
|
145 | it('should allow to store and fetch arrays', function() {
|
146 | const map = new includeUtils.KVMap();
|
147 | map.set(1, [1, 2, 3]);
|
148 | map.set(2, [2, 3, 4]);
|
149 | const valueOne = map.get(1);
|
150 | valueOne.should.be.eql([1, 2, 3]);
|
151 | valueOne.push(99);
|
152 | map.set(1, valueOne);
|
153 | const valueOneUpdated = map.get(1);
|
154 | valueOneUpdated.should.be.eql([1, 2, 3, 99]);
|
155 | });
|
156 | });
|