@@ -176,6 +176,54 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) {
176
176
})
177
177
}
178
178
179
+ func SubtestFilter (t * testing.T , ds dstore.Datastore ) {
180
+ test := func (filters ... dsq.Filter ) {
181
+ var types []string
182
+ for _ , o := range filters {
183
+ types = append (types , fmt .Sprintf ("%T" , o ))
184
+ }
185
+ name := strings .Join (types , ">" )
186
+ t .Run (name , func (t * testing.T ) {
187
+ subtestQuery (t , ds , dsq.Query {
188
+ Filters : filters ,
189
+ }, func (t * testing.T , input , output []dsq.Entry ) {
190
+ var exp []dsq.Entry
191
+ input:
192
+ for _ , e := range input {
193
+ for _ , f := range filters {
194
+ if ! f .Filter (e ) {
195
+ continue input
196
+ }
197
+ }
198
+ exp = append (exp , e )
199
+ }
200
+
201
+ if len (exp ) != len (output ) {
202
+ t .Fatalf ("got wrong number of keys back: expected %d, got %d" , len (exp ), len (output ))
203
+ }
204
+
205
+ dsq .Sort ([]dsq.Order {dsq.OrderByKey {}}, exp )
206
+ dsq .Sort ([]dsq.Order {dsq.OrderByKey {}}, output )
207
+
208
+ for i , e := range output {
209
+ if exp [i ].Key != e .Key {
210
+ t .Fatalf ("in key output, got %s but expected %s" , e .Key , exp [i ].Key )
211
+ }
212
+ }
213
+ })
214
+ })
215
+ }
216
+ test (dsq.FilterKeyCompare {
217
+ Op : dsq .Equal ,
218
+ Key : "/0key0" ,
219
+ })
220
+
221
+ test (dsq.FilterKeyCompare {
222
+ Op : dsq .LessThan ,
223
+ Key : "/2" ,
224
+ })
225
+ }
226
+
179
227
func subtestQuery (t * testing.T , ds dstore.Datastore , q dsq.Query , check func (t * testing.T , input , output []dsq.Entry )) {
180
228
var input []dsq.Entry
181
229
count := 100
0 commit comments