-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_number_test.go
158 lines (135 loc) · 3.92 KB
/
engine_number_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
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
package expr
import (
"context"
"testing"
"github.com/google/cel-go/common/operators"
"github.com/stretchr/testify/require"
)
const testConcurrency = 100
func TestEngineNumber(t *testing.T) {
ctx := context.Background()
n := newNumberMatcher(testConcurrency).(*numbers)
// int64
a := ExpressionPart{
Predicate: &Predicate{
Ident: "async.data.id",
Literal: int64(123),
Operator: operators.Equals,
},
}
b := ExpressionPart{
Predicate: &Predicate{
Ident: "async.data.pi",
Literal: float64(1.131),
Operator: operators.Equals,
},
}
c := ExpressionPart{
Predicate: &Predicate{
Ident: "async.data.id",
Literal: 25,
Operator: operators.GreaterEquals,
},
}
d := ExpressionPart{
Predicate: &Predicate{
Ident: "async.data.id",
Literal: 9999,
Operator: operators.Greater,
},
}
e := ExpressionPart{
Predicate: &Predicate{
Ident: "async.data.id",
Literal: -100,
Operator: operators.Less,
},
}
t.Run("It adds numbers", func(t *testing.T) {
var err error
err = n.Add(ctx, a)
require.NoError(t, err)
err = n.Add(ctx, b)
require.NoError(t, err)
err = n.Add(ctx, c)
require.NoError(t, err)
err = n.Add(ctx, d)
require.NoError(t, err)
err = n.Add(ctx, e)
require.NoError(t, err)
})
t.Run("It searches >=", func(t *testing.T) {
t.Run("with ints", func(t *testing.T) {
// Expect only the >= id match.
parts := n.Search(ctx, "async.data.id", 999)
require.Equal(t, 1, len(parts))
for _, part := range parts {
require.EqualValues(t, part.PredicateID, c.Hash())
}
})
t.Run("with float64", func(t *testing.T) {
// Expect only the >= id match.
parts := n.Search(ctx, "async.data.id", float64(999))
require.Equal(t, 1, len(parts))
for _, part := range parts {
require.EqualValues(t, part.PredicateID, c.Hash())
}
})
})
t.Run("It matches == && >=", func(t *testing.T) {
t.Run("with ints", func(t *testing.T) {
// Expect only the >= id match.
parts := n.Search(ctx, "async.data.id", 123)
require.Equal(t, 2, len(parts))
require.EqualValues(t, parts[0].PredicateID, a.Hash())
require.EqualValues(t, parts[1].PredicateID, c.Hash())
})
t.Run("with float64", func(t *testing.T) {
// Expect only the >= id match.
parts := n.Search(ctx, "async.data.id", float64(123))
require.Equal(t, 2, len(parts))
require.EqualValues(t, parts[0].PredicateID, a.Hash())
require.EqualValues(t, parts[1].PredicateID, c.Hash())
})
t.Run("with a low number", func(t *testing.T) {
// Expect only the >= id match.
parts := n.Search(ctx, "async.data.id", float64(1.00001))
require.Equal(t, 0, len(parts), "returned parts: %#v")
parts = n.Search(ctx, "async.data.id", float64(24.999))
require.Equal(t, 0, len(parts), "returned parts: %#v")
parts = n.Search(ctx, "async.data.id", float64(25.0001))
require.Equal(t, 1, len(parts), "returned parts: %#v")
})
t.Run("matches pi", func(t *testing.T) {
parts := n.Search(ctx, "async.data.pi", 1.131)
require.Equal(t, 1, len(parts))
require.EqualValues(t, parts[0].PredicateID, b.Hash())
})
t.Run("gt", func(t *testing.T) {
parts := n.Search(ctx, "async.data.id", 999999)
require.Equal(t, 2, len(parts))
require.EqualValues(t, parts[0].PredicateID, c.Hash())
require.EqualValues(t, parts[1].PredicateID, d.Hash())
})
t.Run("lt", func(t *testing.T) {
parts := n.Search(ctx, "async.data.id", -999999)
require.Equal(t, 1, len(parts))
require.EqualValues(t, parts[0].PredicateID, e.Hash())
})
})
t.Run("It matches data", func(t *testing.T) {
found, err := n.Match(ctx, map[string]any{
"async": map[string]any{
"data": map[string]any{
"id": 123,
},
},
})
require.NoError(t, err)
require.Equal(t, 2, len(found))
require.EqualValues(t, found[0].PredicateID, a.Hash())
require.EqualValues(t, found[1].PredicateID, c.Hash())
})
err := n.Add(ctx, a)
require.NoError(t, err, "re-adding expression failed")
}