Skip to content

Commit

Permalink
test: improve query coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 18, 2019
1 parent 54ed9bc commit ffc9f91
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
8 changes: 7 additions & 1 deletion keytransform/keytransform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ func strsToKeys(strs []string) []ds.Key {
return keys
}

func TestSuite(t *testing.T) {
func TestSuiteDefaultPair(t *testing.T) {
mpds := dstest.NewTestDatastore(true)
ktds := kt.Wrap(mpds, pair)
dstest.SubtestAll(t, ktds)
}

func TestSuitePrefixTransform(t *testing.T) {
mpds := dstest.NewTestDatastore(true)
ktds := kt.Wrap(mpds, kt.PrefixTransform{Prefix: ds.NewKey("/foo")})
dstest.SubtestAll(t, ktds)
}
37 changes: 35 additions & 2 deletions test/basic_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) {
})
}

// need a custom test filter to test the "fallback" filter case for unknown
// filters.
type testFilter struct{}

func (testFilter) Filter(e dsq.Entry) bool {
return len(e.Key)%2 == 0
}

func SubtestFilter(t *testing.T, ds dstore.Datastore) {
test := func(filters ...dsq.Filter) {
var types []string
Expand Down Expand Up @@ -222,6 +230,32 @@ func SubtestFilter(t *testing.T, ds dstore.Datastore) {
Op: dsq.LessThan,
Key: "/2",
})

test(&dsq.FilterKeyCompare{
Op: dsq.Equal,
Key: "/0key0",
})

test(dsq.FilterKeyPrefix{
Prefix: "/0key0",
})

test(&dsq.FilterKeyPrefix{
Prefix: "/0key0",
})

test(dsq.FilterValueCompare{
Op: dsq.LessThan,
Value: randValue(),
})

test(new(testFilter))
}

func randValue() []byte {
value := make([]byte, 64)
rand.Read(value)
return value
}

func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, check func(t *testing.T, input, output []dsq.Entry)) {
Expand All @@ -230,8 +264,7 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, check func(t *
for i := 0; i < count; i++ {
s := fmt.Sprintf("%dkey%d", i, i)
key := dstore.NewKey(s).String()
value := make([]byte, 64)
rand.Read(value)
value := randValue()
input = append(input, dsq.Entry{
Key: key,
Value: value,
Expand Down

0 comments on commit ffc9f91

Please sign in to comment.