-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_access_test.go
100 lines (94 loc) · 4.69 KB
/
data_access_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
90
91
92
93
94
95
96
97
98
99
100
package jsonlogic
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestOpVar(t *testing.T) {
assert := assert.New(t)
jl := NewEmpty()
AddOpVar(jl)
TestCases{
// http://jsonlogic.com/operations.html#var
{Logic: `{"var":["a"]}`, Data: `{"a":1,"b":2}`, Result: float64(1)},
{Logic: `{"var":"a"}`, Data: `{"a":1,"b":2}`, Result: float64(1)},
{Logic: `{"var":["z",26]}`, Data: `{"a":1,"b":2}`, Result: float64(26)},
{Logic: `{"var":"champ.name"}`, Data: `{"champ":{"name":"Fezzig","height":223},"challenger":{"name":"DreadPirateRoberts","height":183}}`, Result: "Fezzig"},
{Logic: `{"var":1}`, Data: `["zero", "one", "two"]`, Result: "one"},
{Logic: `{"var":""}`, Data: `"Dolly"`, Result: "Dolly"},
// Default.
{Logic: `{"var":["a",["def"]]}`, Data: `{"c":"d"}`, Result: []interface{}{"def"}},
// Using logic in key/default.
{Logic: `{"var":["a",{"var":"a_default"}]}`, Data: `{"c":"d","a_default":"aaa"}`, Result: "aaa"},
{Logic: `{"var":{"var":"pointer"}}`, Data: `{"pointer":"x","x":1.1}`, Result: float64(1.1)},
// null or "" returns whole data.
{Logic: `{"var":null}`, Data: `{"a":"b"}`, Result: map[string]interface{}{"a": "b"}},
{Logic: `{"var":""}`, Data: `{"a":"b"}`, Result: map[string]interface{}{"a": "b"}},
// String key and map.
{Logic: `{"var":"a"}`, Data: `{"c":"d"}`, Result: nil},
{Logic: `{"var":"a"}`, Data: `{"a":"b"}`, Result: "b"},
{Logic: `{"var":"a.c"}`, Data: `{"a":"b"}`, Result: nil},
{Logic: `{"var":"a.c"}`, Data: `{"a":{"c":"d"}}`, Result: "d"},
// Numeric key and map.
{Logic: `{"var":0.0}`, Data: `{"1":2}`, Result: nil},
{Logic: `{"var":0.0}`, Data: `{"0":1}`, Result: float64(1)},
{Logic: `{"var":0.1}`, Data: `{"0.1":1}`, Result: nil}, // Surprise! Since 0.1 -> "0.1" -> data["0"]["1"]
{Logic: `{"var":0.1}`, Data: `{"0":{"1":3}}`, Result: float64(3)},
{Logic: `{"var":-0.1}`, Data: `{"0":{"1":3}}`, Result: nil},
{Logic: `{"var":-0.1}`, Data: `{"-0":{"1":3}}`, Result: float64(3)},
// String key and array.
{Logic: `{"var":"-1"}`, Data: `["a","b"]`, Result: nil},
{Logic: `{"var":"0"}`, Data: `["a","b"]`, Result: "a"},
{Logic: `{"var":"1"}`, Data: `["a","b"]`, Result: "b"},
{Logic: `{"var":"2"}`, Data: `["a","b"]`, Result: nil},
{Logic: `{"var":"a"}`, Data: `["a","b"]`, Result: nil},
// Numeric key and array.
{Logic: `{"var":-1.0}`, Data: `["a","b"]`, Result: nil},
{Logic: `{"var":0.0}`, Data: `["a","b"]`, Result: "a"},
{Logic: `{"var":1.0}`, Data: `["a","b"]`, Result: "b"},
{Logic: `{"var":2.0}`, Data: `["a","b"]`, Result: nil},
{Logic: `{"var":0.1}`, Data: `["a","b"]`, Result: nil},
// Mix.
{Logic: `{"var":"1.a"}`, Data: `["a",{"a":"b"}]`, Result: "b"},
{Logic: `{"var":"a.0"}`, Data: `{"a":[8,9,10]}`, Result: float64(8)},
// Err.
{Logic: `{"var":[]}`, Data: `null`, Err: true},
{Logic: `{"var":[[]]}`, Data: `null`, Err: true},
}.Run(assert, jl)
}
func TestOpMissing(t *testing.T) {
assert := assert.New(t)
jl := NewEmpty()
AddOpVar(jl)
AddOpMissing(jl)
TestCases{
// http://jsonlogic.com/operations.html#missing
{Logic: `{"missing":["a","b"]}`, Data: `{"a":"apple","c":"carrot"}`, Result: []interface{}{"b"}},
{Logic: `{"missing":["a","b"]}`, Data: `{"a":"apple", "b":"banana"}`, Result: []interface{}{}},
// null or "" are treated as missing
{Logic: `{"missing":"a"}`, Data: `{"a":null}`, Result: []interface{}{"a"}}, // Surprise!
{Logic: `{"missing":"a"}`, Data: `{"a":""}`, Result: []interface{}{"a"}}, // Surprise again!
{Logic: `{"missing":"a"}`, Data: `{"a":false}`, Result: []interface{}{}},
{Logic: `{"missing":"a"}`, Data: `{"a":0}`, Result: []interface{}{}},
{Logic: `{"missing":"a"}`, Data: `{"a":[]}`, Result: []interface{}{}},
{Logic: `{"missing":"a"}`, Data: `{"a":{}}`, Result: []interface{}{}},
// First param is an array.
{Logic: `{"missing":[["a"]]}`, Data: `{"a":"1"}`, Result: []interface{}{}},
{Logic: `{"missing":[["a"]]}`, Data: `{"b":"1"}`, Result: []interface{}{"a"}},
// Using logic in param.
{Logic: `{"missing":{"var":"pointer"}}`, Data: `{"a":"xxx","pointer":"a"}`, Result: []interface{}{}},
{Logic: `{"missing":{"var":"pointer"}}`, Data: `{"a":"xxx","pointer":"b"}`, Result: []interface{}{"b"}},
}.Run(assert, jl)
}
func TestOpMissingSome(t *testing.T) {
assert := assert.New(t)
jl := NewEmpty()
AddOpVar(jl)
AddOpMissingSome(jl)
TestCases{
// http://jsonlogic.com/operations.html#missing_some
{Logic: `{"missing_some":[1,["a","b","c"]]}`, Data: `{"a":"apple"}`, Result: []interface{}{}},
{Logic: `{"missing_some":[2,["a","b","c"]]}`, Data: `{"a":"apple"}`, Result: []interface{}{"b", "c"}},
// Using logic in param.
{Logic: `{"missing_some":[2,{"var":"pointer"}]}`, Data: `{"pointer":["x","y"]}`, Result: []interface{}{"x", "y"}},
}.Run(assert, jl)
}