You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

373 lines
9.7 KiB

  1. /*
  2. Copyright 2017 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spanner
  14. import (
  15. "testing"
  16. "time"
  17. "cloud.google.com/go/civil"
  18. proto3 "github.com/golang/protobuf/ptypes/struct"
  19. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  20. )
  21. // Test Key.String() and Key.proto().
  22. func TestKey(t *testing.T) {
  23. tm, _ := time.Parse(time.RFC3339Nano, "2016-11-15T15:04:05.999999999Z")
  24. dt, _ := civil.ParseDate("2016-11-15")
  25. for _, test := range []struct {
  26. k Key
  27. wantProto *proto3.ListValue
  28. wantStr string
  29. }{
  30. {
  31. k: Key{int(1)},
  32. wantProto: listValueProto(stringProto("1")),
  33. wantStr: "(1)",
  34. },
  35. {
  36. k: Key{int8(1)},
  37. wantProto: listValueProto(stringProto("1")),
  38. wantStr: "(1)",
  39. },
  40. {
  41. k: Key{int16(1)},
  42. wantProto: listValueProto(stringProto("1")),
  43. wantStr: "(1)",
  44. },
  45. {
  46. k: Key{int32(1)},
  47. wantProto: listValueProto(stringProto("1")),
  48. wantStr: "(1)",
  49. },
  50. {
  51. k: Key{int64(1)},
  52. wantProto: listValueProto(stringProto("1")),
  53. wantStr: "(1)",
  54. },
  55. {
  56. k: Key{uint8(1)},
  57. wantProto: listValueProto(stringProto("1")),
  58. wantStr: "(1)",
  59. },
  60. {
  61. k: Key{uint16(1)},
  62. wantProto: listValueProto(stringProto("1")),
  63. wantStr: "(1)",
  64. },
  65. {
  66. k: Key{uint32(1)},
  67. wantProto: listValueProto(stringProto("1")),
  68. wantStr: "(1)",
  69. },
  70. {
  71. k: Key{true},
  72. wantProto: listValueProto(boolProto(true)),
  73. wantStr: "(true)",
  74. },
  75. {
  76. k: Key{float32(1.5)},
  77. wantProto: listValueProto(floatProto(1.5)),
  78. wantStr: "(1.5)",
  79. },
  80. {
  81. k: Key{float64(1.5)},
  82. wantProto: listValueProto(floatProto(1.5)),
  83. wantStr: "(1.5)",
  84. },
  85. {
  86. k: Key{"value"},
  87. wantProto: listValueProto(stringProto("value")),
  88. wantStr: `("value")`,
  89. },
  90. {
  91. k: Key{[]byte(nil)},
  92. wantProto: listValueProto(nullProto()),
  93. wantStr: "(<null>)",
  94. },
  95. {
  96. k: Key{[]byte{}},
  97. wantProto: listValueProto(stringProto("")),
  98. wantStr: `("")`,
  99. },
  100. {
  101. k: Key{tm},
  102. wantProto: listValueProto(stringProto("2016-11-15T15:04:05.999999999Z")),
  103. wantStr: `("2016-11-15T15:04:05.999999999Z")`,
  104. },
  105. {k: Key{dt},
  106. wantProto: listValueProto(stringProto("2016-11-15")),
  107. wantStr: `("2016-11-15")`,
  108. },
  109. {
  110. k: Key{[]byte("value")},
  111. wantProto: listValueProto(bytesProto([]byte("value"))),
  112. wantStr: `("value")`,
  113. },
  114. {
  115. k: Key{NullInt64{1, true}},
  116. wantProto: listValueProto(stringProto("1")),
  117. wantStr: "(1)",
  118. },
  119. {
  120. k: Key{NullInt64{2, false}},
  121. wantProto: listValueProto(nullProto()),
  122. wantStr: "(<null>)",
  123. },
  124. {
  125. k: Key{NullFloat64{1.5, true}},
  126. wantProto: listValueProto(floatProto(1.5)),
  127. wantStr: "(1.5)",
  128. },
  129. {
  130. k: Key{NullFloat64{2.0, false}},
  131. wantProto: listValueProto(nullProto()),
  132. wantStr: "(<null>)",
  133. },
  134. {
  135. k: Key{NullBool{true, true}},
  136. wantProto: listValueProto(boolProto(true)),
  137. wantStr: "(true)",
  138. },
  139. {
  140. k: Key{NullBool{true, false}},
  141. wantProto: listValueProto(nullProto()),
  142. wantStr: "(<null>)",
  143. },
  144. {
  145. k: Key{NullString{"value", true}},
  146. wantProto: listValueProto(stringProto("value")),
  147. wantStr: `("value")`,
  148. },
  149. {
  150. k: Key{NullString{"value", false}},
  151. wantProto: listValueProto(nullProto()),
  152. wantStr: "(<null>)",
  153. },
  154. {
  155. k: Key{NullTime{tm, true}},
  156. wantProto: listValueProto(timeProto(tm)),
  157. wantStr: `("2016-11-15T15:04:05.999999999Z")`,
  158. },
  159. {
  160. k: Key{NullTime{time.Now(), false}},
  161. wantProto: listValueProto(nullProto()),
  162. wantStr: "(<null>)",
  163. },
  164. {
  165. k: Key{NullDate{dt, true}},
  166. wantProto: listValueProto(dateProto(dt)),
  167. wantStr: `("2016-11-15")`,
  168. },
  169. {
  170. k: Key{NullDate{civil.Date{}, false}},
  171. wantProto: listValueProto(nullProto()),
  172. wantStr: "(<null>)",
  173. },
  174. {
  175. k: Key{int(1), NullString{"value", false}, "value", 1.5, true},
  176. wantProto: listValueProto(stringProto("1"), nullProto(), stringProto("value"), floatProto(1.5), boolProto(true)),
  177. wantStr: `(1,<null>,"value",1.5,true)`,
  178. },
  179. } {
  180. if got := test.k.String(); got != test.wantStr {
  181. t.Errorf("%v.String() = %v, want %v", test.k, got, test.wantStr)
  182. }
  183. gotProto, err := test.k.proto()
  184. if err != nil {
  185. t.Errorf("%v.proto() returns error %v; want nil error", test.k, err)
  186. }
  187. if !testEqual(gotProto, test.wantProto) {
  188. t.Errorf("%v.proto() = \n%v\nwant:\n%v", test.k, gotProto, test.wantProto)
  189. }
  190. }
  191. }
  192. // Test KeyRange.String() and KeyRange.proto().
  193. func TestKeyRange(t *testing.T) {
  194. for _, test := range []struct {
  195. kr KeyRange
  196. wantProto *sppb.KeyRange
  197. wantStr string
  198. }{
  199. {
  200. kr: KeyRange{Key{"A"}, Key{"D"}, OpenOpen},
  201. wantProto: &sppb.KeyRange{
  202. StartKeyType: &sppb.KeyRange_StartOpen{StartOpen: listValueProto(stringProto("A"))},
  203. EndKeyType: &sppb.KeyRange_EndOpen{EndOpen: listValueProto(stringProto("D"))},
  204. },
  205. wantStr: `(("A"),("D"))`,
  206. },
  207. {
  208. kr: KeyRange{Key{1}, Key{10}, OpenClosed},
  209. wantProto: &sppb.KeyRange{
  210. StartKeyType: &sppb.KeyRange_StartOpen{StartOpen: listValueProto(stringProto("1"))},
  211. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(stringProto("10"))},
  212. },
  213. wantStr: "((1),(10)]",
  214. },
  215. {
  216. kr: KeyRange{Key{1.5, 2.1, 0.2}, Key{1.9, 0.7}, ClosedOpen},
  217. wantProto: &sppb.KeyRange{
  218. StartKeyType: &sppb.KeyRange_StartClosed{StartClosed: listValueProto(floatProto(1.5), floatProto(2.1), floatProto(0.2))},
  219. EndKeyType: &sppb.KeyRange_EndOpen{EndOpen: listValueProto(floatProto(1.9), floatProto(0.7))},
  220. },
  221. wantStr: "[(1.5,2.1,0.2),(1.9,0.7))",
  222. },
  223. {
  224. kr: KeyRange{Key{NullInt64{1, true}}, Key{10}, ClosedClosed},
  225. wantProto: &sppb.KeyRange{
  226. StartKeyType: &sppb.KeyRange_StartClosed{StartClosed: listValueProto(stringProto("1"))},
  227. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(stringProto("10"))},
  228. },
  229. wantStr: "[(1),(10)]",
  230. },
  231. } {
  232. if got := test.kr.String(); got != test.wantStr {
  233. t.Errorf("%v.String() = %v, want %v", test.kr, got, test.wantStr)
  234. }
  235. gotProto, err := test.kr.proto()
  236. if err != nil {
  237. t.Errorf("%v.proto() returns error %v; want nil error", test.kr, err)
  238. }
  239. if !testEqual(gotProto, test.wantProto) {
  240. t.Errorf("%v.proto() = \n%v\nwant:\n%v", test.kr, gotProto.String(), test.wantProto.String())
  241. }
  242. }
  243. }
  244. func TestPrefixRange(t *testing.T) {
  245. got := Key{1}.AsPrefix()
  246. want := KeyRange{Start: Key{1}, End: Key{1}, Kind: ClosedClosed}
  247. if !testEqual(got, want) {
  248. t.Errorf("got %v, want %v", got, want)
  249. }
  250. }
  251. func TestKeySets(t *testing.T) {
  252. int1 := intProto(1)
  253. int2 := intProto(2)
  254. int3 := intProto(3)
  255. int4 := intProto(4)
  256. for i, test := range []struct {
  257. ks KeySet
  258. wantProto *sppb.KeySet
  259. }{
  260. {
  261. KeySets(),
  262. &sppb.KeySet{},
  263. },
  264. {
  265. Key{4},
  266. &sppb.KeySet{
  267. Keys: []*proto3.ListValue{listValueProto(int4)},
  268. },
  269. },
  270. {
  271. AllKeys(),
  272. &sppb.KeySet{All: true},
  273. },
  274. {
  275. KeySets(Key{1, 2}, Key{3, 4}),
  276. &sppb.KeySet{
  277. Keys: []*proto3.ListValue{
  278. listValueProto(int1, int2),
  279. listValueProto(int3, int4),
  280. },
  281. },
  282. },
  283. {
  284. KeyRange{Key{1}, Key{2}, ClosedOpen},
  285. &sppb.KeySet{Ranges: []*sppb.KeyRange{
  286. {
  287. StartKeyType: &sppb.KeyRange_StartClosed{StartClosed: listValueProto(int1)},
  288. EndKeyType: &sppb.KeyRange_EndOpen{EndOpen: listValueProto(int2)},
  289. },
  290. }},
  291. },
  292. {
  293. Key{2}.AsPrefix(),
  294. &sppb.KeySet{Ranges: []*sppb.KeyRange{
  295. {
  296. StartKeyType: &sppb.KeyRange_StartClosed{StartClosed: listValueProto(int2)},
  297. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(int2)},
  298. },
  299. }},
  300. },
  301. {
  302. KeySets(
  303. KeyRange{Key{1}, Key{2}, ClosedClosed},
  304. KeyRange{Key{3}, Key{4}, OpenClosed},
  305. ),
  306. &sppb.KeySet{
  307. Ranges: []*sppb.KeyRange{
  308. {
  309. StartKeyType: &sppb.KeyRange_StartClosed{StartClosed: listValueProto(int1)},
  310. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(int2)},
  311. },
  312. {
  313. StartKeyType: &sppb.KeyRange_StartOpen{StartOpen: listValueProto(int3)},
  314. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(int4)},
  315. },
  316. },
  317. },
  318. },
  319. {
  320. KeySets(
  321. Key{1},
  322. KeyRange{Key{2}, Key{3}, ClosedClosed},
  323. KeyRange{Key{4}, Key{5}, OpenClosed},
  324. KeySets(),
  325. Key{6}),
  326. &sppb.KeySet{
  327. Keys: []*proto3.ListValue{
  328. listValueProto(int1),
  329. listValueProto(intProto(6)),
  330. },
  331. Ranges: []*sppb.KeyRange{
  332. {
  333. StartKeyType: &sppb.KeyRange_StartClosed{StartClosed: listValueProto(int2)},
  334. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(int3)},
  335. },
  336. {
  337. StartKeyType: &sppb.KeyRange_StartOpen{StartOpen: listValueProto(int4)},
  338. EndKeyType: &sppb.KeyRange_EndClosed{EndClosed: listValueProto(intProto(5))},
  339. },
  340. },
  341. },
  342. },
  343. {
  344. KeySets(
  345. Key{1},
  346. KeyRange{Key{2}, Key{3}, ClosedClosed},
  347. AllKeys(),
  348. KeyRange{Key{4}, Key{5}, OpenClosed},
  349. Key{6}),
  350. &sppb.KeySet{All: true},
  351. },
  352. } {
  353. gotProto, err := test.ks.keySetProto()
  354. if err != nil {
  355. t.Errorf("#%d: %v.proto() returns error %v; want nil error", i, test.ks, err)
  356. }
  357. if !testEqual(gotProto, test.wantProto) {
  358. t.Errorf("#%d: %v.proto() = \n%v\nwant:\n%v", i, test.ks, gotProto.String(), test.wantProto.String())
  359. }
  360. }
  361. }