Skip to content

Commit

Permalink
Fix bug with NaiveQueryApply and add corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelavila committed Mar 22, 2019
1 parent 37e058e commit f4f30df
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion query/query_impl.go
Expand Up @@ -125,7 +125,7 @@ func NaiveQueryApply(q Query, qr Results) Results {
}
if q.Limit != 0 {
// TODO: Offset?
qr = NaiveLimit(qr, q.Offset)
qr = NaiveLimit(qr, q.Limit)
}
return qr
}
Expand Down
64 changes: 64 additions & 0 deletions query/query_test.go
Expand Up @@ -40,6 +40,70 @@ func testResults(t *testing.T, res Results, expect []string) {
}
}

func TestNaiveQueryApply(t *testing.T) {
testNaiveQueryApply := func(t *testing.T, query Query, keys []string, expect []string) {
e := make([]Entry, len(keys))
for i, k := range keys {
e[i] = Entry{Key: k}
}

res := ResultsWithEntries(query, e)
res = NaiveQueryApply(query, res)

testResults(t, res, expect)
}

q := Query{Limit: 2}

testNaiveQueryApply(t, q, sampleKeys, []string{
"/ab/c",
"/ab/cd",
})

q = Query{Offset: 3, Limit: 2}
testNaiveQueryApply(t, q, sampleKeys, []string{
"/abce",
"/abcf",
})

f := &FilterKeyCompare{Op: Equal, Key: "/ab"}
q = Query{Filters: []Filter{f}}
testNaiveQueryApply(t, q, sampleKeys, []string{
"/ab",
})

q = Query{Prefix: "/ab"}
testNaiveQueryApply(t, q, sampleKeys, []string{
"/ab/c",
"/ab/cd",
"/abce",
"/abcf",
"/ab",
})

q = Query{Orders: []Order{OrderByKeyDescending{}}}
testNaiveQueryApply(t, q, sampleKeys, []string{
"/abcf",
"/abce",
"/ab/cd",
"/ab/c",
"/ab",
"/a",
})

q = Query{
Limit: 3,
Offset: 2,
Prefix: "/ab",
Orders: []Order{OrderByKey{}},
}
testNaiveQueryApply(t, q, sampleKeys, []string{
"/ab/cd",
"/abce",
"/abcf",
})
}

func TestLimit(t *testing.T) {
testKeyLimit := func(t *testing.T, limit int, keys []string, expect []string) {
e := make([]Entry, len(keys))
Expand Down

0 comments on commit f4f30df

Please sign in to comment.