Skip to content

Commit c4cb68b

Browse files
committed
test: improve query tests
1 parent cd09572 commit c4cb68b

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

keytransform/keytransform_test.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ type DSSuite struct{}
2020

2121
var _ = Suite(&DSSuite{})
2222

23-
func (ks *DSSuite) TestBasic(c *C) {
23+
func testDatastore() {
24+
}
2425

25-
pair := &kt.Pair{
26-
Convert: func(k ds.Key) ds.Key {
27-
return ds.NewKey("/abc").Child(k)
28-
},
29-
Invert: func(k ds.Key) ds.Key {
30-
// remove abc prefix
31-
l := k.List()
32-
if l[0] != "abc" {
33-
panic("key does not have prefix. convert failed?")
34-
}
35-
return ds.KeyWithNamespaces(l[1:])
36-
},
37-
}
26+
var pair = &kt.Pair{
27+
Convert: func(k ds.Key) ds.Key {
28+
return ds.NewKey("/abc").Child(k)
29+
},
30+
Invert: func(k ds.Key) ds.Key {
31+
// remove abc prefix
32+
l := k.List()
33+
if l[0] != "abc" {
34+
panic("key does not have prefix. convert failed?")
35+
}
36+
return ds.KeyWithNamespaces(l[1:])
37+
},
38+
}
3839

40+
func (ks *DSSuite) TestBasic(c *C) {
3941
mpds := dstest.NewTestDatastore(true)
4042
ktds := kt.Wrap(mpds, pair)
4143

@@ -110,3 +112,9 @@ func strsToKeys(strs []string) []ds.Key {
110112
}
111113
return keys
112114
}
115+
116+
func TestSuite(t *testing.T) {
117+
mpds := dstest.NewTestDatastore(true)
118+
ktds := kt.Wrap(mpds, pair)
119+
dstest.SubtestAll(t, ktds)
120+
}

mount/mount_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,17 @@ func TestMaintenanceFunctions(t *testing.T) {
685685
t.Errorf("Unexpected Scrub() error: %s", err)
686686
}
687687
}
688+
689+
func TestSuite(t *testing.T) {
690+
mapds0 := datastore.NewMapDatastore()
691+
mapds1 := datastore.NewMapDatastore()
692+
mapds2 := datastore.NewMapDatastore()
693+
mapds3 := datastore.NewMapDatastore()
694+
m := mount.New([]mount.Mount{
695+
{Prefix: datastore.NewKey("/foo"), Datastore: mapds1},
696+
{Prefix: datastore.NewKey("/bar"), Datastore: mapds2},
697+
{Prefix: datastore.NewKey("/baz"), Datastore: mapds3},
698+
{Prefix: datastore.NewKey("/"), Datastore: mapds0},
699+
})
700+
dstest.SubtestAll(t, m)
701+
}

namespace/namespace_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,9 @@ func strsToKeys(strs []string) []ds.Key {
155155
}
156156
return keys
157157
}
158+
159+
func TestSuite(t *testing.T) {
160+
mpds := dstest.NewTestDatastore(true)
161+
nsds := ns.Wrap(mpds, ds.NewKey("/foo"))
162+
dstest.SubtestAll(t, nsds)
163+
}

test/basic_tests.go

+48
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,54 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) {
176176
})
177177
}
178178

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+
179227
func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, check func(t *testing.T, input, output []dsq.Entry)) {
180228
var input []dsq.Entry
181229
count := 100

test/suite.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
1414
SubtestBasicPutGet,
1515
SubtestNotFounds,
1616
SubtestOrder,
17+
SubtestFilter,
1718
SubtestManyKeysAndQuery,
1819
}
1920

0 commit comments

Comments
 (0)