Skip to content

Commit

Permalink
Merge pull request #136 from ipfs/feat/test
Browse files Browse the repository at this point in the history
Add a large test suite
  • Loading branch information
Stebalien authored Oct 1, 2019
2 parents 0431387 + 113fe4c commit d0ca9bc
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
language: go

go:
- 1.11.x
- 1.12.x

env:
global:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ require (
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

go 1.12
7 changes: 3 additions & 4 deletions mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func (d *Datastore) Delete(key ds.Key) error {
func (d *Datastore) Query(master query.Query) (query.Results, error) {
childQuery := query.Query{
Prefix: master.Prefix,
Limit: master.Limit,
Orders: master.Orders,
KeysOnly: master.KeysOnly,
ReturnExpirations: master.ReturnExpirations,
Expand Down Expand Up @@ -254,7 +253,7 @@ func (d *Datastore) Query(master query.Query) (query.Results, error) {
queries.addResults(mount, results)
}

qr := query.ResultsFromIterator(childQuery, query.Iterator{
qr := query.ResultsFromIterator(master, query.Iterator{
Next: queries.next,
Close: queries.close,
})
Expand All @@ -269,8 +268,8 @@ func (d *Datastore) Query(master query.Query) (query.Results, error) {
qr = query.NaiveOffset(qr, master.Offset)
}

if childQuery.Limit > 0 {
qr = query.NaiveLimit(qr, childQuery.Limit)
if master.Limit > 0 {
qr = query.NaiveLimit(qr, master.Limit)
}

return qr, nil
Expand Down
12 changes: 12 additions & 0 deletions query/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (f FilterValueCompare) Filter(e Entry) bool {
}
}

func (f FilterValueCompare) String() string {
return fmt.Sprintf("VALUE %s %q", f.Op, string(f.Value))
}

type FilterKeyCompare struct {
Op Op
Key string
Expand All @@ -81,10 +85,18 @@ func (f FilterKeyCompare) Filter(e Entry) bool {
}
}

func (f FilterKeyCompare) String() string {
return fmt.Sprintf("KEY %s %q", f.Op, f.Key)
}

type FilterKeyPrefix struct {
Prefix string
}

func (f FilterKeyPrefix) Filter(e Entry) bool {
return strings.HasPrefix(e.Key, f.Prefix)
}

func (f FilterKeyPrefix) String() string {
return fmt.Sprintf("PREFIX(%q)", f.Prefix)
}
20 changes: 20 additions & 0 deletions query/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (o OrderByFunction) Compare(a, b Entry) int {
return o(a, b)
}

func (OrderByFunction) String() string {
return "FN"
}

// OrderByValue is used to signal to datastores they should apply internal
// orderings.
type OrderByValue struct{}
Expand All @@ -26,6 +30,10 @@ func (o OrderByValue) Compare(a, b Entry) int {
return bytes.Compare(a.Value, b.Value)
}

func (OrderByValue) String() string {
return "VALUE"
}

// OrderByValueDescending is used to signal to datastores they
// should apply internal orderings.
type OrderByValueDescending struct{}
Expand All @@ -34,20 +42,32 @@ func (o OrderByValueDescending) Compare(a, b Entry) int {
return -bytes.Compare(a.Value, b.Value)
}

func (OrderByValueDescending) String() string {
return "desc(VALUE)"
}

// OrderByKey
type OrderByKey struct{}

func (o OrderByKey) Compare(a, b Entry) int {
return strings.Compare(a.Key, b.Key)
}

func (OrderByKey) String() string {
return "KEY"
}

// OrderByKeyDescending
type OrderByKeyDescending struct{}

func (o OrderByKeyDescending) Compare(a, b Entry) int {
return -strings.Compare(a.Key, b.Key)
}

func (OrderByKeyDescending) String() string {
return "desc(KEY)"
}

// Less returns true if a comes before b with the requested orderings.
func Less(orders []Order, a, b Entry) bool {
for _, cmp := range orders {
Expand Down
43 changes: 43 additions & 0 deletions query/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package query

import (
"fmt"
"time"

goprocess "github.com/jbenet/goprocess"
Expand Down Expand Up @@ -67,6 +68,48 @@ type Query struct {
ReturnExpirations bool // return expirations (see TTLDatastore)
}

func (q Query) String() string {
s := "SELECT keys"
if !q.KeysOnly {
s += ",vals"
}
if q.ReturnExpirations {
s += ",exps"
}

s += " "

if q.Prefix != "" {
s += fmt.Sprintf("FROM %q ", q.Prefix)
}

if len(q.Filters) > 0 {
s += fmt.Sprintf("FILTER [%s", q.Filters[0])
for _, f := range q.Filters[1:] {
s += fmt.Sprintf(", %s", f)
}
s += "] "
}

if len(q.Orders) > 0 {
s += fmt.Sprintf("ORDER [%s", q.Orders[0])
for _, f := range q.Orders[1:] {
s += fmt.Sprintf(", %s", f)
}
s += "] "
}

if q.Offset > 0 {
s += fmt.Sprintf("OFFSET %d ", q.Offset)
}

if q.Limit > 0 {
s += fmt.Sprintf("LIMIT %d ", q.Limit)
}
// Will always end with a space, strip it.
return s[:len(s)-1]
}

// Entry is a query result entry.
type Entry struct {
Key string // cant be ds.Key because circular imports ...!!!
Expand Down
62 changes: 62 additions & 0 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,65 @@ func getKeysViaChan(rs Results) []string {
}
return ret
}

func TestStringer(t *testing.T) {
q := Query{}

expected := `SELECT keys,vals`
actual := q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}

q.Offset = 10
q.Limit = 10
expected = `SELECT keys,vals OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}

q.Orders = []Order{OrderByValue{}, OrderByKey{}}
expected = `SELECT keys,vals ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}

q.Filters = []Filter{
FilterKeyCompare{Op: GreaterThan, Key: "/foo/bar"},
FilterKeyCompare{Op: LessThan, Key: "/foo/bar"},
}
expected = `SELECT keys,vals FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}

q.Prefix = "/foo"
expected = `SELECT keys,vals FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}

q.ReturnExpirations = true
expected = `SELECT keys,vals,exps FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}

q.KeysOnly = true
expected = `SELECT keys,exps FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}
q.ReturnExpirations = false
expected = `SELECT keys FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
actual = q.String()
if actual != expected {
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
}
}
Loading

0 comments on commit d0ca9bc

Please sign in to comment.