forked from primeroIMS/primero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-expression.unit.test.js
229 lines (179 loc) · 7.31 KB
/
parse-expression.unit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
import { COMPARISON_OPERATORS, LOGICAL_OPERATORS } from "./constants";
import parseExpression from "./parse-expression";
describe("parseExpression", () => {
context("when is a eq expression", () => {
const expression = parseExpression({ eq: { sex: "male" } });
it("returns a eq expression", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.EQ);
});
it("evaluates to true", () => {
expect(expression.evaluate({ sex: "male" })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ sex: "female" })).to.be.false;
});
});
context("when is a ge expression", () => {
const expression = parseExpression({ ge: { age: 5 } });
it("returns a ge expression", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.GE);
});
it("evaluates to true", () => {
expect(expression.evaluate({ age: 5 })).to.be.true;
expect(expression.evaluate({ age: 10 })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.GE);
expect(expression.evaluate({ age: 4 })).to.be.false;
});
});
context("when is a gt expression", () => {
const expression = parseExpression({ gt: { age: 5 } });
it("returns a gt expression", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.GT);
});
it("evaluates to true", () => {
expect(expression.evaluate({ age: 10 })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ age: 4 })).to.be.false;
expect(expression.evaluate({ age: 5 })).to.be.false;
});
});
context("when is a le expression", () => {
const expression = parseExpression({ le: { age: 10 } });
it("returns lt in expression", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.LE);
});
it("evaluates to true", () => {
expect(expression.evaluate({ age: 10 })).to.be.true;
expect(expression.evaluate({ age: 5 })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ age: 12 })).to.be.false;
});
});
context("when is a lt expression", () => {
const expression = parseExpression({ lt: { age: 10 } });
it("returns lt in expression", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.LT);
});
it("evaluates to true", () => {
expect(expression.evaluate({ age: 8 })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ age: 10 })).to.be.false;
expect(expression.evaluate({ age: 12 })).to.be.false;
});
});
context("when is a in expression", () => {
const expression = parseExpression({ in: { sex: ["male", "female"] } });
it("returns an in expression", () => {
expect(expression.operator).to.equal(COMPARISON_OPERATORS.IN);
});
it("evaluates to true", () => {
expect(expression.evaluate({ sex: "male" })).to.be.true;
expect(expression.evaluate({ sex: "female" })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ sex: "value1" })).to.be.false;
});
});
context("when is an and expression", () => {
const expression = parseExpression({ and: [{ eq: { sex: "male" } }, { eq: { age: 5 } }] });
it("returns an and expression", () => {
expect(expression.operator).to.equal(LOGICAL_OPERATORS.AND);
expect(expression.expressions.map(nested => nested.operator)).to.deep.equal([
COMPARISON_OPERATORS.EQ,
COMPARISON_OPERATORS.EQ
]);
});
it("evaluates to true", () => {
expect(expression.evaluate({ sex: "male", age: 5 })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ sex: "male", age: 10 })).to.be.false;
expect(expression.evaluate({ sex: "female", age: 5 })).to.be.false;
});
});
context("when is an or expression", () => {
const expression = parseExpression({ or: [{ eq: { sex: "male" } }, { eq: { age: 5 } }] });
it("returns an or expression", () => {
expect(expression.operator).to.equal(LOGICAL_OPERATORS.OR);
expect(expression.expressions.map(nested => nested.operator)).to.deep.equal([
COMPARISON_OPERATORS.EQ,
COMPARISON_OPERATORS.EQ
]);
});
it("evaluates to true", () => {
expect(expression.evaluate({ sex: "male", age: 10 })).to.be.true;
expect(expression.evaluate({ sex: "female", age: 5 })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ sex: "female", age: 10 })).to.be.false;
});
});
context("when is not expression", () => {
const expression = parseExpression({ not: { eq: { sex: "male" } } });
it("returns an not expression", () => {
expect(expression.operator).to.equal(LOGICAL_OPERATORS.NOT);
expect(expression.expression.operator).to.equal(COMPARISON_OPERATORS.EQ);
});
it("evaluates to true", () => {
expect(expression.evaluate({ sex: "female" })).to.be.true;
});
it("evaluates to false", () => {
expect(expression.evaluate({ sex: "male" })).to.be.false;
});
});
context("when is a deprecated syntax", () => {
const expression = parseExpression([{ sex: "male" }]);
it("correctly evaluates the expression to be true", () => {
expect(expression.evaluate({ sex: "male" })).to.be.true;
});
it("correctly evaluates the expression to be false", () => {
expect(expression.evaluate({ sex: "female" })).to.be.false;
});
});
context("when is a sum expression", () => {
const expression = parseExpression({ sum: ["a", "b", "c"] });
it("correctly evaluates the sum", () => {
expect(expression.evaluate({ a: 2, b: 3 })).to.deep.equals(5);
});
it("returns 0 when wrong arguments are passed", () => {
expect(expression.evaluate({ d: 2, e: 3 })).to.deep.equals(0);
});
});
context("when sum is nested", () => {
const expression = parseExpression({ sum: [{ sum: ["a", "b", "c"] }, "d"] });
it("expression is nested", () => {
expect(expression.evaluate({ a: 1, b: 2, c: 3, d: 4 })).to.deep.equal(10);
});
});
context("avgOperator", () => {
const operator = parseExpression({ avg: ["a", "b", "c"] });
const decimalOperator = parseExpression({ avg: { data: ["a", "b", "c"], extra: { decimalPlaces: 3 } } });
it("should return avg", () => {
expect(operator.evaluate({ a: 3, b: 4, c: 2 })).to.deep.equals(3);
});
it("should return avg when single argument passed", () => {
expect(operator.evaluate({ a: 3 })).to.deep.equals(3);
});
it("should return 0 when wrong arguments passed", () => {
expect(operator.evaluate({ d: 3, e: 4 })).to.deep.equals(0);
});
it("returns 0 when no argument passed", () => {
expect(operator.evaluate({})).to.deep.equals(0);
});
it("works with decimalPlaces specified", () => {
expect(decimalOperator.evaluate({ a: 3, b: 2 })).to.deep.equals(2.5);
});
it("Correctly rounds to the right number of decimal places", () => {
expect(decimalOperator.evaluate({ a: 2, b: 2, c: 1 })).to.deep.equals(1.667);
});
it("works with strings", () => {
expect(decimalOperator.evaluate({ a: "2", b: "3" })).to.deep.equals(2.5);
});
});
});