diff --git a/go.sum b/go.sum index 8d4fd79..d128972 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,7 @@ github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8 h1:bspPhN+oKYFk5f github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= diff --git a/mount/mount_test.go b/mount/mount_test.go index 7d88b0a..a3059b4 100644 --- a/mount/mount_test.go +++ b/mount/mount_test.go @@ -841,9 +841,9 @@ func TestSuite(t *testing.T) { mapds2 := datastore.NewMapDatastore() mapds3 := datastore.NewMapDatastore() m := mount.New([]mount.Mount{ - {Prefix: datastore.NewKey("/foo"), Datastore: mapds1}, - {Prefix: datastore.NewKey("/bar"), Datastore: mapds2}, - {Prefix: datastore.NewKey("/baz"), Datastore: mapds3}, + {Prefix: datastore.NewKey("/prefix"), Datastore: mapds1}, + {Prefix: datastore.NewKey("/prefix/sub"), Datastore: mapds2}, + {Prefix: datastore.NewKey("/0"), Datastore: mapds3}, {Prefix: datastore.NewKey("/"), Datastore: mapds0}, }) dstest.SubtestAll(t, m) diff --git a/test/basic_tests.go b/test/basic_tests.go index 0506ee5..6b8d10d 100644 --- a/test/basic_tests.go +++ b/test/basic_tests.go @@ -177,27 +177,27 @@ func SubtestManyKeysAndQuery(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) } - 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) } - if err := ds.Sync(dstore.NewKey("/foo")); err != nil { + if err := ds.Sync(dstore.NewKey("/prefix")); err != nil { 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) } - if err := ds.Sync(dstore.NewKey("/foo")); err != nil { + if err := ds.Sync(dstore.NewKey("/prefix")); err != nil { 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) } @@ -237,6 +237,11 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) { Key: "/2", }}, } + prefixes := []string{ + "", + "/prefix", + "/0", // keys exist under this prefix but they shouldn't match. + } orders := [][]dsq.Order{ {dsq.OrderByKey{}}, {dsq.OrderByKeyDescending{}}, @@ -255,8 +260,9 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) { Limit: limits[perm[1]], Filters: filters[perm[2]], 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) { subtestQuery(t, ds, q, length) @@ -266,6 +272,7 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) { len(limits), len(filters), len(orders), + len(prefixes), len(lengths), ) } @@ -335,6 +342,28 @@ func SubtestReturnSizes(t *testing.T, ds dstore.Datastore) { 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 { value := make([]byte, 64) rand.Read(value) @@ -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) for i, e := range input { err := ds.Put(dstore.RawKey(e.Key), e.Value) diff --git a/test/suite.go b/test/suite.go index afebd1c..012f802 100644 --- a/test/suite.go +++ b/test/suite.go @@ -14,6 +14,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){ SubtestBasicPutGet, SubtestNotFounds, SubtestCombinations, + SubtestPrefix, SubtestOrder, SubtestLimit, SubtestFilter,