namespace_test.go 3.65 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7
package namespace_test

import (
	"bytes"
	"sort"
	"testing"

Jakub Sztandera's avatar
Jakub Sztandera committed
8
	. "launchpad.net/gocheck"
9

Jeromy's avatar
Jeromy committed
10 11 12
	ds "github.com/ipfs/go-datastore"
	ns "github.com/ipfs/go-datastore/namespace"
	dsq "github.com/ipfs/go-datastore/query"
13
	dstest "github.com/ipfs/go-datastore/test"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18 19 20 21 22 23
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type DSSuite struct{}

var _ = Suite(&DSSuite{})

func (ks *DSSuite) TestBasic(c *C) {
24 25 26 27 28
	ks.testBasic(c, "abc")
	ks.testBasic(c, "")
}

func (ks *DSSuite) testBasic(c *C, prefix string) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30

	mpds := ds.NewMapDatastore()
31
	nsds := ns.Wrap(mpds, ds.NewKey(prefix))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

	keys := strsToKeys([]string{
		"foo",
		"foo/bar",
		"foo/bar/baz",
		"foo/barb",
		"foo/bar/bazb",
		"foo/bar/baz/barb",
	})

	for _, k := range keys {
		err := nsds.Put(k, []byte(k.String()))
		c.Check(err, Equals, nil)
	}

	for _, k := range keys {
		v1, err := nsds.Get(k)
		c.Check(err, Equals, nil)
		c.Check(bytes.Equal(v1.([]byte), []byte(k.String())), Equals, true)

52
		v2, err := mpds.Get(ds.NewKey(prefix).Child(k))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53 54 55 56
		c.Check(err, Equals, nil)
		c.Check(bytes.Equal(v2.([]byte), []byte(k.String())), Equals, true)
	}

57 58 59 60 61 62 63 64 65
	run := func(d ds.Datastore, q dsq.Query) []ds.Key {
		r, err := d.Query(q)
		c.Check(err, Equals, nil)

		e, err := r.Rest()
		c.Check(err, Equals, nil)

		return ds.EntryKeys(e)
	}
66

67 68
	listA := run(mpds, dsq.Query{})
	listB := run(nsds, dsq.Query{})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71 72 73 74 75 76 77 78 79 80 81
	c.Check(len(listA), Equals, len(listB))

	// sort them cause yeah.
	sort.Sort(ds.KeySlice(listA))
	sort.Sort(ds.KeySlice(listB))

	for i, kA := range listA {
		kB := listB[i]
		c.Check(nsds.InvertKey(kA), Equals, kB)
		c.Check(kA, Equals, nsds.ConvertKey(kB))
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
82
func (ks *DSSuite) TestQuery(c *C) {
83
	mpds := dstest.NewTestDatastore(true)
Łukasz Magiera's avatar
Łukasz Magiera committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	nsds := ns.Wrap(mpds, ds.NewKey("/foo"))

	keys := strsToKeys([]string{
		"abc/foo",
		"bar/foo",
		"foo/bar",
		"foo/bar/baz",
		"foo/baz/abc",
		"xyz/foo",
	})

	for _, k := range keys {
		err := mpds.Put(k, []byte(k.String()))
		c.Check(err, Equals, nil)
	}

	qres, err := nsds.Query(dsq.Query{})
	c.Check(err, Equals, nil)

	expect := []dsq.Entry{
Łukasz Magiera's avatar
Łukasz Magiera committed
104 105 106
		{Key: "/bar", Value: []byte("/foo/bar")},
		{Key: "/bar/baz", Value: []byte("/foo/bar/baz")},
		{Key: "/baz/abc", Value: []byte("/foo/baz/abc")},
Łukasz Magiera's avatar
Łukasz Magiera committed
107 108 109 110
	}

	results, err := qres.Rest()
	c.Check(err, Equals, nil)
Łukasz Magiera's avatar
Łukasz Magiera committed
111
	sort.Slice(results, func(i, j int) bool { return results[i].Key < results[j].Key })
Łukasz Magiera's avatar
Łukasz Magiera committed
112 113 114 115 116 117 118 119 120 121 122

	for i, ent := range results {
		c.Check(ent.Key, Equals, expect[i].Key)
		entval, _ := ent.Value.([]byte)
		expval, _ := expect[i].Value.([]byte)
		c.Check(string(entval), Equals, string(expval))
	}

	err = qres.Close()
	c.Check(err, Equals, nil)

Łukasz Magiera's avatar
Łukasz Magiera committed
123
	qres, err = nsds.Query(dsq.Query{Prefix: "bar"})
Łukasz Magiera's avatar
Łukasz Magiera committed
124 125 126
	c.Check(err, Equals, nil)

	expect = []dsq.Entry{
Łukasz Magiera's avatar
Łukasz Magiera committed
127 128
		{Key: "/bar", Value: []byte("/foo/bar")},
		{Key: "/bar/baz", Value: []byte("/foo/bar/baz")},
Łukasz Magiera's avatar
Łukasz Magiera committed
129 130 131 132
	}

	results, err = qres.Rest()
	c.Check(err, Equals, nil)
Łukasz Magiera's avatar
Łukasz Magiera committed
133
	sort.Slice(results, func(i, j int) bool { return results[i].Key < results[j].Key })
Łukasz Magiera's avatar
Łukasz Magiera committed
134 135 136 137 138 139 140

	for i, ent := range results {
		c.Check(ent.Key, Equals, expect[i].Key)
		entval, _ := ent.Value.([]byte)
		expval, _ := expect[i].Value.([]byte)
		c.Check(string(entval), Equals, string(expval))
	}
141 142 143 144 145 146 147 148 149 150 151 152

	if err := nsds.Datastore.(ds.CheckedDatastore).Check(); err != dstest.TestError {
		c.Errorf("Unexpected Check() error: %s", err)
	}

	if err := nsds.Datastore.(ds.GCDatastore).CollectGarbage(); err != dstest.TestError {
		c.Errorf("Unexpected CollectGarbage() error: %s", err)
	}

	if err := nsds.Datastore.(ds.ScrubbedDatastore).Scrub(); err != dstest.TestError {
		c.Errorf("Unexpected Scrub() error: %s", err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
153 154
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155 156 157 158 159 160 161
func strsToKeys(strs []string) []ds.Key {
	keys := make([]ds.Key, len(strs))
	for i, s := range strs {
		keys[i] = ds.NewKey(s)
	}
	return keys
}