| Index: service/datastore/dskey/key_test.go
|
| diff --git a/service/datastore/key_test.go b/service/datastore/dskey/key_test.go
|
| similarity index 62%
|
| rename from service/datastore/key_test.go
|
| rename to service/datastore/dskey/key_test.go
|
| index 3e3f2b7e6c3ee6ef0d4889d6fb5235495654a411..d39b885029819e4e964a80104b7f26a1b5c0ba7c 100644
|
| --- a/service/datastore/key_test.go
|
| +++ b/service/datastore/dskey/key_test.go
|
| @@ -2,21 +2,23 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -package datastore
|
| +package dskey
|
|
|
| import (
|
| "encoding/json"
|
| "fmt"
|
| "testing"
|
|
|
| + ds "github.com/luci/gae/service/datastore"
|
| + . "github.com/luci/luci-go/common/testing/assertions"
|
| . "github.com/smartystreets/goconvey/convey"
|
| )
|
|
|
| -func mkKey(aid, ns string, elems ...interface{}) Key {
|
| +func mkKey(aid, ns string, elems ...interface{}) ds.Key {
|
| if len(elems)%2 != 0 {
|
| panic("odd number of tokens")
|
| }
|
| - toks := make([]KeyTok, len(elems)/2)
|
| + toks := make([]ds.KeyTok, len(elems)/2)
|
| for i := 0; i < len(elems); i += 2 {
|
| toks[i/2].Kind = elems[i].(string)
|
| switch x := elems[i+1].(type) {
|
| @@ -28,14 +30,14 @@ func mkKey(aid, ns string, elems ...interface{}) Key {
|
| panic("bad token id")
|
| }
|
| }
|
| - return NewKeyToks(aid, ns, toks)
|
| + return NewToks(aid, ns, toks)
|
| }
|
|
|
| func ShouldEqualKey(actual interface{}, expected ...interface{}) string {
|
| if len(expected) != 1 {
|
| return fmt.Sprintf("Assertion requires 1 expected value, got %d", len(expected))
|
| }
|
| - if KeysEqual(actual.(Key), expected[0].(Key)) {
|
| + if Equal(actual.(ds.Key), expected[0].(ds.Key)) {
|
| return ""
|
| }
|
| return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0])
|
| @@ -44,7 +46,7 @@ func ShouldEqualKey(actual interface{}, expected ...interface{}) string {
|
| func TestKeyEncode(t *testing.T) {
|
| t.Parallel()
|
|
|
| - keys := []Key{
|
| + keys := []ds.Key{
|
| mkKey("appid", "ns", "kind", 1),
|
| mkKey("appid", "ns", "nerd", "moo"),
|
| mkKey("appid", "ns", "parent", 10, "renerd", "moo"),
|
| @@ -54,97 +56,100 @@ func TestKeyEncode(t *testing.T) {
|
| for _, k := range keys {
|
| k := k
|
| Convey(k.String(), func() {
|
| - enc := KeyEncode(k)
|
| - aid, ns, toks, err := KeyToksDecode(enc)
|
| + enc := Encode(k)
|
| + aid, ns, toks, err := ToksDecode(enc)
|
| So(err, ShouldBeNil)
|
| - dec := NewKeyToks(aid, ns, toks)
|
| + dec := NewToks(aid, ns, toks)
|
| So(dec, ShouldNotBeNil)
|
| So(dec, ShouldEqualKey, k)
|
|
|
| - dec2, err := NewKeyFromEncoded(enc)
|
| + dec2, err := NewFromEncoded(enc)
|
| So(err, ShouldBeNil)
|
| So(dec2, ShouldEqualKey, dec)
|
| So(dec2, ShouldEqualKey, k)
|
| })
|
|
|
| Convey(k.String()+" (json)", func() {
|
| - data, err := KeyMarshalJSON(k)
|
| + data, err := MarshalJSON(k)
|
| So(err, ShouldBeNil)
|
|
|
| - aid, ns, toks, err := KeyUnmarshalJSON(data)
|
| + aid, ns, toks, err := UnmarshalJSON(data)
|
| So(err, ShouldBeNil)
|
| - So(NewKeyToks(aid, ns, toks), ShouldEqualKey, k)
|
| + So(NewToks(aid, ns, toks), ShouldEqualKey, k)
|
| })
|
| }
|
| })
|
|
|
| Convey("NewKey", t, func() {
|
| Convey("single", func() {
|
| - k := NewKey("appid", "ns", "kind", "", 1, nil)
|
| + k := New("appid", "ns", "kind", "", 1, nil)
|
| So(k, ShouldEqualKey, keys[0])
|
| })
|
|
|
| Convey("nest", func() {
|
| - k := NewKey("appid", "ns", "renerd", "moo", 0,
|
| - NewKey("appid", "ns", "parent", "", 10, nil))
|
| + k := New("appid", "ns", "renerd", "moo", 0,
|
| + New("appid", "ns", "parent", "", 10, nil))
|
| So(k, ShouldEqualKey, keys[2])
|
| })
|
| })
|
|
|
| Convey("Key bad encoding", t, func() {
|
| Convey("extra junk before", func() {
|
| - enc := KeyEncode(keys[2])
|
| - _, _, _, err := KeyToksDecode("/" + enc)
|
| + enc := Encode(keys[2])
|
| + _, _, _, err := ToksDecode("/" + enc)
|
| So(err, ShouldErrLike, "illegal base64")
|
| })
|
|
|
| Convey("extra junk after", func() {
|
| - enc := KeyEncode(keys[2])
|
| - _, _, _, err := KeyToksDecode(enc[:len(enc)-1])
|
| + enc := Encode(keys[2])
|
| + _, _, _, err := ToksDecode(enc[:len(enc)-1])
|
| So(err, ShouldErrLike, "EOF")
|
| })
|
|
|
| Convey("json encoding includes quotes", func() {
|
| - data, err := KeyMarshalJSON(keys[0])
|
| + data, err := MarshalJSON(keys[0])
|
| So(err, ShouldBeNil)
|
|
|
| - _, _, _, err = KeyUnmarshalJSON(append(data, '!'))
|
| + _, _, _, err = UnmarshalJSON(append(data, '!'))
|
| So(err, ShouldErrLike, "bad JSON key")
|
| })
|
| })
|
| }
|
|
|
| -type dumbKey1 struct{ Key }
|
| +type dumbKey1 struct{ ds.Key }
|
|
|
| func (dk dumbKey1) Namespace() string { return "ns" }
|
| -func (dk dumbKey1) Parent() Key { return dk.Key }
|
| +func (dk dumbKey1) Parent() ds.Key { return dk.Key }
|
| func (dk dumbKey1) String() string { return "dumbKey1" }
|
|
|
| -type dumbKey2 struct{ Key }
|
| +type dumbKey2 struct{ ds.Key }
|
|
|
| /// This is the dumb part... can't have both IDs set.
|
| func (dk dumbKey2) IntID() int64 { return 1 }
|
| func (dk dumbKey2) StringID() string { return "wat" }
|
|
|
| func (dk dumbKey2) Kind() string { return "kind" }
|
| -func (dk dumbKey2) Parent() Key { return nil }
|
| +func (dk dumbKey2) Parent() ds.Key { return nil }
|
| func (dk dumbKey2) Namespace() string { return "ns" }
|
| func (dk dumbKey2) AppID() string { return "aid" }
|
| func (dk dumbKey2) String() string { return "dumbKey2" }
|
| +func (dk dumbKey2) Valid(allowSpecial bool, aid, ns string) bool {
|
| + return Valid(dk, allowSpecial, aid, ns)
|
| +}
|
|
|
| -func TestBadKeyEncode(t *testing.T) {
|
| - t.Parallel()
|
| +func TestKeyValidity(t *testing.T) {
|
| + //t.Parallel()
|
|
|
| - Convey("bad keys", t, func() {
|
| + Convey("keys validity", t, func() {
|
| Convey("incomplete", func() {
|
| - So(KeyIncomplete(mkKey("aid", "ns", "kind", 1)), ShouldBeFalse)
|
| - So(KeyIncomplete(mkKey("aid", "ns", "kind", 0)), ShouldBeTrue)
|
| + So(Incomplete(mkKey("aid", "ns", "kind", 1)), ShouldBeFalse)
|
| + So(Incomplete(mkKey("aid", "ns", "kind", 0)), ShouldBeTrue)
|
| })
|
|
|
| Convey("invalid", func() {
|
| - So(KeyValid(mkKey("aid", "ns", "hat", "face", "__kind__", 1), true, "aid", "ns"), ShouldBeTrue)
|
| + So(mkKey("aid", "ns", "hat", "face", "__kind__", 1).Valid(true, "aid", "ns"), ShouldBeTrue)
|
|
|
| - bads := []Key{
|
| + bads := []ds.Key{
|
| nil,
|
| mkKey("", "ns", "hat", "face"),
|
| mkKey("aid", "ns", "base", 1, "", "id"),
|
| @@ -159,16 +164,25 @@ func TestBadKeyEncode(t *testing.T) {
|
| s = k.String()
|
| }
|
| Convey(s, func() {
|
| - So(KeyValid(k, false, "aid", "ns"), ShouldBeFalse)
|
| + if k != nil {
|
| + So(k.Valid(false, "aid", "ns"), ShouldBeFalse)
|
| + } else {
|
| + So(Valid(k, false, "aid", "ns"), ShouldBeFalse)
|
| + }
|
| })
|
| }
|
| })
|
| +
|
| + Convey("partially valid", func() {
|
| + So(mkKey("aid", "ns", "kind", "").PartialValid("aid", "ns"), ShouldBeTrue)
|
| + So(mkKey("aid", "ns", "kind", "", "child", "").PartialValid("aid", "ns"), ShouldBeFalse)
|
| + })
|
| })
|
| }
|
|
|
| -type keyWrap struct{ Key }
|
| +type keyWrap struct{ ds.Key }
|
|
|
| -func (k keyWrap) Parent() Key {
|
| +func (k keyWrap) Parent() ds.Key {
|
| if k.Key.Parent() != nil {
|
| return keyWrap{k.Key.Parent()}
|
| }
|
| @@ -181,57 +195,57 @@ func TestMiscKey(t *testing.T) {
|
| Convey("KeyRoot", t, func() {
|
| k := mkKey("appid", "ns", "parent", 10, "renerd", "moo")
|
| r := mkKey("appid", "ns", "parent", 10)
|
| - So(KeyRoot(k), ShouldEqualKey, r)
|
| - So(KeyRoot(nil), ShouldBeNil)
|
| + So(Root(k), ShouldEqualKey, r)
|
| + So(Root(nil), ShouldBeNil)
|
| })
|
|
|
| Convey("KeySplit", t, func() {
|
| // keyWrap forces KeySplit to not take the GenericKey shortcut.
|
| k := keyWrap{mkKey("appid", "ns", "parent", 10, "renerd", "moo")}
|
| - aid, ns, toks := KeySplit(k)
|
| + aid, ns, toks := Split(k)
|
| So(aid, ShouldEqual, "appid")
|
| So(ns, ShouldEqual, "ns")
|
| - So(toks, ShouldResemble, []KeyTok{
|
| + So(toks, ShouldResemble, []ds.KeyTok{
|
| {Kind: "parent", IntID: 10},
|
| {Kind: "renerd", StringID: "moo"},
|
| })
|
| })
|
|
|
| Convey("KeySplit (nil)", t, func() {
|
| - aid, ns, toks := KeySplit(nil)
|
| + aid, ns, toks := Split(nil)
|
| So(aid, ShouldEqual, "")
|
| So(ns, ShouldEqual, "")
|
| - So(toks, ShouldResemble, []KeyTok(nil))
|
| + So(toks, ShouldResemble, []ds.KeyTok(nil))
|
| })
|
|
|
| Convey("KeySplit ((*GenericKey)(nil))", t, func() {
|
| - aid, ns, toks := KeySplit((*GenericKey)(nil))
|
| + aid, ns, toks := Split((*Generic)(nil))
|
| So(aid, ShouldEqual, "")
|
| So(ns, ShouldEqual, "")
|
| - So(toks, ShouldResemble, []KeyTok(nil))
|
| + So(toks, ShouldResemble, []ds.KeyTok(nil))
|
| })
|
|
|
| Convey("KeysEqual", t, func() {
|
| k1 := mkKey("a", "n", "knd", 1)
|
| k2 := mkKey("a", "n", "knd", 1)
|
| - So(KeysEqual(k1, k2), ShouldBeTrue)
|
| + So(Equal(k1, k2), ShouldBeTrue)
|
| k3 := mkKey("a", "n", "knd", 2)
|
| - So(KeysEqual(k1, k3), ShouldBeFalse)
|
| + So(Equal(k1, k3), ShouldBeFalse)
|
| })
|
|
|
| Convey("KeyString", t, func() {
|
| k1 := mkKey("a", "n", "knd", 1, "other", "wat")
|
| - So(KeyString(k1), ShouldEqual, "/knd,1/other,wat")
|
| - So(KeyString(nil), ShouldEqual, "")
|
| + So(String(k1), ShouldEqual, "/knd,1/other,wat")
|
| + So(String(nil), ShouldEqual, "")
|
| })
|
|
|
| Convey("*GenericKey supports json encoding", t, func() {
|
| type TestStruct struct {
|
| - Key *GenericKey
|
| + Key *Generic
|
| }
|
| t := &TestStruct{
|
| - NewKey("aid", "ns", "kind", "id", 0,
|
| - NewKey("aid", "ns", "parent", "", 1, nil),
|
| + New("aid", "ns", "kind", "id", 0,
|
| + New("aid", "ns", "parent", "", 1, nil),
|
| )}
|
| d, err := json.Marshal(t)
|
| So(err, ShouldBeNil)
|
|
|