Skip to content

Commit

Permalink
Merge pull request #145 from ipfs/feat/enhance-tests
Browse files Browse the repository at this point in the history
test(suite): add a bunch of prefix tests for the new behavior
  • Loading branch information
Stebalien committed Feb 11, 2020
2 parents 83f9a95 + f2327ba commit b72ac7e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions mount/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 58 additions & 7 deletions test/basic_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{}},
Expand All @@ -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)
Expand All @@ -266,6 +272,7 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
len(limits),
len(filters),
len(orders),
len(prefixes),
len(lengths),
)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
SubtestBasicPutGet,
SubtestNotFounds,
SubtestCombinations,
SubtestPrefix,
SubtestOrder,
SubtestLimit,
SubtestFilter,
Expand Down

0 comments on commit b72ac7e

Please sign in to comment.