key_test.go 4.48 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package datastore_test

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
3 4 5 6 7 8
import (
	"bytes"
	"math/rand"
	"path"
	"strings"
	"testing"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
9 10

	. "github.com/jbenet/datastore.go"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
11
	. "github.com/jbenet/datastore.go/Godeps/_workspace/src/gopkg.in/check.v1"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14 15 16 17
)

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

func randomString() string {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
18 19 20 21 22 23 24
	chars := "abcdefghijklmnopqrstuvwxyz1234567890"
	var buf bytes.Buffer
	l := rand.Intn(50)
	for j := 0; j < l; j++ {
		buf.WriteByte(chars[rand.Intn(len(chars))])
	}
	return buf.String()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27
}

type KeySuite struct{}
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
28

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31
var _ = Suite(&KeySuite{})

func (ks *KeySuite) SubtestKey(s string, c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	fixed := path.Clean("/" + s)
	namespaces := strings.Split(fixed, "/")[1:]
	lastNamespace := namespaces[len(namespaces)-1]
	lnparts := strings.Split(lastNamespace, ":")
	ktype := ""
	if len(lnparts) > 1 {
		ktype = strings.Join(lnparts[:len(lnparts)-1], ":")
	}
	kname := lnparts[len(lnparts)-1]

	kchild := path.Clean(fixed + "/cchildd")
	kparent := "/" + strings.Join(append(namespaces[:len(namespaces)-1]), "/")
	kpath := path.Clean(kparent + "/" + ktype)
	kinstance := fixed + ":" + "inst"

	c.Log("Testing: ", NewKey(s))

	c.Check(NewKey(s).String(), Equals, fixed)
	c.Check(NewKey(s), Equals, NewKey(s))
	c.Check(NewKey(s).String(), Equals, NewKey(s).String())
	c.Check(NewKey(s).Name(), Equals, kname)
	c.Check(NewKey(s).Type(), Equals, ktype)
	c.Check(NewKey(s).Path().String(), Equals, kpath)
	c.Check(NewKey(s).Instance("inst").String(), Equals, kinstance)

	c.Check(NewKey(s).Child("cchildd").String(), Equals, kchild)
	c.Check(NewKey(s).Child("cchildd").Parent().String(), Equals, fixed)
	c.Check(NewKey(s).Parent().String(), Equals, kparent)
	c.Check(len(NewKey(s).List()), Equals, len(namespaces))
	c.Check(len(NewKey(s).Namespaces()), Equals, len(namespaces))
	for i, e := range NewKey(s).List() {
		c.Check(namespaces[i], Equals, e)
	}
65 66 67 68 69 70 71 72

	c.Check(NewKey(s), Equals, NewKey(s))
	c.Check(NewKey(s).Equal(NewKey(s)), Equals, true)
	c.Check(NewKey(s).Equal(NewKey("/fdsafdsa/"+s)), Equals, false)

	// less
	c.Check(NewKey(s).Less(NewKey(s).Parent()), Equals, false)
	c.Check(NewKey(s).Less(NewKey(s).Child("foo")), Equals, true)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75
}

func (ks *KeySuite) TestKeyBasic(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
76 77 78 79 80 81 82 83 84 85 86
	ks.SubtestKey("", c)
	ks.SubtestKey("abcde", c)
	ks.SubtestKey("disahfidsalfhduisaufidsail", c)
	ks.SubtestKey("/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/", c)
	ks.SubtestKey("4215432143214321432143214321", c)
	ks.SubtestKey("/fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/", c)
	ks.SubtestKey("abcde:fdsfd", c)
	ks.SubtestKey("disahfidsalfhduisaufidsail:fdsa", c)
	ks.SubtestKey("/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/:", c)
	ks.SubtestKey("4215432143214321432143214321:", c)
	ks.SubtestKey("fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/f:fdaf", c)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89
}

func CheckTrue(c *C, cond bool) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
90
	c.Check(cond, Equals, true)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93
}

func (ks *KeySuite) TestKeyAncestry(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	k1 := NewKey("/A/B/C")
	k2 := NewKey("/A/B/C/D")

	c.Check(k1.String(), Equals, "/A/B/C")
	c.Check(k2.String(), Equals, "/A/B/C/D")
	CheckTrue(c, k1.IsAncestorOf(k2))
	CheckTrue(c, k2.IsDescendantOf(k1))
	CheckTrue(c, NewKey("/A").IsAncestorOf(k2))
	CheckTrue(c, NewKey("/A").IsAncestorOf(k1))
	CheckTrue(c, !NewKey("/A").IsDescendantOf(k2))
	CheckTrue(c, !NewKey("/A").IsDescendantOf(k1))
	CheckTrue(c, k2.IsDescendantOf(NewKey("/A")))
	CheckTrue(c, k1.IsDescendantOf(NewKey("/A")))
	CheckTrue(c, !k2.IsAncestorOf(NewKey("/A")))
	CheckTrue(c, !k1.IsAncestorOf(NewKey("/A")))
	CheckTrue(c, !k2.IsAncestorOf(k2))
	CheckTrue(c, !k1.IsAncestorOf(k1))
	c.Check(k1.Child("D").String(), Equals, k2.String())
	c.Check(k1.String(), Equals, k2.Parent().String())
	c.Check(k1.Path().String(), Equals, k2.Parent().Path().String())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114 115 116
}

func (ks *KeySuite) TestType(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
117 118 119 120 121 122 123 124
	k1 := NewKey("/A/B/C:c")
	k2 := NewKey("/A/B/C:c/D:d")

	CheckTrue(c, k1.IsAncestorOf(k2))
	CheckTrue(c, k2.IsDescendantOf(k1))
	c.Check(k1.Type(), Equals, "C")
	c.Check(k2.Type(), Equals, "D")
	c.Check(k1.Type(), Equals, k2.Parent().Type())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
125 126 127
}

func (ks *KeySuite) TestRandom(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
128 129 130 131 132 133 134 135
	keys := map[Key]bool{}
	for i := 0; i < 1000; i++ {
		r := RandomKey()
		_, found := keys[r]
		CheckTrue(c, !found)
		keys[r] = true
	}
	CheckTrue(c, len(keys) == 1000)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
}
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

func (ks *KeySuite) TestLess(c *C) {

	checkLess := func(a, b string) {
		ak := NewKey(a)
		bk := NewKey(b)
		c.Check(ak.Less(bk), Equals, true)
		c.Check(bk.Less(ak), Equals, false)
	}

	checkLess("/a/b/c", "/a/b/c/d")
	checkLess("/a/b", "/a/b/c/d")
	checkLess("/a", "/a/b/c/d")
	checkLess("/a/a/c", "/a/b/c")
	checkLess("/a/a/d", "/a/b/c")
	checkLess("/a/b/c/d/e/f/g/h", "/b")
	checkLess("/", "/a")
}