Skip to content

Commit d0ca9bc

Browse files
authored
Merge pull request #136 from ipfs/feat/test
Add a large test suite
2 parents 0431387 + 113fe4c commit d0ca9bc

File tree

9 files changed

+273
-61
lines changed

9 files changed

+273
-61
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ os:
44
language: go
55

66
go:
7-
- 1.11.x
7+
- 1.12.x
88

99
env:
1010
global:

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ require (
99
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
1010
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
1111
)
12+
13+
go 1.12

mount/mount.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ func (d *Datastore) Delete(key ds.Key) error {
224224
func (d *Datastore) Query(master query.Query) (query.Results, error) {
225225
childQuery := query.Query{
226226
Prefix: master.Prefix,
227-
Limit: master.Limit,
228227
Orders: master.Orders,
229228
KeysOnly: master.KeysOnly,
230229
ReturnExpirations: master.ReturnExpirations,
@@ -254,7 +253,7 @@ func (d *Datastore) Query(master query.Query) (query.Results, error) {
254253
queries.addResults(mount, results)
255254
}
256255

257-
qr := query.ResultsFromIterator(childQuery, query.Iterator{
256+
qr := query.ResultsFromIterator(master, query.Iterator{
258257
Next: queries.next,
259258
Close: queries.close,
260259
})
@@ -269,8 +268,8 @@ func (d *Datastore) Query(master query.Query) (query.Results, error) {
269268
qr = query.NaiveOffset(qr, master.Offset)
270269
}
271270

272-
if childQuery.Limit > 0 {
273-
qr = query.NaiveLimit(qr, childQuery.Limit)
271+
if master.Limit > 0 {
272+
qr = query.NaiveLimit(qr, master.Limit)
274273
}
275274

276275
return qr, nil

query/filter.go

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (f FilterValueCompare) Filter(e Entry) bool {
5757
}
5858
}
5959

60+
func (f FilterValueCompare) String() string {
61+
return fmt.Sprintf("VALUE %s %q", f.Op, string(f.Value))
62+
}
63+
6064
type FilterKeyCompare struct {
6165
Op Op
6266
Key string
@@ -81,10 +85,18 @@ func (f FilterKeyCompare) Filter(e Entry) bool {
8185
}
8286
}
8387

88+
func (f FilterKeyCompare) String() string {
89+
return fmt.Sprintf("KEY %s %q", f.Op, f.Key)
90+
}
91+
8492
type FilterKeyPrefix struct {
8593
Prefix string
8694
}
8795

8896
func (f FilterKeyPrefix) Filter(e Entry) bool {
8997
return strings.HasPrefix(e.Key, f.Prefix)
9098
}
99+
100+
func (f FilterKeyPrefix) String() string {
101+
return fmt.Sprintf("PREFIX(%q)", f.Prefix)
102+
}

query/order.go

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func (o OrderByFunction) Compare(a, b Entry) int {
1818
return o(a, b)
1919
}
2020

21+
func (OrderByFunction) String() string {
22+
return "FN"
23+
}
24+
2125
// OrderByValue is used to signal to datastores they should apply internal
2226
// orderings.
2327
type OrderByValue struct{}
@@ -26,6 +30,10 @@ func (o OrderByValue) Compare(a, b Entry) int {
2630
return bytes.Compare(a.Value, b.Value)
2731
}
2832

33+
func (OrderByValue) String() string {
34+
return "VALUE"
35+
}
36+
2937
// OrderByValueDescending is used to signal to datastores they
3038
// should apply internal orderings.
3139
type OrderByValueDescending struct{}
@@ -34,20 +42,32 @@ func (o OrderByValueDescending) Compare(a, b Entry) int {
3442
return -bytes.Compare(a.Value, b.Value)
3543
}
3644

45+
func (OrderByValueDescending) String() string {
46+
return "desc(VALUE)"
47+
}
48+
3749
// OrderByKey
3850
type OrderByKey struct{}
3951

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

56+
func (OrderByKey) String() string {
57+
return "KEY"
58+
}
59+
4460
// OrderByKeyDescending
4561
type OrderByKeyDescending struct{}
4662

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

67+
func (OrderByKeyDescending) String() string {
68+
return "desc(KEY)"
69+
}
70+
5171
// Less returns true if a comes before b with the requested orderings.
5272
func Less(orders []Order, a, b Entry) bool {
5373
for _, cmp := range orders {

query/query.go

+43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package query
22

33
import (
4+
"fmt"
45
"time"
56

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

71+
func (q Query) String() string {
72+
s := "SELECT keys"
73+
if !q.KeysOnly {
74+
s += ",vals"
75+
}
76+
if q.ReturnExpirations {
77+
s += ",exps"
78+
}
79+
80+
s += " "
81+
82+
if q.Prefix != "" {
83+
s += fmt.Sprintf("FROM %q ", q.Prefix)
84+
}
85+
86+
if len(q.Filters) > 0 {
87+
s += fmt.Sprintf("FILTER [%s", q.Filters[0])
88+
for _, f := range q.Filters[1:] {
89+
s += fmt.Sprintf(", %s", f)
90+
}
91+
s += "] "
92+
}
93+
94+
if len(q.Orders) > 0 {
95+
s += fmt.Sprintf("ORDER [%s", q.Orders[0])
96+
for _, f := range q.Orders[1:] {
97+
s += fmt.Sprintf(", %s", f)
98+
}
99+
s += "] "
100+
}
101+
102+
if q.Offset > 0 {
103+
s += fmt.Sprintf("OFFSET %d ", q.Offset)
104+
}
105+
106+
if q.Limit > 0 {
107+
s += fmt.Sprintf("LIMIT %d ", q.Limit)
108+
}
109+
// Will always end with a space, strip it.
110+
return s[:len(s)-1]
111+
}
112+
70113
// Entry is a query result entry.
71114
type Entry struct {
72115
Key string // cant be ds.Key because circular imports ...!!!

query/query_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,65 @@ func getKeysViaChan(rs Results) []string {
248248
}
249249
return ret
250250
}
251+
252+
func TestStringer(t *testing.T) {
253+
q := Query{}
254+
255+
expected := `SELECT keys,vals`
256+
actual := q.String()
257+
if actual != expected {
258+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
259+
}
260+
261+
q.Offset = 10
262+
q.Limit = 10
263+
expected = `SELECT keys,vals OFFSET 10 LIMIT 10`
264+
actual = q.String()
265+
if actual != expected {
266+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
267+
}
268+
269+
q.Orders = []Order{OrderByValue{}, OrderByKey{}}
270+
expected = `SELECT keys,vals ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
271+
actual = q.String()
272+
if actual != expected {
273+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
274+
}
275+
276+
q.Filters = []Filter{
277+
FilterKeyCompare{Op: GreaterThan, Key: "/foo/bar"},
278+
FilterKeyCompare{Op: LessThan, Key: "/foo/bar"},
279+
}
280+
expected = `SELECT keys,vals FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
281+
actual = q.String()
282+
if actual != expected {
283+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
284+
}
285+
286+
q.Prefix = "/foo"
287+
expected = `SELECT keys,vals FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
288+
actual = q.String()
289+
if actual != expected {
290+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
291+
}
292+
293+
q.ReturnExpirations = true
294+
expected = `SELECT keys,vals,exps FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
295+
actual = q.String()
296+
if actual != expected {
297+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
298+
}
299+
300+
q.KeysOnly = true
301+
expected = `SELECT keys,exps FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
302+
actual = q.String()
303+
if actual != expected {
304+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
305+
}
306+
q.ReturnExpirations = false
307+
expected = `SELECT keys FROM "/foo" FILTER [KEY > "/foo/bar", KEY < "/foo/bar"] ORDER [VALUE, KEY] OFFSET 10 LIMIT 10`
308+
actual = q.String()
309+
if actual != expected {
310+
t.Fatalf("expected\n\t%s\ngot\n\t%s", expected, actual)
311+
}
312+
}

0 commit comments

Comments
 (0)