Skip to content

Commit

Permalink
test: improve query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 18, 2019
1 parent cd09572 commit c4cb68b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
36 changes: 22 additions & 14 deletions keytransform/keytransform_test.go
Expand Up @@ -20,22 +20,24 @@ type DSSuite struct{}

var _ = Suite(&DSSuite{})

func (ks *DSSuite) TestBasic(c *C) {
func testDatastore() {
}

pair := &kt.Pair{
Convert: func(k ds.Key) ds.Key {
return ds.NewKey("/abc").Child(k)
},
Invert: func(k ds.Key) ds.Key {
// remove abc prefix
l := k.List()
if l[0] != "abc" {
panic("key does not have prefix. convert failed?")
}
return ds.KeyWithNamespaces(l[1:])
},
}
var pair = &kt.Pair{
Convert: func(k ds.Key) ds.Key {
return ds.NewKey("/abc").Child(k)
},
Invert: func(k ds.Key) ds.Key {
// remove abc prefix
l := k.List()
if l[0] != "abc" {
panic("key does not have prefix. convert failed?")
}
return ds.KeyWithNamespaces(l[1:])
},
}

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

Expand Down Expand Up @@ -110,3 +112,9 @@ func strsToKeys(strs []string) []ds.Key {
}
return keys
}

func TestSuite(t *testing.T) {
mpds := dstest.NewTestDatastore(true)
ktds := kt.Wrap(mpds, pair)
dstest.SubtestAll(t, ktds)
}
14 changes: 14 additions & 0 deletions mount/mount_test.go
Expand Up @@ -685,3 +685,17 @@ func TestMaintenanceFunctions(t *testing.T) {
t.Errorf("Unexpected Scrub() error: %s", err)
}
}

func TestSuite(t *testing.T) {
mapds0 := datastore.NewMapDatastore()
mapds1 := datastore.NewMapDatastore()
mapds2 := datastore.NewMapDatastore()
mapds3 := datastore.NewMapDatastore()
m := mount.New([]mount.Mount{
{Prefix: datastore.NewKey("/foo"), Datastore: mapds1},
{Prefix: datastore.NewKey("/bar"), Datastore: mapds2},
{Prefix: datastore.NewKey("/baz"), Datastore: mapds3},
{Prefix: datastore.NewKey("/"), Datastore: mapds0},
})
dstest.SubtestAll(t, m)
}
6 changes: 6 additions & 0 deletions namespace/namespace_test.go
Expand Up @@ -155,3 +155,9 @@ func strsToKeys(strs []string) []ds.Key {
}
return keys
}

func TestSuite(t *testing.T) {
mpds := dstest.NewTestDatastore(true)
nsds := ns.Wrap(mpds, ds.NewKey("/foo"))
dstest.SubtestAll(t, nsds)
}
48 changes: 48 additions & 0 deletions test/basic_tests.go
Expand Up @@ -176,6 +176,54 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) {
})
}

func SubtestFilter(t *testing.T, ds dstore.Datastore) {
test := func(filters ...dsq.Filter) {
var types []string
for _, o := range filters {
types = append(types, fmt.Sprintf("%T", o))
}
name := strings.Join(types, ">")
t.Run(name, func(t *testing.T) {
subtestQuery(t, ds, dsq.Query{
Filters: filters,
}, func(t *testing.T, input, output []dsq.Entry) {
var exp []dsq.Entry
input:
for _, e := range input {
for _, f := range filters {
if !f.Filter(e) {
continue input
}
}
exp = append(exp, e)
}

if len(exp) != len(output) {
t.Fatalf("got wrong number of keys back: expected %d, got %d", len(exp), len(output))
}

dsq.Sort([]dsq.Order{dsq.OrderByKey{}}, exp)
dsq.Sort([]dsq.Order{dsq.OrderByKey{}}, output)

for i, e := range output {
if exp[i].Key != e.Key {
t.Fatalf("in key output, got %s but expected %s", e.Key, exp[i].Key)
}
}
})
})
}
test(dsq.FilterKeyCompare{
Op: dsq.Equal,
Key: "/0key0",
})

test(dsq.FilterKeyCompare{
Op: dsq.LessThan,
Key: "/2",
})
}

func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, check func(t *testing.T, input, output []dsq.Entry)) {
var input []dsq.Entry
count := 100
Expand Down
1 change: 1 addition & 0 deletions test/suite.go
Expand Up @@ -14,6 +14,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
SubtestBasicPutGet,
SubtestNotFounds,
SubtestOrder,
SubtestFilter,
SubtestManyKeysAndQuery,
}

Expand Down

0 comments on commit c4cb68b

Please sign in to comment.