key_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package datastore
  15. import (
  16. "bytes"
  17. "encoding/gob"
  18. "encoding/json"
  19. "testing"
  20. "golang.org/x/net/context"
  21. )
  22. func TestNamespace(t *testing.T) {
  23. c := context.Background()
  24. k := NewIncompleteKey(c, "foo", nil)
  25. if got, want := k.Namespace(), ""; got != want {
  26. t.Errorf("No namespace, k.Namespace() = %q, want %q", got, want)
  27. }
  28. c = WithNamespace(c, "gopherspace")
  29. k = NewIncompleteKey(c, "foo", nil)
  30. if got, want := k.Namespace(), "gopherspace"; got != want {
  31. t.Errorf("No namespace, k.Namespace() = %q, want %q", got, want)
  32. }
  33. }
  34. func TestParent(t *testing.T) {
  35. c := context.Background()
  36. k := NewIncompleteKey(c, "foo", nil)
  37. par := NewKey(c, "foomum", "", 1248, nil)
  38. k.SetParent(par)
  39. if got := k.Parent(); got != par {
  40. t.Errorf("k.Parent() = %v; want %v", got, par)
  41. }
  42. }
  43. func TestEqual(t *testing.T) {
  44. c := context.Background()
  45. cN := WithNamespace(c, "gopherspace")
  46. testCases := []struct {
  47. x, y *Key
  48. equal bool
  49. }{
  50. {
  51. x: nil,
  52. y: nil,
  53. equal: true,
  54. },
  55. {
  56. x: NewKey(c, "kindA", "", 0, nil),
  57. y: NewIncompleteKey(c, "kindA", nil),
  58. equal: true,
  59. },
  60. {
  61. x: NewKey(c, "kindA", "nameA", 0, nil),
  62. y: NewKey(c, "kindA", "nameA", 0, nil),
  63. equal: true,
  64. },
  65. {
  66. x: NewKey(cN, "kindA", "nameA", 0, nil),
  67. y: NewKey(cN, "kindA", "nameA", 0, nil),
  68. equal: true,
  69. },
  70. {
  71. x: NewKey(c, "kindA", "", 1337, NewKey(c, "kindX", "nameX", 0, nil)),
  72. y: NewKey(c, "kindA", "", 1337, NewKey(c, "kindX", "nameX", 0, nil)),
  73. equal: true,
  74. },
  75. {
  76. x: NewKey(c, "kindA", "nameA", 0, nil),
  77. y: NewKey(c, "kindB", "nameA", 0, nil),
  78. equal: false,
  79. },
  80. {
  81. x: NewKey(c, "kindA", "nameA", 0, nil),
  82. y: NewKey(c, "kindA", "nameB", 0, nil),
  83. equal: false,
  84. },
  85. {
  86. x: NewKey(c, "kindA", "nameA", 0, nil),
  87. y: NewKey(c, "kindA", "", 1337, nil),
  88. equal: false,
  89. },
  90. {
  91. x: NewKey(c, "kindA", "nameA", 0, nil),
  92. y: NewKey(cN, "kindA", "nameA", 0, nil),
  93. equal: false,
  94. },
  95. {
  96. x: NewKey(c, "kindA", "", 1337, NewKey(c, "kindX", "nameX", 0, nil)),
  97. y: NewKey(c, "kindA", "", 1337, NewKey(c, "kindY", "nameX", 0, nil)),
  98. equal: false,
  99. },
  100. {
  101. x: NewKey(c, "kindA", "", 1337, NewKey(c, "kindX", "nameX", 0, nil)),
  102. y: NewKey(c, "kindA", "", 1337, nil),
  103. equal: false,
  104. },
  105. }
  106. for _, tt := range testCases {
  107. if got := tt.x.Equal(tt.y); got != tt.equal {
  108. t.Errorf("Equal(%v, %v) = %t; want %t", tt.x, tt.y, got, tt.equal)
  109. }
  110. if got := tt.y.Equal(tt.x); got != tt.equal {
  111. t.Errorf("Equal(%v, %v) = %t; want %t", tt.y, tt.x, got, tt.equal)
  112. }
  113. }
  114. }
  115. func TestEncoding(t *testing.T) {
  116. c := context.Background()
  117. cN := WithNamespace(c, "gopherspace")
  118. testCases := []struct {
  119. k *Key
  120. valid bool
  121. }{
  122. {
  123. k: nil,
  124. valid: false,
  125. },
  126. {
  127. k: NewKey(c, "", "", 0, nil),
  128. valid: false,
  129. },
  130. {
  131. k: NewKey(c, "kindA", "", 0, nil),
  132. valid: true,
  133. },
  134. {
  135. k: NewKey(cN, "kindA", "", 0, nil),
  136. valid: true,
  137. },
  138. {
  139. k: NewKey(c, "kindA", "nameA", 0, nil),
  140. valid: true,
  141. },
  142. {
  143. k: NewKey(c, "kindA", "", 1337, nil),
  144. valid: true,
  145. },
  146. {
  147. k: NewKey(c, "kindA", "nameA", 1337, nil),
  148. valid: false,
  149. },
  150. {
  151. k: NewKey(c, "kindA", "", 0, NewKey(c, "kindB", "nameB", 0, nil)),
  152. valid: true,
  153. },
  154. {
  155. k: NewKey(c, "kindA", "", 0, NewKey(c, "kindB", "", 0, nil)),
  156. valid: false,
  157. },
  158. {
  159. k: NewKey(c, "kindA", "", 0, NewKey(cN, "kindB", "nameB", 0, nil)),
  160. valid: false,
  161. },
  162. }
  163. for _, tt := range testCases {
  164. if got := tt.k.valid(); got != tt.valid {
  165. t.Errorf("valid(%v) = %t; want %t", tt.k, got, tt.valid)
  166. }
  167. // Check encoding/decoding for valid keys.
  168. if !tt.valid {
  169. continue
  170. }
  171. enc := tt.k.Encode()
  172. dec, err := DecodeKey(enc)
  173. if err != nil {
  174. t.Errorf("DecodeKey(%q) from %v: %v", enc, tt.k, err)
  175. continue
  176. }
  177. if !tt.k.Equal(dec) {
  178. t.Errorf("Decoded key %v not equal to %v", dec, tt.k)
  179. }
  180. b, err := json.Marshal(tt.k)
  181. if err != nil {
  182. t.Errorf("json.Marshal(%v): %v", tt.k, err)
  183. continue
  184. }
  185. key := &Key{}
  186. if err := json.Unmarshal(b, key); err != nil {
  187. t.Errorf("json.Unmarshal(%s) for key %v: %v", b, tt.k, err)
  188. continue
  189. }
  190. if !tt.k.Equal(key) {
  191. t.Errorf("JSON decoded key %v not equal to %v", dec, tt.k)
  192. }
  193. buf := &bytes.Buffer{}
  194. gobEnc := gob.NewEncoder(buf)
  195. if err := gobEnc.Encode(tt.k); err != nil {
  196. t.Errorf("gobEnc.Encode(%v): %v", tt.k, err)
  197. continue
  198. }
  199. gobDec := gob.NewDecoder(buf)
  200. key = &Key{}
  201. if err := gobDec.Decode(key); err != nil {
  202. t.Errorf("gobDec.Decode() for key %v: %v", tt.k, err)
  203. }
  204. if !tt.k.Equal(key) {
  205. t.Errorf("gob decoded key %v not equal to %v", dec, tt.k)
  206. }
  207. }
  208. }
  209. func TestInvalidKeyDecode(t *testing.T) {
  210. // Check that decoding an invalid key returns an err and doesn't panic.
  211. enc := NewKey(context.Background(), "Kind", "Foo", 0, nil).Encode()
  212. invalid := []string{
  213. "",
  214. "Laboratorio",
  215. enc + "Junk",
  216. enc[:len(enc)-4],
  217. }
  218. for _, enc := range invalid {
  219. key, err := DecodeKey(enc)
  220. if err == nil || key != nil {
  221. t.Errorf("DecodeKey(%q) = %v, %v; want nil, error", key, err)
  222. }
  223. }
  224. }