-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquery_test.go
255 lines (247 loc) · 5.53 KB
/
query_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
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
package circuits
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
func TestScalarCompare(t *testing.T) {
type test struct {
name string
x *big.Int
y *big.Int
operator int
expected bool
withErr bool
}
tests := []test{
{
name: "testing $eq operator where x == y",
x: big.NewInt(0),
y: big.NewInt(0),
operator: EQ,
expected: true,
},
{
name: "testing $eq operator where x != y",
x: big.NewInt(10),
y: big.NewInt(0),
operator: EQ,
expected: false,
},
{
name: "testing $lt operator where x < y",
x: big.NewInt(-1),
y: big.NewInt(0),
operator: LT,
expected: true,
},
{
name: "testing $lt operator where x > y",
x: big.NewInt(1),
y: big.NewInt(0),
operator: LT,
expected: false,
},
{
name: "testing $gt operator where x > y",
x: big.NewInt(1),
y: big.NewInt(0),
operator: GT,
expected: true,
},
{
name: "testing $gt operator where x < y",
x: big.NewInt(0),
y: big.NewInt(1),
operator: GT,
expected: false,
},
{
name: "testing $in should fail",
x: big.NewInt(0),
y: big.NewInt(1),
operator: IN,
expected: false,
withErr: true,
},
{
name: "testing $nin should faile",
x: big.NewInt(0),
y: big.NewInt(1),
operator: NIN,
expected: false,
withErr: true,
},
{
name: "testing $ne operator where x == y",
x: big.NewInt(0),
y: big.NewInt(0),
operator: NE,
expected: false,
},
{
name: "testing $ne operator where x != y",
x: big.NewInt(10),
y: big.NewInt(0),
operator: NE,
expected: true,
},
{
name: "testing $lte operator where x == y",
x: big.NewInt(0),
y: big.NewInt(0),
operator: LTE,
expected: true,
},
{
name: "testing $lte operator where x < y",
x: big.NewInt(0),
y: big.NewInt(1),
operator: LTE,
expected: true,
},
{
name: "testing $lte operator where x > y",
x: big.NewInt(2),
y: big.NewInt(1),
operator: LTE,
expected: false,
},
{
name: "testing $gte operator where x == y",
x: big.NewInt(0),
y: big.NewInt(0),
operator: GTE,
expected: true,
},
{
name: "testing $gte operator where x < y",
x: big.NewInt(0),
y: big.NewInt(1),
operator: GTE,
expected: false,
},
{
name: "testing $gte operator where x > y",
x: big.NewInt(2),
y: big.NewInt(1),
operator: GTE,
expected: true,
},
{
name: "testing unknown operator should fail",
x: big.NewInt(0),
y: big.NewInt(1),
operator: 10, // unknown operator.
expected: false,
withErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmp := NewScalar(tt.x, tt.y)
actual, err := cmp.Compare(tt.operator)
if tt.withErr {
require.NotNil(t, err)
} else if err != nil {
require.NoError(t, err)
}
require.Equal(t, tt.expected, actual)
})
}
}
func TestVectorCompare(t *testing.T) {
type test struct {
name string
x *big.Int
y []*big.Int
operator int
expected bool
withErr bool
}
tests := []test{
{
name: "testing $in operator where x EXIST in y",
x: big.NewInt(100),
y: []*big.Int{big.NewInt(1), big.NewInt(10), big.NewInt(100)},
operator: IN,
expected: true,
},
{
name: "testing $in operator where x NOT EXIST y",
x: big.NewInt(1000),
y: []*big.Int{big.NewInt(1), big.NewInt(10), big.NewInt(100)},
operator: IN,
expected: false,
},
{
name: "testing $nin operator where x NOT EXIST in y",
x: big.NewInt(1000),
y: []*big.Int{big.NewInt(1), big.NewInt(10), big.NewInt(100)},
operator: NIN,
expected: true,
},
{
name: "testing $nin operator where x EXIST in y",
x: big.NewInt(100),
y: []*big.Int{big.NewInt(1), big.NewInt(10), big.NewInt(100)},
operator: NIN,
expected: false,
},
{
name: "testing unknown operator",
x: big.NewInt(0),
y: []*big.Int{big.NewInt(1), big.NewInt(10), big.NewInt(100)},
operator: 12, // unknown operator.
expected: false,
withErr: true,
},
{
name: "empty array for $in. return false",
x: big.NewInt(0),
y: []*big.Int{},
operator: IN,
expected: false,
},
{
name: "empty array for $nin. return true",
x: big.NewInt(0),
y: []*big.Int{},
operator: NIN,
expected: true,
},
{
name: "one value array for $between. return false",
x: big.NewInt(0),
y: []*big.Int{big.NewInt(1)},
operator: BETWEEN,
expected: false,
},
{
name: "testing $between operator where x in between range.",
x: big.NewInt(2),
y: []*big.Int{big.NewInt(1), big.NewInt(3)},
operator: BETWEEN,
expected: true,
},
{
name: "testing $between operator where x not in between range.",
x: big.NewInt(0),
y: []*big.Int{big.NewInt(1), big.NewInt(3)},
operator: BETWEEN,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmp := NewVector(tt.x, tt.y)
actual, err := cmp.Compare(tt.operator)
if tt.withErr {
require.NotNil(t, err)
} else if err != nil {
require.NoError(t, err)
}
require.Equal(t, tt.expected, actual)
})
}
}