Commit f2327ba5 authored by Steven Allen's avatar Steven Allen

test(suite): add a bunch of prefix tests for the new behavior

parent 83f9a950
...@@ -841,9 +841,9 @@ func TestSuite(t *testing.T) { ...@@ -841,9 +841,9 @@ func TestSuite(t *testing.T) {
mapds2 := datastore.NewMapDatastore() mapds2 := datastore.NewMapDatastore()
mapds3 := datastore.NewMapDatastore() mapds3 := datastore.NewMapDatastore()
m := mount.New([]mount.Mount{ m := mount.New([]mount.Mount{
{Prefix: datastore.NewKey("/foo"), Datastore: mapds1}, {Prefix: datastore.NewKey("/prefix"), Datastore: mapds1},
{Prefix: datastore.NewKey("/bar"), Datastore: mapds2}, {Prefix: datastore.NewKey("/prefix/sub"), Datastore: mapds2},
{Prefix: datastore.NewKey("/baz"), Datastore: mapds3}, {Prefix: datastore.NewKey("/0"), Datastore: mapds3},
{Prefix: datastore.NewKey("/"), Datastore: mapds0}, {Prefix: datastore.NewKey("/"), Datastore: mapds0},
}) })
dstest.SubtestAll(t, m) dstest.SubtestAll(t, m)
......
...@@ -177,27 +177,27 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) { ...@@ -177,27 +177,27 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) {
} }
func SubtestBasicSync(t *testing.T, ds dstore.Datastore) { func SubtestBasicSync(t *testing.T, ds dstore.Datastore) {
if err := ds.Sync(dstore.NewKey("foo")); err != nil { if err := ds.Sync(dstore.NewKey("prefix")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := ds.Put(dstore.NewKey("/foo"), []byte("foo")); err != nil { if err := ds.Put(dstore.NewKey("/prefix"), []byte("foo")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := ds.Sync(dstore.NewKey("/foo")); err != nil { if err := ds.Sync(dstore.NewKey("/prefix")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := ds.Put(dstore.NewKey("/foo/bar"), []byte("bar")); err != nil { if err := ds.Put(dstore.NewKey("/prefix/sub"), []byte("bar")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := ds.Sync(dstore.NewKey("/foo")); err != nil { if err := ds.Sync(dstore.NewKey("/prefix")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := ds.Sync(dstore.NewKey("/foo/bar")); err != nil { if err := ds.Sync(dstore.NewKey("/prefix/sub")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -237,6 +237,11 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) { ...@@ -237,6 +237,11 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
Key: "/2", Key: "/2",
}}, }},
} }
prefixes := []string{
"",
"/prefix",
"/0", // keys exist under this prefix but they shouldn't match.
}
orders := [][]dsq.Order{ orders := [][]dsq.Order{
{dsq.OrderByKey{}}, {dsq.OrderByKey{}},
{dsq.OrderByKeyDescending{}}, {dsq.OrderByKeyDescending{}},
...@@ -255,8 +260,9 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) { ...@@ -255,8 +260,9 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
Limit: limits[perm[1]], Limit: limits[perm[1]],
Filters: filters[perm[2]], Filters: filters[perm[2]],
Orders: orders[perm[3]], Orders: orders[perm[3]],
Prefix: prefixes[perm[4]],
} }
length := lengths[perm[4]] length := lengths[perm[5]]
t.Run(strings.ReplaceAll(fmt.Sprintf("%d/{%s}", length, q), " ", "·"), func(t *testing.T) { t.Run(strings.ReplaceAll(fmt.Sprintf("%d/{%s}", length, q), " ", "·"), func(t *testing.T) {
subtestQuery(t, ds, q, length) subtestQuery(t, ds, q, length)
...@@ -266,6 +272,7 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) { ...@@ -266,6 +272,7 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
len(limits), len(limits),
len(filters), len(filters),
len(orders), len(orders),
len(prefixes),
len(lengths), len(lengths),
) )
} }
...@@ -335,6 +342,28 @@ func SubtestReturnSizes(t *testing.T, ds dstore.Datastore) { ...@@ -335,6 +342,28 @@ func SubtestReturnSizes(t *testing.T, ds dstore.Datastore) {
subtestQuery(t, ds, dsq.Query{ReturnsSizes: true}, 100) subtestQuery(t, ds, dsq.Query{ReturnsSizes: true}, 100)
} }
func SubtestPrefix(t *testing.T, ds dstore.Datastore) {
test := func(prefix string) {
t.Run(prefix, func(t *testing.T) {
subtestQuery(t, ds, dsq.Query{
Prefix: prefix,
}, 100)
})
}
test("")
test("/")
test("/./")
test("/.././/")
test("/prefix/../")
test("/prefix")
test("/prefix/")
test("/prefix/sub/")
test("/0/")
test("/bad/")
}
func randValue() []byte { func randValue() []byte {
value := make([]byte, 64) value := make([]byte, 64)
rand.Read(value) rand.Read(value)
...@@ -354,6 +383,28 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) { ...@@ -354,6 +383,28 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) {
}) })
} }
for i := 0; i < count; i++ {
s := fmt.Sprintf("/prefix/%dkey%d", i, i)
key := dstore.NewKey(s).String()
value := randValue()
input = append(input, dsq.Entry{
Key: key,
Size: len(value),
Value: value,
})
}
for i := 0; i < count; i++ {
s := fmt.Sprintf("/prefix/sub/%dkey%d", i, i)
key := dstore.NewKey(s).String()
value := randValue()
input = append(input, dsq.Entry{
Key: key,
Size: len(value),
Value: value,
})
}
t.Logf("putting %d values", count) t.Logf("putting %d values", count)
for i, e := range input { for i, e := range input {
err := ds.Put(dstore.RawKey(e.Key), e.Value) err := ds.Put(dstore.RawKey(e.Key), e.Value)
......
...@@ -14,6 +14,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){ ...@@ -14,6 +14,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
SubtestBasicPutGet, SubtestBasicPutGet,
SubtestNotFounds, SubtestNotFounds,
SubtestCombinations, SubtestCombinations,
SubtestPrefix,
SubtestOrder, SubtestOrder,
SubtestLimit, SubtestLimit,
SubtestFilter, SubtestFilter,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment