key_test.go 3.85 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
package datastore_test

import(
  "bytes"
  . "github.com/jbenet/datastore.go"
  . "launchpad.net/gocheck"
  "math/rand"
  "path"
  "testing"
  "strings"
)

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

func randomString() string {
  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()
}

type KeySuite struct{}
var _ = Suite(&KeySuite{})

func (ks *KeySuite) SubtestKey(s string, c *C) {
  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)
  }
}

func (ks *KeySuite) TestKeyBasic(c *C) {
  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)
}

func CheckTrue(c *C, cond bool) {
  c.Check(cond, Equals, true)
}

func (ks *KeySuite) TestKeyAncestry(c *C) {
  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())
}

func (ks *KeySuite) TestType(c *C) {
  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())
}

func (ks *KeySuite) TestRandom(c *C) {
  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)
}