key_test.go 3.75 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 9 10
import (
	"bytes"
	. "github.com/jbenet/datastore.go"
	. "launchpad.net/gocheck"
	"math/rand"
	"path"
	"strings"
	"testing"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15 16
)

// 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
17 18 19 20 21 22 23
	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
24 25 26
}

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

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

func (ks *KeySuite) SubtestKey(s string, c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
31 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
	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)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65 66
}

func (ks *KeySuite) TestKeyBasic(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
67 68 69 70 71 72 73 74 75 76 77
	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
78 79 80
}

func CheckTrue(c *C, cond bool) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
81
	c.Check(cond, Equals, true)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84
}

func (ks *KeySuite) TestKeyAncestry(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	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
105 106 107
}

func (ks *KeySuite) TestType(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
108 109 110 111 112 113 114 115
	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
116 117 118
}

func (ks *KeySuite) TestRandom(c *C) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
119 120 121 122 123 124 125 126
	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
127
}