-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathexists_test.go
89 lines (76 loc) · 2.08 KB
/
exists_test.go
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
package json
import (
"encoding/json"
"testing"
"github.com/project-flogo/core/data/expression/function"
"github.com/stretchr/testify/assert"
)
const inputExists = `{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10,
"emptyString": ""
}`
func TestFnCheckExists(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)
f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$.store.book[?(@.price == 12.99)].price[0]")
assert.Nil(t, err)
assert.Equal(t, true, v)
}
func TestFnExistsLoop(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)
f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$loop.store.book[?(@.price == 22.99)].price[0]")
assert.Nil(t, err)
assert.Equal(t, false, v)
}
func TestFnExistsNegative(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)
f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$.store.abc")
assert.Nil(t, err)
assert.Equal(t, false, v)
}
func TestFnExistsEmpty(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)
f := &fnExists{}
v, err := function.Eval(f, inputJSON, "$.emptyString")
assert.Nil(t, err)
assert.Equal(t, true, v)
}
func TestFnExistsWithoutJSONPath(t *testing.T) {
var inputJSON interface{}
err := json.Unmarshal([]byte(inputExists), &inputJSON)
assert.Nil(t, err)
f := &fnExists{}
v, err := function.Eval(f, inputJSON, "expensive")
assert.Nil(t, err)
assert.Equal(t, true, v)
}