@e22m4u/js-repository 0.0.33 → 0.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/package.json +1 -1
  2. package/src/adapter/adapter-loader.js +1 -1
  3. package/src/adapter/adapter-loader.spec.js +1 -1
  4. package/src/adapter/decorator/data-sanitizing-decorator.js +1 -1
  5. package/src/adapter/decorator/data-validation-decorator.js +1 -1
  6. package/src/adapter/decorator/default-values-decorator.js +1 -1
  7. package/src/adapter/decorator/fields-filtering-decorator.js +1 -1
  8. package/src/adapter/decorator/inclusion-decorator.js +1 -1
  9. package/src/definition/model/model-data-sanitizer.js +2 -2
  10. package/src/definition/model/model-data-validator.js +1 -1
  11. package/src/definition/model/model-data-validator.spec.js +1 -1
  12. package/src/definition/model/model-definition-utils.js +1 -1
  13. package/src/definition/model/model-definition-utils.spec.js +1 -1
  14. package/src/definition/model/properties/default-values-definition-validator.js +1 -1
  15. package/src/definition/model/properties/default-values-definition-validator.spec.js +1 -1
  16. package/src/definition/model/properties/properties-definition-validator.js +2 -2
  17. package/src/definition/model/properties/properties-definition-validator.spec.js +1 -1
  18. package/src/definition/model/relations/relations-definition-validator.js +2 -2
  19. package/src/definition/model/relations/relations-definition-validator.spec.js +1 -1
  20. package/src/filter/fields-clause-tool.d.ts +2 -2
  21. package/src/filter/fields-clause-tool.js +32 -21
  22. package/src/filter/fields-clause-tool.spec.js +479 -100
  23. package/src/filter/include-clause-tool.js +50 -42
  24. package/src/filter/include-clause-tool.spec.js +4391 -291
  25. package/src/filter/operator-clause-tool.js +13 -13
  26. package/src/filter/operator-clause-tool.spec.js +13 -13
  27. package/src/filter/order-clause-tool.js +20 -17
  28. package/src/filter/order-clause-tool.spec.js +621 -362
  29. package/src/filter/slice-clause-tool.js +5 -5
  30. package/src/filter/slice-clause-tool.spec.js +92 -51
  31. package/src/filter/where-clause-tool.js +7 -7
  32. package/src/filter/where-clause-tool.spec.js +79 -55
  33. package/src/utils/select-object-keys.js +3 -3
  34. package/src/utils/select-object-keys.spec.js +3 -3
@@ -11,123 +11,502 @@ const T = S.getService(FieldsClauseTool);
11
11
 
12
12
  describe('FieldsClauseTool', function () {
13
13
  describe('filter', function () {
14
- it('returns an object with selected fields', function () {
15
- const value = {foo: 'fooVal', bar: 'barVal', baz: 'bazVal'};
16
- const fields = ['bar', 'baz'];
17
- const result = T.filter(value, MODEL_NAME, fields);
18
- expect(result).to.be.eql({bar: 'barVal', baz: 'bazVal'});
19
- });
14
+ describe('object', function () {
15
+ describe('single field', function () {
16
+ it('does not throw an error if the given field is not exist', function () {
17
+ const object = {foo: 'a1', bar: 'a2', baz: 'a3'};
18
+ const res = T.filter(object, MODEL_NAME, 'qux');
19
+ expect(res).to.be.eql({});
20
+ });
20
21
 
21
- it('returns an array with selected fields', function () {
22
- const value = [
23
- {foo: 'fooVal', bar: 'barVal', baz: 'bazVal'},
24
- {foo: 'fooVal', bar: 'barVal', baz: 'bazVal'},
25
- {foo: 'fooVal', bar: 'barVal', baz: 'bazVal'},
26
- ];
27
- const fields = ['bar', 'baz'];
28
- const result = T.filter(value, MODEL_NAME, fields);
29
- expect(result[0]).to.be.eql({bar: 'barVal', baz: 'bazVal'});
30
- expect(result[1]).to.be.eql({bar: 'barVal', baz: 'bazVal'});
31
- expect(result[2]).to.be.eql({bar: 'barVal', baz: 'bazVal'});
32
- });
22
+ it('requires the first argument to be an object', function () {
23
+ const throwable = v => () => T.filter(v, MODEL_NAME, 'bar');
24
+ const error = v =>
25
+ format(
26
+ 'The first argument of FieldsClauseTool.filter should be an Object or ' +
27
+ 'an Array of Object, but %s given.',
28
+ v,
29
+ );
30
+ expect(throwable('str')).to.throw(error('"str"'));
31
+ expect(throwable('')).to.throw(error('""'));
32
+ expect(throwable(10)).to.throw(error('10'));
33
+ expect(throwable(0)).to.throw(error('0'));
34
+ expect(throwable(true)).to.throw(error('true'));
35
+ expect(throwable(false)).to.throw(error('false'));
36
+ expect(throwable(undefined)).to.throw(error('undefined'));
37
+ expect(throwable(null)).to.throw(error('null'));
38
+ expect(throwable({foo: 'a1', bar: 'a2'})()).to.be.eql({bar: 'a2'});
39
+ expect(throwable({})()).to.be.eql({});
40
+ });
33
41
 
34
- it('includes a primary key', function () {
35
- const value = {[DEF_PK]: 10, foo: 'fooVal', bar: 'barVal', baz: 'bazVal'};
36
- const fields = ['bar', 'baz'];
37
- const result = T.filter(value, MODEL_NAME, fields);
38
- expect(result).to.be.eql({[DEF_PK]: 10, bar: 'barVal', baz: 'bazVal'});
39
- });
42
+ it('requires the second argument to be a non-empty string', function () {
43
+ const entity = {foo: 'a1', bar: 'a2', baz: 'a3'};
44
+ const throwable = v => () => T.filter(entity, v, 'bar');
45
+ const error = v =>
46
+ format(
47
+ 'The second argument of FieldsClauseTool.filter should be ' +
48
+ 'a non-empty String, but %s given.',
49
+ v,
50
+ );
51
+ expect(throwable('')).to.throw(error('""'));
52
+ expect(throwable(10)).to.throw(error('10'));
53
+ expect(throwable(0)).to.throw(error('0'));
54
+ expect(throwable(true)).to.throw(error('true'));
55
+ expect(throwable(false)).to.throw(error('false'));
56
+ expect(throwable(undefined)).to.throw(error('undefined'));
57
+ expect(throwable(null)).to.throw(error('null'));
58
+ expect(throwable('model')()).to.be.eql({bar: 'a2'});
59
+ });
40
60
 
41
- it('throws an error if a first argument is not an object', function () {
42
- const throwable = () => T.filter(10, MODEL_NAME, ['bar']);
43
- expect(throwable).to.throw(
44
- 'A first argument of FieldClauseTool.filter should be an Object or ' +
45
- 'an Array of Object, but 10 given.',
46
- );
47
- });
61
+ it('requires the third argument to be a non-empty string', function () {
62
+ const entity = {foo: 'a1', bar: 'a2', baz: 'a3'};
63
+ const throwable = v => () => T.filter(entity, MODEL_NAME, v);
64
+ const error = v =>
65
+ format(
66
+ 'The provided option "fields" should be a non-empty String ' +
67
+ 'or an Array of non-empty String, but %s given.',
68
+ v,
69
+ );
70
+ expect(throwable('')).to.throw(error('""'));
71
+ expect(throwable(10)).to.throw(error('10'));
72
+ expect(throwable(0)).to.throw(error('0'));
73
+ expect(throwable(true)).to.throw(error('true'));
74
+ expect(throwable(false)).to.throw(error('false'));
75
+ expect(throwable({})).to.throw(error('Object'));
76
+ expect(throwable('bar')()).to.be.eql({bar: 'a2'});
77
+ expect(throwable(undefined)()).to.be.eq(entity);
78
+ expect(throwable(null)()).to.be.eq(entity);
79
+ });
48
80
 
49
- it('throws an error if elements of a first argument is not an object', function () {
50
- const throwable = () => T.filter([10], MODEL_NAME, ['bar']);
51
- expect(throwable).to.throw(
52
- 'A first argument of FieldClauseTool.filter should be an Object or ' +
53
- 'an Array of Object, but 10 given.',
54
- );
55
- });
81
+ it('picks field of the given object', function () {
82
+ const value = {foo: 'a1', bar: 'a2', baz: 'a3'};
83
+ const result = T.filter(value, MODEL_NAME, 'bar');
84
+ expect(result).to.be.eql({bar: 'a2'});
85
+ });
86
+
87
+ it('includes the primary key of the given object', function () {
88
+ const value = {[DEF_PK]: 10, foo: 'a1', bar: 'a2', baz: 'a3'};
89
+ const result = T.filter(value, MODEL_NAME, 'bar');
90
+ expect(result).to.be.eql({[DEF_PK]: 10, bar: 'a2'});
91
+ });
92
+ });
93
+
94
+ describe('multiple fields', function () {
95
+ it('does not throw an error if multiple fields is not exist', function () {
96
+ const object = {foo: 'a1', bar: 'a2', baz: 'a3'};
97
+ const res = T.filter(object, MODEL_NAME, ['bar', 'qux']);
98
+ expect(res).to.be.eql({bar: 'a2'});
99
+ });
56
100
 
57
- it('throws an error if a second argument is not a string', function () {
58
- const throwable = () => T.filter({}, MODEL_NAME, 10);
59
- expect(throwable).to.throw(
60
- 'The provided option "fields" should be a String ' +
61
- 'or an Array of String, but 10 given.',
62
- );
101
+ it('requires the first argument to be an object', function () {
102
+ const throwable = v => () => T.filter(v, MODEL_NAME, ['bar', 'baz']);
103
+ const error = v =>
104
+ format(
105
+ 'The first argument of FieldsClauseTool.filter should be an Object or ' +
106
+ 'an Array of Object, but %s given.',
107
+ v,
108
+ );
109
+ expect(throwable('str')).to.throw(error('"str"'));
110
+ expect(throwable('')).to.throw(error('""'));
111
+ expect(throwable(10)).to.throw(error('10'));
112
+ expect(throwable(0)).to.throw(error('0'));
113
+ expect(throwable(true)).to.throw(error('true'));
114
+ expect(throwable(false)).to.throw(error('false'));
115
+ expect(throwable(undefined)).to.throw(error('undefined'));
116
+ expect(throwable(null)).to.throw(error('null'));
117
+ expect(throwable({foo: 'a1', bar: 'a2'})()).to.be.eql({bar: 'a2'});
118
+ expect(throwable({})()).to.be.eql({});
119
+ });
120
+
121
+ it('requires the second argument to be a non-empty string', function () {
122
+ const entity = {foo: 'a1', bar: 'a2', baz: 'a3'};
123
+ const throwable = v => () => T.filter(entity, v, ['bar', 'baz']);
124
+ const error = v =>
125
+ format(
126
+ 'The second argument of FieldsClauseTool.filter should be ' +
127
+ 'a non-empty String, but %s given.',
128
+ v,
129
+ );
130
+ expect(throwable('')).to.throw(error('""'));
131
+ expect(throwable(10)).to.throw(error('10'));
132
+ expect(throwable(0)).to.throw(error('0'));
133
+ expect(throwable(true)).to.throw(error('true'));
134
+ expect(throwable(false)).to.throw(error('false'));
135
+ expect(throwable(undefined)).to.throw(error('undefined'));
136
+ expect(throwable(null)).to.throw(error('null'));
137
+ expect(throwable('model')()).to.be.eql({bar: 'a2', baz: 'a3'});
138
+ });
139
+
140
+ it('requires the third argument to be an array of non-empty strings', function () {
141
+ const entity = {foo: 'a1', bar: 'a2', baz: 'a3'};
142
+ const throwable = v => () => T.filter(entity, MODEL_NAME, v);
143
+ const error = v =>
144
+ format(
145
+ 'The provided option "fields" should be a non-empty String ' +
146
+ 'or an Array of non-empty String, but %s given.',
147
+ v,
148
+ );
149
+ expect(throwable([''])).to.throw(error('""'));
150
+ expect(throwable([10])).to.throw(error('10'));
151
+ expect(throwable([0])).to.throw(error('0'));
152
+ expect(throwable([true])).to.throw(error('true'));
153
+ expect(throwable([false])).to.throw(error('false'));
154
+ expect(throwable([{}])).to.throw(error('Object'));
155
+ expect(throwable([undefined])).to.throw(error('undefined'));
156
+ expect(throwable([null])).to.throw(error('null'));
157
+ expect(throwable(['bar', 'baz'])()).to.be.eql({bar: 'a2', baz: 'a3'});
158
+ expect(throwable([])()).to.be.eq(entity);
159
+ });
160
+
161
+ it('picks fields of the given object', function () {
162
+ const value = {foo: 'a1', bar: 'a2', baz: 'a3'};
163
+ const result = T.filter(value, MODEL_NAME, ['bar', 'baz']);
164
+ expect(result).to.be.eql({bar: 'a2', baz: 'a3'});
165
+ });
166
+
167
+ it('includes the primary key of the given object', function () {
168
+ const value = {[DEF_PK]: 10, foo: 'a1', bar: 'a2', baz: 'a3'};
169
+ const result = T.filter(value, MODEL_NAME, ['bar', 'baz']);
170
+ expect(result).to.be.eql({[DEF_PK]: 10, bar: 'a2', baz: 'a3'});
171
+ });
172
+ });
63
173
  });
64
174
 
65
- it('throws an error if elements of a second argument is not a string', function () {
66
- const throwable = () => T.filter({}, MODEL_NAME, [10]);
67
- expect(throwable).to.throw(
68
- 'The provided option "fields" should be a String ' +
69
- 'or an Array of String, but 10 given.',
70
- );
175
+ describe('array', function () {
176
+ describe('single field', function () {
177
+ it('does not throw an error if the given field is not exist', function () {
178
+ const objects = [
179
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
180
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
181
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
182
+ ];
183
+ const res = T.filter(objects, MODEL_NAME, 'qux');
184
+ expect(res).to.be.eql([{}, {}, {}]);
185
+ });
186
+
187
+ it('requires the first argument to be an array of objects', function () {
188
+ const throwable = v => () => T.filter(v, MODEL_NAME, 'bar');
189
+ const error = v =>
190
+ format(
191
+ 'The first argument of FieldsClauseTool.filter should be an Object or ' +
192
+ 'an Array of Object, but %s given.',
193
+ v,
194
+ );
195
+ expect(throwable(['str'])).to.throw(error('"str"'));
196
+ expect(throwable([''])).to.throw(error('""'));
197
+ expect(throwable([10])).to.throw(error('10'));
198
+ expect(throwable([0])).to.throw(error('0'));
199
+ expect(throwable([true])).to.throw(error('true'));
200
+ expect(throwable([false])).to.throw(error('false'));
201
+ expect(throwable([undefined])).to.throw(error('undefined'));
202
+ expect(throwable([null])).to.throw(error('null'));
203
+ expect(throwable([{foo: 'a1', bar: 'a2'}])()).to.be.eql([
204
+ {bar: 'a2'},
205
+ ]);
206
+ expect(throwable([{}])()).to.be.eql([{}]);
207
+ });
208
+
209
+ it('requires the second argument to be a non-empty string', function () {
210
+ const entities = [
211
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
212
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
213
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
214
+ ];
215
+ const throwable = v => () => T.filter(entities, v, 'bar');
216
+ const error = v =>
217
+ format(
218
+ 'The second argument of FieldsClauseTool.filter should be ' +
219
+ 'a non-empty String, but %s given.',
220
+ v,
221
+ );
222
+ expect(throwable('')).to.throw(error('""'));
223
+ expect(throwable(10)).to.throw(error('10'));
224
+ expect(throwable(0)).to.throw(error('0'));
225
+ expect(throwable(true)).to.throw(error('true'));
226
+ expect(throwable(false)).to.throw(error('false'));
227
+ expect(throwable(undefined)).to.throw(error('undefined'));
228
+ expect(throwable(null)).to.throw(error('null'));
229
+ expect(throwable('model')()).to.be.eql([
230
+ {bar: 'a2'},
231
+ {bar: 'b2'},
232
+ {bar: 'c2'},
233
+ ]);
234
+ });
235
+
236
+ it('requires the third argument to be a non-empty string', function () {
237
+ const entities = [
238
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
239
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
240
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
241
+ ];
242
+ const throwable = v => () => T.filter(entities, MODEL_NAME, v);
243
+ const error = v =>
244
+ format(
245
+ 'The provided option "fields" should be a non-empty String ' +
246
+ 'or an Array of non-empty String, but %s given.',
247
+ v,
248
+ );
249
+ expect(throwable('')).to.throw(error('""'));
250
+ expect(throwable(10)).to.throw(error('10'));
251
+ expect(throwable(0)).to.throw(error('0'));
252
+ expect(throwable(true)).to.throw(error('true'));
253
+ expect(throwable(false)).to.throw(error('false'));
254
+ expect(throwable({})).to.throw(error('Object'));
255
+ expect(throwable('bar')()).to.be.eql([
256
+ {bar: 'a2'},
257
+ {bar: 'b2'},
258
+ {bar: 'c2'},
259
+ ]);
260
+ expect(throwable(undefined)()).to.be.eq(entities);
261
+ expect(throwable(null)()).to.be.eq(entities);
262
+ });
263
+
264
+ it('picks field of the given object', function () {
265
+ const entities = [
266
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
267
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
268
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
269
+ ];
270
+ const result = T.filter(entities, MODEL_NAME, 'bar');
271
+ expect(result).to.have.lengthOf(3);
272
+ expect(result[0]).to.be.eql({bar: 'a2'});
273
+ expect(result[1]).to.be.eql({bar: 'b2'});
274
+ expect(result[2]).to.be.eql({bar: 'c2'});
275
+ });
276
+
277
+ it('includes the primary key of the given object', function () {
278
+ const entities = [
279
+ {[DEF_PK]: 1, foo: 'a1', bar: 'a2', baz: 'a3'},
280
+ {[DEF_PK]: 2, foo: 'b1', bar: 'b2', baz: 'b3'},
281
+ {[DEF_PK]: 3, foo: 'c1', bar: 'c2', baz: 'c3'},
282
+ ];
283
+ const result = T.filter(entities, MODEL_NAME, 'bar');
284
+ expect(result).to.have.lengthOf(3);
285
+ expect(result[0]).to.be.eql({[DEF_PK]: 1, bar: 'a2'});
286
+ expect(result[1]).to.be.eql({[DEF_PK]: 2, bar: 'b2'});
287
+ expect(result[2]).to.be.eql({[DEF_PK]: 3, bar: 'c2'});
288
+ });
289
+ });
290
+
291
+ describe('multiple fields', function () {
292
+ it('does not throw an error if multiple fields is not exist', function () {
293
+ const object = [
294
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
295
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
296
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
297
+ ];
298
+ const res = T.filter(object, MODEL_NAME, ['bar', 'qux']);
299
+ expect(res[0]).to.be.eql({bar: 'a2'});
300
+ expect(res[1]).to.be.eql({bar: 'b2'});
301
+ expect(res[2]).to.be.eql({bar: 'c2'});
302
+ });
303
+
304
+ it('requires the first argument to be an array of objects', function () {
305
+ const throwable = v => () => T.filter(v, MODEL_NAME, ['bar', 'baz']);
306
+ const error = v =>
307
+ format(
308
+ 'The first argument of FieldsClauseTool.filter should be an Object or ' +
309
+ 'an Array of Object, but %s given.',
310
+ v,
311
+ );
312
+ expect(throwable(['str'])).to.throw(error('"str"'));
313
+ expect(throwable([''])).to.throw(error('""'));
314
+ expect(throwable([10])).to.throw(error('10'));
315
+ expect(throwable([0])).to.throw(error('0'));
316
+ expect(throwable([true])).to.throw(error('true'));
317
+ expect(throwable([false])).to.throw(error('false'));
318
+ expect(throwable([undefined])).to.throw(error('undefined'));
319
+ expect(throwable([null])).to.throw(error('null'));
320
+ expect(throwable([{foo: 'a1', bar: 'a2'}])()).to.be.eql([
321
+ {bar: 'a2'},
322
+ ]);
323
+ expect(throwable([{}])()).to.be.eql([{}]);
324
+ });
325
+
326
+ it('requires the second argument to be a non-empty string', function () {
327
+ const entities = [
328
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
329
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
330
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
331
+ ];
332
+ const throwable = v => () => T.filter(entities, v, ['bar', 'baz']);
333
+ const error = v =>
334
+ format(
335
+ 'The second argument of FieldsClauseTool.filter should be ' +
336
+ 'a non-empty String, but %s given.',
337
+ v,
338
+ );
339
+ expect(throwable('')).to.throw(error('""'));
340
+ expect(throwable(10)).to.throw(error('10'));
341
+ expect(throwable(0)).to.throw(error('0'));
342
+ expect(throwable(true)).to.throw(error('true'));
343
+ expect(throwable(false)).to.throw(error('false'));
344
+ expect(throwable(undefined)).to.throw(error('undefined'));
345
+ expect(throwable(null)).to.throw(error('null'));
346
+ expect(throwable('model')()).to.be.eql([
347
+ {bar: 'a2', baz: 'a3'},
348
+ {bar: 'b2', baz: 'b3'},
349
+ {bar: 'c2', baz: 'c3'},
350
+ ]);
351
+ });
352
+
353
+ it('requires the third argument to be a non-empty string', function () {
354
+ const entities = [
355
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
356
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
357
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
358
+ ];
359
+ const throwable = v => () => T.filter(entities, MODEL_NAME, v);
360
+ const error = v =>
361
+ format(
362
+ 'The provided option "fields" should be a non-empty String ' +
363
+ 'or an Array of non-empty String, but %s given.',
364
+ v,
365
+ );
366
+ expect(throwable([''])).to.throw(error('""'));
367
+ expect(throwable([10])).to.throw(error('10'));
368
+ expect(throwable([0])).to.throw(error('0'));
369
+ expect(throwable([true])).to.throw(error('true'));
370
+ expect(throwable([false])).to.throw(error('false'));
371
+ expect(throwable([{}])).to.throw(error('Object'));
372
+ expect(throwable([undefined])).to.throw(error('undefined'));
373
+ expect(throwable([null])).to.throw(error('null'));
374
+ expect(throwable(['bar', 'baz'])()).to.be.eql([
375
+ {bar: 'a2', baz: 'a3'},
376
+ {bar: 'b2', baz: 'b3'},
377
+ {bar: 'c2', baz: 'c3'},
378
+ ]);
379
+ });
380
+
381
+ it('picks field of the given object', function () {
382
+ const entities = [
383
+ {foo: 'a1', bar: 'a2', baz: 'a3'},
384
+ {foo: 'b1', bar: 'b2', baz: 'b3'},
385
+ {foo: 'c1', bar: 'c2', baz: 'c3'},
386
+ ];
387
+ const result = T.filter(entities, MODEL_NAME, ['bar', 'baz']);
388
+ expect(result).to.have.lengthOf(3);
389
+ expect(result[0]).to.be.eql({bar: 'a2', baz: 'a3'});
390
+ expect(result[1]).to.be.eql({bar: 'b2', baz: 'b3'});
391
+ expect(result[2]).to.be.eql({bar: 'c2', baz: 'c3'});
392
+ });
393
+
394
+ it('includes the primary key of the given object', function () {
395
+ const entities = [
396
+ {[DEF_PK]: 1, foo: 'a1', bar: 'a2', baz: 'a3'},
397
+ {[DEF_PK]: 2, foo: 'b1', bar: 'b2', baz: 'b3'},
398
+ {[DEF_PK]: 3, foo: 'c1', bar: 'c2', baz: 'c3'},
399
+ ];
400
+ const result = T.filter(entities, MODEL_NAME, ['bar', 'baz']);
401
+ expect(result).to.have.lengthOf(3);
402
+ expect(result[0]).to.be.eql({[DEF_PK]: 1, bar: 'a2', baz: 'a3'});
403
+ expect(result[1]).to.be.eql({[DEF_PK]: 2, bar: 'b2', baz: 'b3'});
404
+ expect(result[2]).to.be.eql({[DEF_PK]: 3, bar: 'c2', baz: 'c3'});
405
+ });
406
+ });
71
407
  });
72
408
  });
73
409
 
74
410
  describe('validateFieldsClause', function () {
75
- it('requires a non-empty string or an array of non-empty strings', function () {
76
- const validate = v => () => FieldsClauseTool.validateFieldsClause(v);
77
- const error = v =>
78
- format(
79
- 'The provided option "fields" should be a non-empty String ' +
80
- 'or an Array of String, but %s given.',
81
- v,
82
- );
83
- expect(validate(10)).to.throw(error('10'));
84
- expect(validate(true)).to.throw(error('true'));
85
- expect(validate({})).to.throw(error('Object'));
86
- expect(validate([''])).to.throw(error('""'));
87
- expect(validate([10])).to.throw(error('10'));
88
- expect(validate([true])).to.throw(error('true'));
89
- expect(validate([false])).to.throw(error('false'));
90
- expect(validate([undefined])).to.throw(error('undefined'));
91
- expect(validate([null])).to.throw(error('null'));
92
- validate('')();
93
- validate(false)();
94
- validate(undefined)();
95
- validate(null)();
96
- validate('foo')();
97
- validate(['foo'])();
411
+ describe('single field', function () {
412
+ it('requires the first argument to be a non-empty string', function () {
413
+ const throwable = v => () => FieldsClauseTool.validateFieldsClause(v);
414
+ const error = v =>
415
+ format(
416
+ 'The provided option "fields" should be a non-empty String ' +
417
+ 'or an Array of non-empty String, but %s given.',
418
+ v,
419
+ );
420
+ expect(throwable('')).to.throw(error('""'));
421
+ expect(throwable(10)).to.throw(error('10'));
422
+ expect(throwable(0)).to.throw(error('0'));
423
+ expect(throwable(true)).to.throw(error('true'));
424
+ expect(throwable(false)).to.throw(error('false'));
425
+ expect(throwable({})).to.throw(error('Object'));
426
+ throwable('field')();
427
+ throwable(undefined)();
428
+ throwable(null)();
429
+ });
430
+ });
431
+
432
+ describe('multiple fields', function () {
433
+ it('requires the first argument to be an array of non-empty strings', function () {
434
+ const throwable = v => () => FieldsClauseTool.validateFieldsClause(v);
435
+ const error = v =>
436
+ format(
437
+ 'The provided option "fields" should be a non-empty String ' +
438
+ 'or an Array of non-empty String, but %s given.',
439
+ v,
440
+ );
441
+ expect(throwable([''])).to.throw(error('""'));
442
+ expect(throwable([10])).to.throw(error('10'));
443
+ expect(throwable([0])).to.throw(error('0'));
444
+ expect(throwable([true])).to.throw(error('true'));
445
+ expect(throwable([false])).to.throw(error('false'));
446
+ expect(throwable([{}])).to.throw(error('Object'));
447
+ expect(throwable([undefined])).to.throw(error('undefined'));
448
+ expect(throwable([null])).to.throw(error('null'));
449
+ throwable(['field'])();
450
+ throwable([])();
451
+ });
98
452
  });
99
453
  });
100
454
 
101
455
  describe('normalizeFieldsClause', function () {
102
- it('returns an array of strings', function () {
103
- const fn = FieldsClauseTool.normalizeFieldsClause;
104
- expect(fn('foo')).to.be.eql(['foo']);
105
- expect(fn(['foo'])).to.be.eql(['foo']);
456
+ describe('single field', function () {
457
+ it('requires the first argument to be a non-empty string', function () {
458
+ const throwable = clause => () =>
459
+ FieldsClauseTool.normalizeFieldsClause(clause);
460
+ const error = v =>
461
+ format(
462
+ 'The provided option "fields" should be a non-empty String ' +
463
+ 'or an Array of non-empty String, but %s given.',
464
+ v,
465
+ );
466
+ expect(throwable('')).to.throw(error('""'));
467
+ expect(throwable(10)).to.throw(error('10'));
468
+ expect(throwable(0)).to.throw(error('0'));
469
+ expect(throwable(true)).to.throw(error('true'));
470
+ expect(throwable(false)).to.throw(error('false'));
471
+ expect(throwable({})).to.throw(error('Object'));
472
+ expect(throwable('field')()).to.be.eql(['field']);
473
+ expect(throwable(undefined)()).to.be.undefined;
474
+ expect(throwable(null)()).to.be.undefined;
475
+ });
476
+
477
+ it('returns an array of strings', function () {
478
+ const fn = FieldsClauseTool.normalizeFieldsClause;
479
+ expect(fn('foo')).to.be.eql(['foo']);
480
+ });
106
481
  });
107
482
 
108
- it('requires a non-empty string or an array of non-empty strings', function () {
109
- const fn = clause => () => FieldsClauseTool.normalizeFieldsClause(clause);
110
- const error = v =>
111
- format(
112
- 'The provided option "fields" should be a non-empty String ' +
113
- 'or an Array of String, but %s given.',
114
- v,
115
- );
116
- expect(fn(10)).to.throw(error('10'));
117
- expect(fn(true)).to.throw(error('true'));
118
- expect(fn({})).to.throw(error('Object'));
119
- expect(fn([''])).to.throw(error('""'));
120
- expect(fn([10])).to.throw(error('10'));
121
- expect(fn([true])).to.throw(error('true'));
122
- expect(fn([false])).to.throw(error('false'));
123
- expect(fn([undefined])).to.throw(error('undefined'));
124
- expect(fn([null])).to.throw(error('null'));
125
- expect(fn('')()).to.be.undefined;
126
- expect(fn(false)()).to.be.undefined;
127
- expect(fn(undefined)()).to.be.undefined;
128
- expect(fn(null)()).to.be.undefined;
129
- expect(fn('foo')()).to.be.eql(['foo']);
130
- expect(fn(['foo'])()).to.be.eql(['foo']);
483
+ describe('multiple fields', function () {
484
+ it('requires the first argument to be an array of non-empty strings', function () {
485
+ const throwable = clause => () =>
486
+ FieldsClauseTool.normalizeFieldsClause(clause);
487
+ const error = v =>
488
+ format(
489
+ 'The provided option "fields" should be a non-empty String ' +
490
+ 'or an Array of non-empty String, but %s given.',
491
+ v,
492
+ );
493
+ expect(throwable([''])).to.throw(error('""'));
494
+ expect(throwable([10])).to.throw(error('10'));
495
+ expect(throwable([0])).to.throw(error('0'));
496
+ expect(throwable([true])).to.throw(error('true'));
497
+ expect(throwable([false])).to.throw(error('false'));
498
+ expect(throwable([{}])).to.throw(error('Object'));
499
+ expect(throwable([undefined])).to.throw(error('undefined'));
500
+ expect(throwable([null])).to.throw(error('null'));
501
+ expect(throwable(['field'])()).to.be.eql(['field']);
502
+ expect(throwable([])()).to.be.undefined;
503
+ });
504
+
505
+ it('returns an array of strings', function () {
506
+ const fn = FieldsClauseTool.normalizeFieldsClause;
507
+ expect(fn(['foo'])).to.be.eql(['foo']);
508
+ expect(fn(['foo', 'bar'])).to.be.eql(['foo', 'bar']);
509
+ });
131
510
  });
132
511
  });
133
512
  });