FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
json-rules-engine/test/condition.test.js at master · CacheControl/json-rules-engine · GitHub
CacheControl
/
json-rules-engine
Public
Notifications
You must be signed in to change notification settings
Fork
507
Star
3.1k
Code
Issues
61
Pull requests
7
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
json-rules-engine
/
test
/
condition.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
380 lines (338 loc) · 14 KB
Breadcrumbs
json-rules-engine
/
test
/
condition.test.js
Copy path
File metadata and controls
380 lines (338 loc) · 14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'use strict'
import
Condition
from
'../src/condition'
import
defaultOperators
from
'../src/engine-default-operators'
import
Almanac
from
'../src/almanac'
import
Fact
from
'../src/fact'
const
operators
=
new
Map
(
)
defaultOperators
.
forEach
(
o
=>
operators
.
set
(
o
.
name
,
o
)
)
function
condition
(
)
{
return
{
all
:
[
{
id
:
'6ed20017-375f-40c9-a1d2-6d7e0f4733c5'
,
name
:
'team participation in form'
,
fact
:
'team_participation'
,
operator
:
'equal'
,
value
:
50
,
path
:
'.metrics[0].forum-posts'
}
]
}
}
describe
(
'Condition'
,
(
)
=>
{
describe
(
'constructor'
,
(
)
=>
{
it
(
'fact conditions have properties'
,
(
)
=>
{
const
properties
=
condition
(
)
const
subject
=
new
Condition
(
properties
.
all
[
0
]
)
expect
(
subject
)
.
to
.
have
.
property
(
'fact'
)
expect
(
subject
)
.
to
.
have
.
property
(
'operator'
)
expect
(
subject
)
.
to
.
have
.
property
(
'value'
)
expect
(
subject
)
.
to
.
have
.
property
(
'path'
)
expect
(
subject
)
.
to
.
have
.
property
(
'name'
)
}
)
it
(
'boolean conditions have properties'
,
(
)
=>
{
const
properties
=
condition
(
)
const
subject
=
new
Condition
(
properties
)
expect
(
subject
)
.
to
.
have
.
property
(
'operator'
)
expect
(
subject
)
.
to
.
have
.
property
(
'priority'
)
expect
(
subject
.
priority
)
.
to
.
equal
(
1
)
}
)
}
)
describe
(
'toJSON'
,
(
)
=>
{
it
(
'converts the condition into a json string'
,
(
)
=>
{
const
properties
=
factories
.
condition
(
{
fact
:
'age'
,
value
:
{
fact
:
'weight'
,
params
:
{
unit
:
'lbs'
}
,
path
:
'.value'
}
}
)
const
condition
=
new
Condition
(
properties
)
const
json
=
condition
.
toJSON
(
)
expect
(
json
)
.
to
.
equal
(
'{"operator":"equal","value":{"fact":"weight","params":{"unit":"lbs"},"path":".value"},"fact":"age"}'
)
}
)
it
(
'converts "not" conditions'
,
(
)
=>
{
const
properties
=
{
not
:
{
...
factories
.
condition
(
{
fact
:
'age'
,
value
:
{
fact
:
'weight'
,
params
:
{
unit
:
'lbs'
}
,
path
:
'.value'
}
}
)
}
}
const
condition
=
new
Condition
(
properties
)
const
json
=
condition
.
toJSON
(
)
expect
(
json
)
.
to
.
equal
(
'{"priority":1,"not":{"operator":"equal","value":{"fact":"weight","params":{"unit":"lbs"},"path":".value"},"fact":"age"}}'
)
}
)
}
)
describe
(
'evaluate'
,
(
)
=>
{
const
conditionBase
=
factories
.
condition
(
{
fact
:
'age'
,
value
:
50
}
)
let
condition
let
almanac
function
setup
(
options
,
factValue
)
{
const
properties
=
Object
.
assign
(
{
}
,
conditionBase
,
options
)
condition
=
new
Condition
(
properties
)
const
fact
=
new
Fact
(
conditionBase
.
fact
,
factValue
)
almanac
=
new
Almanac
(
)
almanac
.
addFact
(
fact
)
}
context
(
'validations'
,
(
)
=>
{
beforeEach
(
(
)
=>
setup
(
{
}
,
1
)
)
it
(
'throws when missing an almanac'
,
(
)
=>
{
return
expect
(
condition
.
evaluate
(
undefined
,
operators
)
)
.
to
.
be
.
rejectedWith
(
'almanac required'
)
}
)
it
(
'throws when missing operators'
,
(
)
=>
{
return
expect
(
condition
.
evaluate
(
almanac
,
undefined
)
)
.
to
.
be
.
rejectedWith
(
'operatorMap required'
)
}
)
it
(
'throws when run against a boolean operator'
,
(
)
=>
{
condition
.
all
=
[
]
return
expect
(
condition
.
evaluate
(
almanac
,
operators
)
)
.
to
.
be
.
rejectedWith
(
'Cannot evaluate() a boolean condition'
)
}
)
}
)
it
(
'evaluates "equal"'
,
async
(
)
=>
{
setup
(
{
operator
:
'equal'
}
,
50
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
,
50
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'equal'
}
,
5
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
,
5
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "equal" to check for undefined'
,
async
(
)
=>
{
condition
=
new
Condition
(
{
fact
:
'age'
,
operator
:
'equal'
,
value
:
undefined
}
)
let
fact
=
new
Fact
(
'age'
,
undefined
)
almanac
=
new
Almanac
(
)
almanac
.
addFact
(
fact
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
fact
=
new
Fact
(
'age'
,
1
)
almanac
=
new
Almanac
(
)
almanac
.
addFact
(
fact
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "notEqual"'
,
async
(
)
=>
{
setup
(
{
operator
:
'notEqual'
}
,
50
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'notEqual'
}
,
5
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
}
)
it
(
'evaluates "in"'
,
async
(
)
=>
{
setup
(
{
operator
:
'in'
,
value
:
[
5
,
10
,
15
,
20
]
}
,
15
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'in'
,
value
:
[
5
,
10
,
15
,
20
]
}
,
99
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "contains"'
,
async
(
)
=>
{
setup
(
{
operator
:
'contains'
,
value
:
10
}
,
[
5
,
10
,
15
]
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'contains'
,
value
:
10
}
,
[
1
,
2
,
3
]
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "doesNotContain"'
,
async
(
)
=>
{
setup
(
{
operator
:
'doesNotContain'
,
value
:
10
}
,
[
5
,
10
,
15
]
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'doesNotContain'
,
value
:
10
}
,
[
1
,
2
,
3
]
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
}
)
it
(
'evaluates "notIn"'
,
async
(
)
=>
{
setup
(
{
operator
:
'notIn'
,
value
:
[
5
,
10
,
15
,
20
]
}
,
15
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'notIn'
,
value
:
[
5
,
10
,
15
,
20
]
}
,
99
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
}
)
it
(
'evaluates "lessThan"'
,
async
(
)
=>
{
setup
(
{
operator
:
'lessThan'
}
,
49
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'lessThan'
}
,
50
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'lessThan'
}
,
51
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "lessThanInclusive"'
,
async
(
)
=>
{
setup
(
{
operator
:
'lessThanInclusive'
}
,
49
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'lessThanInclusive'
}
,
50
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'lessThanInclusive'
}
,
51
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "greaterThan"'
,
async
(
)
=>
{
setup
(
{
operator
:
'greaterThan'
}
,
51
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'greaterThan'
}
,
49
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'greaterThan'
}
,
50
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'evaluates "greaterThanInclusive"'
,
async
(
)
=>
{
setup
(
{
operator
:
'greaterThanInclusive'
}
,
51
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'greaterThanInclusive'
}
,
50
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
setup
(
{
operator
:
'greaterThanInclusive'
}
,
49
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
describe
(
'invalid comparisonValues'
,
(
)
=>
{
it
(
'returns false when using contains or doesNotContain with a non-array'
,
async
(
)
=>
{
setup
(
{
operator
:
'contains'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'doesNotContain'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'returns false when using comparison operators with null'
,
async
(
)
=>
{
setup
(
{
operator
:
'lessThan'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'lessThanInclusive'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'greaterThan'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'greaterThanInclusive'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'returns false when using comparison operators with non-numbers'
,
async
(
)
=>
{
setup
(
{
operator
:
'lessThan'
}
,
'non-number'
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'lessThan'
}
,
null
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'lessThan'
}
,
[
]
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
setup
(
{
operator
:
'lessThan'
}
,
{
}
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
}
)
}
)
describe
(
'objects'
,
(
)
=>
{
describe
(
'.path'
,
(
)
=>
{
it
(
'extracts the object property values using its "path" property'
,
async
(
)
=>
{
const
condition
=
new
Condition
(
{
operator
:
'equal'
,
path
:
'$.[0].id'
,
fact
:
'age'
,
value
:
50
}
)
const
ageFact
=
new
Fact
(
'age'
,
[
{
id
:
50
}
,
{
id
:
60
}
]
)
const
almanac
=
new
Almanac
(
)
almanac
.
addFact
(
ageFact
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
condition
.
value
=
100
// negative case
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
it
(
'ignores "path" when non-objects are returned by the fact'
,
async
(
)
=>
{
const
ageFact
=
new
Fact
(
'age'
,
50
)
const
almanac
=
new
Almanac
(
)
almanac
.
addFact
(
ageFact
)
const
condition
=
new
Condition
(
{
operator
:
'equal'
,
path
:
'$.[0].id'
,
fact
:
'age'
,
value
:
50
}
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
,
50
)
)
.
result
)
.
to
.
equal
(
true
)
condition
.
value
=
100
// negative case
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
,
50
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
}
)
describe
(
'jsonPath'
,
(
)
=>
{
it
(
'allows json path to extract values from complex facts'
,
async
(
)
=>
{
const
condition
=
new
Condition
(
{
operator
:
'contains'
,
path
:
'$.phoneNumbers[*].type'
,
fact
:
'users'
,
value
:
'iPhone'
}
)
const
userData
=
{
phoneNumbers
:
[
{
type
:
'iPhone'
,
number
:
'0123-4567-8888'
}
,
{
type
:
'home'
,
number
:
'0123-4567-8910'
}
]
}
const
usersFact
=
new
Fact
(
'users'
,
userData
)
const
almanac
=
new
Almanac
(
)
almanac
.
addFact
(
usersFact
)
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
true
)
condition
.
value
=
'work'
// negative case
expect
(
(
await
condition
.
evaluate
(
almanac
,
operators
)
)
.
result
)
.
to
.
equal
(
false
)
}
)
}
)
}
)
describe
(
'boolean operators'
,
(
)
=>
{
it
(
'throws if not not an array'
,
(
)
=>
{
const
conditions
=
condition
(
)
conditions
.
all
=
{
foo
:
true
}
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
throw
(
/
"
a
l
l
"
m
u
s
t
b
e
a
n
a
r
r
a
y
/
)
}
)
it
(
'throws if is an array and condition is "not"'
,
(
)
=>
{
const
conditions
=
{
not
:
[
{
foo
:
true
}
]
}
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
throw
(
/
"
n
o
t
"
c
a
n
n
o
t
b
e
a
n
a
r
r
a
y
/
)
}
)
it
(
'does not throw if is not an array and condition is "not"'
,
(
)
=>
{
const
conditions
=
{
not
:
{
fact
:
'foo'
,
operator
:
'equal'
,
value
:
'bar'
}
}
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
not
.
throw
(
)
}
)
}
)
describe
(
'atomic facts'
,
(
)
=>
{
it
(
'throws if no options are provided'
,
(
)
=>
{
expect
(
(
)
=>
new
Condition
(
)
)
.
to
.
throw
(
/
C
o
n
d
i
t
i
o
n
:
c
o
n
s
t
r
u
c
t
o
r
o
p
t
i
o
n
s
r
e
q
u
i
r
e
d
/
)
}
)
it
(
'throws for a missing "operator"'
,
(
)
=>
{
const
conditions
=
condition
(
)
delete
conditions
.
all
[
0
]
.
operator
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
throw
(
/
C
o
n
d
i
t
i
o
n
:
c
o
n
s
t
r
u
c
t
o
r
"
o
p
e
r
a
t
o
r
"
p
r
o
p
e
r
t
y
r
e
q
u
i
r
e
d
/
)
}
)
it
(
'throws for a missing "fact"'
,
(
)
=>
{
const
conditions
=
condition
(
)
delete
conditions
.
all
[
0
]
.
fact
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
throw
(
/
C
o
n
d
i
t
i
o
n
:
c
o
n
s
t
r
u
c
t
o
r
"
f
a
c
t
"
p
r
o
p
e
r
t
y
r
e
q
u
i
r
e
d
/
)
}
)
it
(
'throws for a missing "value"'
,
(
)
=>
{
const
conditions
=
condition
(
)
delete
conditions
.
all
[
0
]
.
value
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
throw
(
/
C
o
n
d
i
t
i
o
n
:
c
o
n
s
t
r
u
c
t
o
r
"
v
a
l
u
e
"
p
r
o
p
e
r
t
y
r
e
q
u
i
r
e
d
/
)
}
)
}
)
describe
(
'complex conditions'
,
(
)
=>
{
function
complexCondition
(
)
{
return
{
all
:
[
{
fact
:
'age'
,
operator
:
'lessThan'
,
value
:
45
}
,
{
fact
:
'pointBalance'
,
operator
:
'greaterThanInclusive'
,
value
:
1000
}
,
{
any
:
[
{
fact
:
'gender'
,
operator
:
'equal'
,
value
:
'female'
}
,
{
fact
:
'income'
,
operator
:
'greaterThanInclusive'
,
value
:
50000
}
]
}
]
}
}
it
(
'recursively parses nested conditions'
,
(
)
=>
{
expect
(
(
)
=>
new
Condition
(
complexCondition
(
)
)
)
.
to
.
not
.
throw
(
)
}
)
it
(
'throws if a nested condition is invalid'
,
(
)
=>
{
const
conditions
=
complexCondition
(
)
delete
conditions
.
all
[
2
]
.
any
[
0
]
.
fact
expect
(
(
)
=>
new
Condition
(
conditions
)
)
.
to
.
throw
(
/
C
o
n
d
i
t
i
o
n
:
c
o
n
s
t
r
u
c
t
o
r
"
f
a
c
t
"
p
r
o
p
e
r
t
y
r
e
q
u
i
r
e
d
/
)
}
)
}
)
}
)
Back
|
FazBrowse Home
|
New Git URL