1 | import { formatDecisionRule, formatProperty, OPERATORS, reduceDecisionRule, TYPES } from '../src/properties';
|
2 |
|
3 | describe('Properties', () => {
|
4 | describe('.formatProperty(<property_type>)', () => {
|
5 | describe(TYPES.time_of_day, () => {
|
6 | const formatter = formatProperty(TYPES.time_of_day);
|
7 |
|
8 | it('Works properly on property values', () => {
|
9 | expect(formatter(11.5)).to.be.equal('11:30');
|
10 | expect(formatter(11.008)).to.be.equal('11:00:29');
|
11 | });
|
12 | });
|
13 |
|
14 | describe(TYPES.enum, () => {
|
15 | const formatter = formatProperty(TYPES.enum);
|
16 | it('Works properly on property values', () => {
|
17 | expect(formatter('toto')).to.be.equal('toto');
|
18 | });
|
19 | });
|
20 |
|
21 | describe(TYPES.continuous, () => {
|
22 | const formatter = formatProperty(TYPES.continuous);
|
23 | it('Works properly on property values', () => {
|
24 | expect(formatter(12.4)).to.be.equal('12.4');
|
25 | expect(formatter(12.4234)).to.be.equal('12.42');
|
26 | });
|
27 | });
|
28 |
|
29 | describe(TYPES.month_of_year, () => {
|
30 | const formatter = formatProperty(TYPES.month_of_year);
|
31 | it('Works properly on property values', () => {
|
32 | expect(formatter(1)).to.be.equal('Jan');
|
33 | expect(formatter(6)).to.be.equal('Jun');
|
34 | expect(formatter(12)).to.be.equal('Dec');
|
35 | });
|
36 | });
|
37 | });
|
38 |
|
39 | describe('.formatDecisionRule(<decision_rule>)', () => {
|
40 | describe(OPERATORS.IN, () => {
|
41 | describe(TYPES.time_of_day, () => {
|
42 | it('Works properly', () => {
|
43 | expect(formatDecisionRule({
|
44 | type: TYPES.time_of_day,
|
45 | operator: OPERATORS.IN,
|
46 | operand: [11.5, 12.3]
|
47 | })).to.be.equal('[11:30, 12:18[');
|
48 | });
|
49 | });
|
50 |
|
51 | describe(TYPES.continuous, () => {
|
52 | it('Works properly', () => {
|
53 | expect(formatDecisionRule({
|
54 | type: TYPES.continuous,
|
55 | operator: OPERATORS.IN,
|
56 | operand: [11.5, 12.3]
|
57 | })).to.be.equal('[11.5, 12.3[');
|
58 | });
|
59 | });
|
60 |
|
61 | describe(TYPES.day_of_week, () => {
|
62 | it('Works properly, when the days are ordered', () => {
|
63 | expect(formatDecisionRule({
|
64 | type: TYPES.day_of_week,
|
65 | operator: OPERATORS.IN,
|
66 | operand: [3, 5]
|
67 | })).to.be.equal('Thu to Fri');
|
68 | });
|
69 |
|
70 | it('Works properly, when the days are out of order', () => {
|
71 | expect(formatDecisionRule({
|
72 | type: TYPES.day_of_week,
|
73 | operator: OPERATORS.IN,
|
74 | operand: [4, 0]
|
75 | })).to.be.equal('Fri to Sun');
|
76 | });
|
77 | });
|
78 |
|
79 | describe(TYPES.month_of_year, () => {
|
80 | it('Works properly, when the months are ordered', () => {
|
81 | expect(formatDecisionRule({
|
82 | type: TYPES.month_of_year,
|
83 | operator: OPERATORS.IN,
|
84 | operand: [1, 12]
|
85 | })).to.be.equal('Jan to Nov');
|
86 | });
|
87 |
|
88 | it('Works properly, when the months are out of order', () => {
|
89 | expect(formatDecisionRule({
|
90 | type: TYPES.month_of_year,
|
91 | operator: OPERATORS.IN,
|
92 | operand: [4, 2]
|
93 | })).to.be.equal('Apr to Jan');
|
94 | expect(formatDecisionRule({
|
95 | type: TYPES.month_of_year,
|
96 | operator: OPERATORS.IN,
|
97 | operand: [5, 1]
|
98 | })).to.be.equal('May to Dec');
|
99 | });
|
100 | });
|
101 | });
|
102 |
|
103 | describe(OPERATORS.GTE, () => {
|
104 | describe(TYPES.continuous, () => {
|
105 | it('Works properly', () => {
|
106 | expect(formatDecisionRule({
|
107 | type: TYPES.continuous,
|
108 | operator: OPERATORS.GTE,
|
109 | operand: 3.14
|
110 | })).to.be.equal('>= 3.14');
|
111 | });
|
112 | });
|
113 |
|
114 | describe(TYPES.enum, () => {
|
115 | it('Works properly', () => {
|
116 | expect(formatDecisionRule({
|
117 | type: TYPES.enum,
|
118 | operator: OPERATORS.GTE,
|
119 | operand: 'foo'
|
120 | })).to.be.equal('>= foo');
|
121 | });
|
122 | });
|
123 | });
|
124 |
|
125 | describe(OPERATORS.LT, () => {
|
126 | describe(TYPES.continuous, () => {
|
127 | it('Works properly', () => {
|
128 | expect(formatDecisionRule({
|
129 | type: TYPES.continuous,
|
130 | operator: OPERATORS.LT,
|
131 | operand: 666
|
132 | })).to.be.equal('< 666');
|
133 | });
|
134 | });
|
135 |
|
136 | describe(TYPES.timezone, () => {
|
137 | it('Works properly', () => {
|
138 | expect(formatDecisionRule({
|
139 | type: TYPES.timezone,
|
140 | operator: OPERATORS.LT,
|
141 | operand: '+02:00'
|
142 | })).to.be.equal('< +02:00');
|
143 | });
|
144 | });
|
145 | });
|
146 |
|
147 | describe(OPERATORS.IS, () => {
|
148 | describe(TYPES.continuous, () => {
|
149 | it('Works properly', () => {
|
150 | expect(formatDecisionRule({
|
151 | type: TYPES.continuous,
|
152 | operator: OPERATORS.IS,
|
153 | operand: 5637
|
154 | })).to.be.equal('is 5637');
|
155 | });
|
156 | });
|
157 |
|
158 | describe(TYPES.enum, () => {
|
159 | it('Works properly', () => {
|
160 | expect(formatDecisionRule({
|
161 | type: TYPES.enum,
|
162 | operator: OPERATORS.IS,
|
163 | operand: 'abracadabra'
|
164 | })).to.be.equal('is abracadabra');
|
165 | });
|
166 | });
|
167 | });
|
168 | });
|
169 |
|
170 | describe('.reduceDecisionRule(<decision_rules>)', () => {
|
171 | it('Reduce "is" properties, same operand', () => {
|
172 | const decisionRules = [
|
173 | { operator: 'is', operand: 'toto' },
|
174 | { operator: 'is', operand: 'toto' }
|
175 | ];
|
176 | const output = reduceDecisionRule(decisionRules);
|
177 | expect(output.operator).to.be.equal('is');
|
178 | expect(output.operand).to.be.equal('toto');
|
179 | });
|
180 |
|
181 | it('Reduce "is" properties, only one element', () => {
|
182 | const decisionRules = [
|
183 | { operator: 'is', operand: 'toto' }
|
184 | ];
|
185 | const output = reduceDecisionRule(decisionRules);
|
186 | expect(output.operator).to.be.equal('is');
|
187 | expect(output.operand).to.be.equal('toto');
|
188 | });
|
189 |
|
190 | it('Reduce "is" properties, different operator', () => {
|
191 | const decisionRules = [
|
192 | { operator: 'is', operand: 'toto' },
|
193 | { operator: '[in[', operand: 'titi' }
|
194 | ];
|
195 | expect(() => reduceDecisionRule(decisionRules)).to.throw('Unable to reduce decision rules \'is toto\' and \'[t, i[\': incompatible operators.');
|
196 | });
|
197 |
|
198 | it('Reduce "is" properties, different operand', () => {
|
199 | const decisionRules = [
|
200 | { operator: 'is', operand: 'toto' },
|
201 | { operator: 'is', operand: 'titi' }
|
202 | ];
|
203 | expect(() => reduceDecisionRule(decisionRules)).to.throw('Operator "is" can\'t have different value. Set to "toto" and receive "titi"');
|
204 | });
|
205 |
|
206 | it('Reduce "in" properties, equal last element', () => {
|
207 | const decisionRules = [
|
208 | { operator: '[in[', operand: [0, 13] },
|
209 | { operator: '[in[', operand: [2, 12] }
|
210 | ];
|
211 | const output = reduceDecisionRule(decisionRules);
|
212 | expect(output.operator).to.be.equal('[in[');
|
213 | expect(output.operand).to.be.deep.equal([2, 12]);
|
214 | });
|
215 |
|
216 | it('Reduce "in" properties 2, update with last element lower limit', () => {
|
217 | const decisionRules = [
|
218 | { operator: '[in[', operand: [0, 13] },
|
219 | { operator: '[in[', operand: [2, 16] }
|
220 | ];
|
221 | const output = reduceDecisionRule(decisionRules);
|
222 | expect(output.operator).to.be.equal('[in[');
|
223 | expect(output.operand).to.be.deep.equal([2, 13]);
|
224 | });
|
225 |
|
226 | it('Reduce "in" properties, update with last element upper limit', () => {
|
227 | const decisionRules = [
|
228 | { operator: '[in[', operand: [1, 13] },
|
229 | { operator: '[in[', operand: [0, 12] }
|
230 | ];
|
231 | const output = reduceDecisionRule(decisionRules);
|
232 | expect(output.operator).to.be.equal('[in[');
|
233 | expect(output.operand).to.be.deep.equal([1, 12]);
|
234 | });
|
235 |
|
236 | it('Reduce "in" properties, days of week - Sat to Wed && Wed', () => {
|
237 | const decisionRules = [
|
238 | { operator: '[in[', operand: [5, 3] },
|
239 | { operator: '[in[', operand: [2, 3] }
|
240 | ];
|
241 | const output = reduceDecisionRule(decisionRules);
|
242 | expect(output.operator).to.be.equal('[in[');
|
243 | expect(output.operand).to.be.deep.equal([2, 3]);
|
244 | });
|
245 |
|
246 | it('Reduce "in" properties, days of month - [5, 4[ && [12, 1[ && [12, 16[', () => {
|
247 | const decisionRules = [
|
248 | { operator: '[in[', operand: [5, 4] },
|
249 | { operator: '[in[', operand: [12, 1] },
|
250 | { operator: '[in[', operand: [12, 16] }
|
251 | ];
|
252 | const output = reduceDecisionRule(decisionRules);
|
253 | expect(output.operator).to.be.equal('[in[');
|
254 | expect(output.operand).to.be.deep.equal([12, 16]);
|
255 | });
|
256 |
|
257 | it('Reduce "in" properties, with itself', () => {
|
258 | const decisionRules = [
|
259 | { operator: '[in[', operand: [3, 4] },
|
260 | { operator: '[in[', operand: [3, 4] }
|
261 | ];
|
262 | const output = reduceDecisionRule(decisionRules);
|
263 | expect(output.operator).to.be.equal('[in[');
|
264 | expect(output.operand).to.be.deep.equal([3, 4]);
|
265 | });
|
266 |
|
267 | it('Reduce "in" properties, with itself, cyclic', () => {
|
268 | const decisionRules = [
|
269 | { operator: '[in[', operand: [4, 2] },
|
270 | { operator: '[in[', operand: [4, 2] }
|
271 | ];
|
272 | const output = reduceDecisionRule(decisionRules);
|
273 | expect(output.operator).to.be.equal('[in[');
|
274 | expect(output.operand).to.be.deep.equal([4, 2]);
|
275 | });
|
276 |
|
277 | it('Reduce "in" properties, days of month - invalid set cyclic and non-cyclic', () => {
|
278 | const decisionRules = [
|
279 | { operator: '[in[', operand: [15, 20] },
|
280 | { operator: '[in[', operand: [22, 14] }
|
281 | ];
|
282 | expect(() => reduceDecisionRule(decisionRules)).to.throw('Unable to reduce decision rules \'[15, 20[\' and \'[22, 14[\': the resulting rule is not fulfillable.');
|
283 | });
|
284 |
|
285 | it('Reduce "in" properties, days of month - invalid set non-cyclic and non-cyclic', () => {
|
286 | const decisionRules = [
|
287 | { operator: '[in[', operand: [15, 20] },
|
288 | { operator: '[in[', operand: [22, 25] }
|
289 | ];
|
290 | expect(() => reduceDecisionRule(decisionRules)).to.throw('Unable to reduce decision rules \'[15, 20[\' and \'[22, 25[\': the resulting rule is not fulfillable.');
|
291 | });
|
292 |
|
293 | it('Reduce "in" properties, only one element', () => {
|
294 | const decisionRules = [
|
295 | { operator: '[in[', operand: [1, 13] }
|
296 | ];
|
297 | const output = reduceDecisionRule(decisionRules);
|
298 | expect(output.operator).to.be.equal('[in[');
|
299 | expect(output.operand).to.be.deep.equal([1, 13]);
|
300 | });
|
301 |
|
302 | it('Reduce "in" properties, operator is', () => {
|
303 | const decisionRules = [
|
304 | { operator: '[in[', operand: [1, 13] },
|
305 | { operator: 'is', operand: 'toto' }
|
306 | ];
|
307 | expect(() => reduceDecisionRule(decisionRules)).to.throw('Unable to reduce decision rules \'[1, 13[\' and \'is toto\': incompatible operators.');
|
308 | });
|
309 |
|
310 | it('Reduce "in" properties, operator <', () => {
|
311 | const decisionRules = [
|
312 | { operator: '[in[', operand: [1, 13] },
|
313 | { operator: '<', operand: 2 }
|
314 | ];
|
315 | const output = reduceDecisionRule(decisionRules);
|
316 | expect(output.operator).to.be.equal('[in[');
|
317 | expect(output.operand).to.be.deep.equal([1, 2]);
|
318 | });
|
319 |
|
320 | it('Reduce "in" properties, operator >=', () => {
|
321 | const decisionRules = [
|
322 | { operator: '[in[', operand: [1, 13] },
|
323 | { operator: '>=', operand: 12 }
|
324 | ];
|
325 | const output = reduceDecisionRule(decisionRules);
|
326 | expect(output.operator).to.be.equal('[in[');
|
327 | expect(output.operand).to.be.deep.equal([12, 13]);
|
328 | });
|
329 |
|
330 | it('Reduce "in" properties, operator < and >=', () => {
|
331 | const decisionRules = [
|
332 | { operator: '[in[', operand: [1, 13] },
|
333 | { operator: '<', operand: 12 },
|
334 | { operator: '>=', operand: 2 }
|
335 | ];
|
336 | const output = reduceDecisionRule(decisionRules);
|
337 | expect(output.operator).to.be.equal('[in[');
|
338 | expect(output.operand).to.be.deep.equal([2, 12]);
|
339 | });
|
340 |
|
341 | it('Reduce "in" properties, periodic time periods', () => {
|
342 | const decisionRules = [
|
343 | { operator: '[in[', operand: [23, 3] },
|
344 | { operator: '[in[', operand: [22, 2] }
|
345 | ];
|
346 | const output = reduceDecisionRule(decisionRules);
|
347 | expect(output.operator).to.be.equal('[in[');
|
348 | expect(output.operand).to.be.deep.equal([23, 2]);
|
349 | });
|
350 |
|
351 | it('Reduce "<" properties', () => {
|
352 | const decisionRules = [
|
353 | { operator: '<', operand: 2 },
|
354 | { operator: '<', operand: 6 }
|
355 | ];
|
356 | const output = reduceDecisionRule(decisionRules);
|
357 | expect(output.operator).to.be.equal('<');
|
358 | expect(output.operand).to.be.equal(2);
|
359 | });
|
360 |
|
361 | it('Reduce "<" properties, with itself', () => {
|
362 | const decisionRules = [
|
363 | { operator: '<', operand: 2 },
|
364 | { operator: '<', operand: 2 }
|
365 | ];
|
366 | const output = reduceDecisionRule(decisionRules);
|
367 | expect(output.operator).to.be.equal('<');
|
368 | expect(output.operand).to.be.equal(2);
|
369 | });
|
370 |
|
371 | it('Reduce "<" properties, only one parameter', () => {
|
372 | const decisionRules = [
|
373 | { operator: '<', operand: 2 }
|
374 | ];
|
375 | const output = reduceDecisionRule(decisionRules);
|
376 | expect(output.operator).to.be.equal('<');
|
377 | expect(output.operand).to.be.equal(2);
|
378 | });
|
379 |
|
380 | it('Reduce "<" properties, with [in[ operator', () => {
|
381 | const decisionRules = [
|
382 | { operator: '<', operand: 2 },
|
383 | { operator: '[in[', operand: [1, 13] }
|
384 | ];
|
385 | const output = reduceDecisionRule(decisionRules);
|
386 | expect(output.operator).to.be.equal('[in[');
|
387 | expect(output.operand).to.be.deep.equal([1, 2]);
|
388 | });
|
389 |
|
390 | it('Reduce "<" properties, with >= operator', () => {
|
391 | const decisionRules = [
|
392 | { operator: '<', operand: 13 },
|
393 | { operator: '>=', operand: 2 }
|
394 | ];
|
395 | const output = reduceDecisionRule(decisionRules);
|
396 | expect(output.operator).to.be.equal('[in[');
|
397 | expect(output.operand).to.be.deep.equal([2, 13]);
|
398 | });
|
399 |
|
400 | it('Reduce "<" and ">=" properties, <650 && >=232.82 && <251.99 && <345.22', () => {
|
401 | const decisionRules = [
|
402 | { operator: '<', operand: 650 },
|
403 | { operator: '>=', operand: 232.82 },
|
404 | { operator: '<', operand: 251.99 },
|
405 | { operator: '<', operand: 345.22 }
|
406 | ];
|
407 | const output = reduceDecisionRule(decisionRules);
|
408 | expect(output.operator).to.be.equal('[in[');
|
409 | expect(output.operand).to.be.deep.equal([232.82, 251.99]);
|
410 | });
|
411 |
|
412 | it('Reduce ">=" properties', () => {
|
413 | const decisionRules = [
|
414 | { operator: '>=', operand: 4 },
|
415 | { operator: '>=', operand: 2 }
|
416 | ];
|
417 | const output = reduceDecisionRule(decisionRules);
|
418 | expect(output.operator).to.be.equal('>=');
|
419 | expect(output.operand).to.be.equal(4);
|
420 | });
|
421 |
|
422 |
|
423 | it('Reduce ">=" properties, only one parameter', () => {
|
424 | const decisionRules = [
|
425 | { operator: '>=', operand: 2 }
|
426 | ];
|
427 | const output = reduceDecisionRule(decisionRules);
|
428 | expect(output.operator).to.be.equal('>=');
|
429 | expect(output.operand).to.be.equal(2);
|
430 | });
|
431 |
|
432 | it('Reduce ">=" properties, with itself', () => {
|
433 | const decisionRules = [
|
434 | { operator: '>=', operand: 2 },
|
435 | { operator: '>=', operand: 2 }
|
436 | ];
|
437 | const output = reduceDecisionRule(decisionRules);
|
438 | expect(output.operator).to.be.equal('>=');
|
439 | expect(output.operand).to.be.equal(2);
|
440 | });
|
441 |
|
442 | it('Reduce ">=" properties, with [in[ operator', () => {
|
443 | const decisionRules = [
|
444 | { operator: '>=', operand: 2 },
|
445 | { operator: '[in[', operand: [1, 13] }
|
446 | ];
|
447 | const output = reduceDecisionRule(decisionRules);
|
448 | expect(output.operator).to.be.equal('[in[');
|
449 | expect(output.operand).to.be.deep.equal([2, 13]);
|
450 | });
|
451 |
|
452 | it('Reduce ">=" properties, with < operator', () => {
|
453 | const decisionRules = [
|
454 | { operator: '>=', operand: 2 },
|
455 | { operator: '<', operand: 13 }
|
456 | ];
|
457 | const output = reduceDecisionRule(decisionRules);
|
458 | expect(output.operator).to.be.equal('[in[');
|
459 | expect(output.operand).to.be.deep.equal([2, 13]);
|
460 | });
|
461 |
|
462 | it('Reduce ">=" properties, with < operator, in a bad order', () => {
|
463 | const decisionRules = [
|
464 | { operator: '>=', operand: 13 },
|
465 | { operator: '<', operand: 2 }
|
466 | ];
|
467 | expect(() => reduceDecisionRule(decisionRules)).to.throw('Unable to reduce decision rules \'>= 13\' and \'< 2\': the resulting rule is not fulfillable.');
|
468 | });
|
469 | });
|
470 | });
|