OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package helper |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 |
| 11 "infra/gae/libs/gae" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 func mkKey(aid, ns string, elems ...interface{}) gae.DSKey { |
| 17 if len(elems)%2 != 0 { |
| 18 panic("odd number of tokens") |
| 19 } |
| 20 toks := make([]gae.DSKeyTok, len(elems)/2) |
| 21 for i := 0; i < len(elems); i += 2 { |
| 22 toks[i/2].Kind = elems[i].(string) |
| 23 switch x := elems[i+1].(type) { |
| 24 case string: |
| 25 toks[i/2].StringID = x |
| 26 case int: |
| 27 toks[i/2].IntID = int64(x) |
| 28 default: |
| 29 panic("bad token id") |
| 30 } |
| 31 } |
| 32 return NewDSKeyToks(aid, ns, toks) |
| 33 } |
| 34 |
| 35 func ShouldEqualKey(actual interface{}, expected ...interface{}) string { |
| 36 if len(expected) != 1 { |
| 37 return fmt.Sprintf("Assertion requires 1 expected value, got %d"
, len(expected)) |
| 38 } |
| 39 if DSKeysEqual(actual.(gae.DSKey), expected[0].(gae.DSKey)) { |
| 40 return "" |
| 41 } |
| 42 return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0]) |
| 43 } |
| 44 |
| 45 func TestKeyEncode(t *testing.T) { |
| 46 t.Parallel() |
| 47 |
| 48 keys := []gae.DSKey{ |
| 49 mkKey("appid", "ns", "kind", 1), |
| 50 mkKey("appid", "ns", "nerd", "moo"), |
| 51 mkKey("appid", "ns", "parent", 10, "renerd", "moo"), |
| 52 } |
| 53 |
| 54 Convey("DSKey Round trip", t, func() { |
| 55 for _, k := range keys { |
| 56 k := k |
| 57 Convey(k.String(), func() { |
| 58 enc := DSKeyEncode(k) |
| 59 aid, ns, toks, err := DSKeyToksDecode(enc) |
| 60 So(err, ShouldBeNil) |
| 61 dec := NewDSKeyToks(aid, ns, toks) |
| 62 So(dec, ShouldNotBeNil) |
| 63 So(dec, ShouldEqualKey, k) |
| 64 }) |
| 65 |
| 66 Convey(k.String()+" (json)", func() { |
| 67 data, err := DSKeyMarshalJSON(k) |
| 68 So(err, ShouldBeNil) |
| 69 |
| 70 aid, ns, toks, err := DSKeyUnmarshalJSON(data) |
| 71 So(err, ShouldBeNil) |
| 72 So(NewDSKeyToks(aid, ns, toks), ShouldEqualKey,
k) |
| 73 }) |
| 74 } |
| 75 }) |
| 76 |
| 77 Convey("NewDSKey", t, func() { |
| 78 Convey("single", func() { |
| 79 k := NewDSKey("appid", "ns", "kind", "", 1, nil) |
| 80 So(k, ShouldEqualKey, keys[0]) |
| 81 }) |
| 82 |
| 83 Convey("nest", func() { |
| 84 k := NewDSKey("appid", "ns", "renerd", "moo", 0, |
| 85 NewDSKey("appid", "ns", "parent", "", 10, nil)) |
| 86 So(k, ShouldEqualKey, keys[2]) |
| 87 }) |
| 88 }) |
| 89 |
| 90 Convey("DSKey bad encoding", t, func() { |
| 91 Convey("extra junk before", func() { |
| 92 enc := DSKeyEncode(keys[2]) |
| 93 _, _, _, err := DSKeyToksDecode("/" + enc) |
| 94 So(err, ShouldErrLike, "illegal base64") |
| 95 }) |
| 96 |
| 97 Convey("extra junk after", func() { |
| 98 enc := DSKeyEncode(keys[2]) |
| 99 _, _, _, err := DSKeyToksDecode(enc[:len(enc)-1]) |
| 100 So(err, ShouldErrLike, "EOF") |
| 101 }) |
| 102 |
| 103 Convey("json encoding includes quotes", func() { |
| 104 data, err := DSKeyMarshalJSON(keys[0]) |
| 105 So(err, ShouldBeNil) |
| 106 |
| 107 _, _, _, err = DSKeyUnmarshalJSON(append(data, '!')) |
| 108 So(err, ShouldErrLike, "bad JSON key") |
| 109 }) |
| 110 }) |
| 111 } |
| 112 |
| 113 type dumbKey1 struct{ gae.DSKey } |
| 114 |
| 115 func (dk dumbKey1) Namespace() string { return "ns" } |
| 116 func (dk dumbKey1) Parent() gae.DSKey { return dk.DSKey } |
| 117 func (dk dumbKey1) String() string { return "dumbKey1" } |
| 118 |
| 119 type dumbKey2 struct{ gae.DSKey } |
| 120 |
| 121 /// This is the dumb part... can't have both IDs set. |
| 122 func (dk dumbKey2) IntID() int64 { return 1 } |
| 123 func (dk dumbKey2) StringID() string { return "wat" } |
| 124 |
| 125 func (dk dumbKey2) Kind() string { return "kind" } |
| 126 func (dk dumbKey2) Parent() gae.DSKey { return nil } |
| 127 func (dk dumbKey2) Namespace() string { return "ns" } |
| 128 func (dk dumbKey2) AppID() string { return "aid" } |
| 129 func (dk dumbKey2) String() string { return "dumbKey2" } |
| 130 |
| 131 func TestBadKeyEncode(t *testing.T) { |
| 132 t.Parallel() |
| 133 |
| 134 Convey("bad keys", t, func() { |
| 135 Convey("incomplete", func() { |
| 136 So(DSKeyIncomplete(mkKey("aid", "ns", "kind", 1)), Shoul
dBeFalse) |
| 137 So(DSKeyIncomplete(mkKey("aid", "ns", "kind", 0)), Shoul
dBeTrue) |
| 138 }) |
| 139 |
| 140 Convey("invalid", func() { |
| 141 So(DSKeyValid(mkKey("aid", "ns", "hat", "face", "__kind_
_", 1), "ns", true), ShouldBeTrue) |
| 142 So(DSKeyValid(mkKey("aid", "ns", "hat", "face", "kind",
1), "wat", false), ShouldBeFalse) |
| 143 |
| 144 bads := []gae.DSKey{ |
| 145 nil, |
| 146 mkKey("", "ns", "hat", "face"), |
| 147 mkKey("aid", "ns", "base", 1, "", "id"), |
| 148 mkKey("aid", "ns", "hat", "face", "__kind__", 1)
, |
| 149 mkKey("aid", "ns", "hat", 0, "kind", 1), |
| 150 dumbKey1{mkKey("aid", "badNS", "hat", 1)}, |
| 151 dumbKey2{}, |
| 152 } |
| 153 for _, k := range bads { |
| 154 s := "<nil>" |
| 155 if k != nil { |
| 156 s = k.String() |
| 157 } |
| 158 Convey(s, func() { |
| 159 So(DSKeyValid(k, "ns", false), ShouldBeF
alse) |
| 160 }) |
| 161 } |
| 162 }) |
| 163 }) |
| 164 } |
| 165 |
| 166 type keyWrap struct{ gae.DSKey } |
| 167 |
| 168 func (k keyWrap) Parent() gae.DSKey { |
| 169 if k.DSKey.Parent() != nil { |
| 170 return keyWrap{k.DSKey.Parent()} |
| 171 } |
| 172 return nil |
| 173 } |
| 174 |
| 175 func TestMiscKey(t *testing.T) { |
| 176 t.Parallel() |
| 177 |
| 178 Convey("DSKeyRoot", t, func() { |
| 179 k := mkKey("appid", "ns", "parent", 10, "renerd", "moo") |
| 180 r := mkKey("appid", "ns", "parent", 10) |
| 181 So(DSKeyRoot(k), ShouldEqualKey, r) |
| 182 So(DSKeyRoot(nil), ShouldBeNil) |
| 183 }) |
| 184 |
| 185 Convey("DSKeySplit", t, func() { |
| 186 // keyWrap forces DSKeySplit to not take the shellKey shortcut. |
| 187 k := keyWrap{mkKey("appid", "ns", "parent", 10, "renerd", "moo")
} |
| 188 aid, ns, toks := DSKeySplit(k) |
| 189 So(aid, ShouldEqual, "appid") |
| 190 So(ns, ShouldEqual, "ns") |
| 191 So(toks, ShouldResemble, []gae.DSKeyTok{ |
| 192 {Kind: "parent", IntID: 10}, |
| 193 {Kind: "renerd", StringID: "moo"}, |
| 194 }) |
| 195 }) |
| 196 |
| 197 Convey("DSKeySplit (nil)", t, func() { |
| 198 aid, ns, toks := DSKeySplit(nil) |
| 199 So(aid, ShouldEqual, "") |
| 200 So(ns, ShouldEqual, "") |
| 201 So(toks, ShouldResemble, []gae.DSKeyTok(nil)) |
| 202 }) |
| 203 |
| 204 Convey("DSKeySplit ((*shellDSKey)(nil))", t, func() { |
| 205 aid, ns, toks := DSKeySplit((*shellDSKey)(nil)) |
| 206 So(aid, ShouldEqual, "") |
| 207 So(ns, ShouldEqual, "") |
| 208 So(toks, ShouldResemble, []gae.DSKeyTok(nil)) |
| 209 }) |
| 210 |
| 211 Convey("DSKeysEqual", t, func() { |
| 212 k1 := mkKey("a", "n", "knd", 1) |
| 213 k2 := mkKey("a", "n", "knd", 1) |
| 214 So(DSKeysEqual(k1, k2), ShouldBeTrue) |
| 215 k3 := mkKey("a", "n", "knd", 2) |
| 216 So(DSKeysEqual(k1, k3), ShouldBeFalse) |
| 217 }) |
| 218 |
| 219 Convey("DSKeyString", t, func() { |
| 220 k1 := mkKey("a", "n", "knd", 1, "other", "wat") |
| 221 So(DSKeyString(k1), ShouldEqual, "/knd,1/other,wat") |
| 222 So(DSKeyString(nil), ShouldEqual, "") |
| 223 }) |
| 224 } |
OLD | NEW |