Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

1117 rindas
30 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. "math"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "cloud.google.com/go/civil"
  20. proto3 "github.com/golang/protobuf/ptypes/struct"
  21. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  22. )
  23. var (
  24. t1 = mustParseTime("2016-11-15T15:04:05.999999999Z")
  25. // Boundaries
  26. t2 = mustParseTime("0000-01-01T00:00:00.000000000Z")
  27. t3 = mustParseTime("9999-12-31T23:59:59.999999999Z")
  28. // Local timezone
  29. t4 = time.Now()
  30. d1 = mustParseDate("2016-11-15")
  31. d2 = mustParseDate("1678-01-01")
  32. )
  33. func mustParseTime(s string) time.Time {
  34. t, err := time.Parse(time.RFC3339Nano, s)
  35. if err != nil {
  36. panic(err)
  37. }
  38. return t
  39. }
  40. func mustParseDate(s string) civil.Date {
  41. d, err := civil.ParseDate(s)
  42. if err != nil {
  43. panic(err)
  44. }
  45. return d
  46. }
  47. // Test encoding Values.
  48. func TestEncodeValue(t *testing.T) {
  49. var (
  50. tString = stringType()
  51. tInt = intType()
  52. tBool = boolType()
  53. tFloat = floatType()
  54. tBytes = bytesType()
  55. tTime = timeType()
  56. tDate = dateType()
  57. )
  58. for i, test := range []struct {
  59. in interface{}
  60. want *proto3.Value
  61. wantType *sppb.Type
  62. }{
  63. // STRING / STRING ARRAY
  64. {"abc", stringProto("abc"), tString},
  65. {NullString{"abc", true}, stringProto("abc"), tString},
  66. {NullString{"abc", false}, nullProto(), tString},
  67. {[]string(nil), nullProto(), listType(tString)},
  68. {[]string{"abc", "bcd"}, listProto(stringProto("abc"), stringProto("bcd")), listType(tString)},
  69. {[]NullString{{"abcd", true}, {"xyz", false}}, listProto(stringProto("abcd"), nullProto()), listType(tString)},
  70. // BYTES / BYTES ARRAY
  71. {[]byte("foo"), bytesProto([]byte("foo")), tBytes},
  72. {[]byte(nil), nullProto(), tBytes},
  73. {[][]byte{nil, []byte("ab")}, listProto(nullProto(), bytesProto([]byte("ab"))), listType(tBytes)},
  74. {[][]byte(nil), nullProto(), listType(tBytes)},
  75. // INT64 / INT64 ARRAY
  76. {7, intProto(7), tInt},
  77. {[]int(nil), nullProto(), listType(tInt)},
  78. {[]int{31, 127}, listProto(intProto(31), intProto(127)), listType(tInt)},
  79. {int64(81), intProto(81), tInt},
  80. {[]int64(nil), nullProto(), listType(tInt)},
  81. {[]int64{33, 129}, listProto(intProto(33), intProto(129)), listType(tInt)},
  82. {NullInt64{11, true}, intProto(11), tInt},
  83. {NullInt64{11, false}, nullProto(), tInt},
  84. {[]NullInt64{{35, true}, {131, false}}, listProto(intProto(35), nullProto()), listType(tInt)},
  85. // BOOL / BOOL ARRAY
  86. {true, boolProto(true), tBool},
  87. {NullBool{true, true}, boolProto(true), tBool},
  88. {NullBool{true, false}, nullProto(), tBool},
  89. {[]bool{true, false}, listProto(boolProto(true), boolProto(false)), listType(tBool)},
  90. {[]NullBool{{true, true}, {true, false}}, listProto(boolProto(true), nullProto()), listType(tBool)},
  91. // FLOAT64 / FLOAT64 ARRAY
  92. {3.14, floatProto(3.14), tFloat},
  93. {NullFloat64{3.1415, true}, floatProto(3.1415), tFloat},
  94. {NullFloat64{math.Inf(1), true}, floatProto(math.Inf(1)), tFloat},
  95. {NullFloat64{3.14159, false}, nullProto(), tFloat},
  96. {[]float64(nil), nullProto(), listType(tFloat)},
  97. {[]float64{3.141, 0.618, math.Inf(-1)}, listProto(floatProto(3.141), floatProto(0.618), floatProto(math.Inf(-1))), listType(tFloat)},
  98. {[]NullFloat64{{3.141, true}, {0.618, false}}, listProto(floatProto(3.141), nullProto()), listType(tFloat)},
  99. // TIMESTAMP / TIMESTAMP ARRAY
  100. {t1, timeProto(t1), tTime},
  101. {NullTime{t1, true}, timeProto(t1), tTime},
  102. {NullTime{t1, false}, nullProto(), tTime},
  103. {[]time.Time(nil), nullProto(), listType(tTime)},
  104. {[]time.Time{t1, t2, t3, t4}, listProto(timeProto(t1), timeProto(t2), timeProto(t3), timeProto(t4)), listType(tTime)},
  105. {[]NullTime{{t1, true}, {t1, false}}, listProto(timeProto(t1), nullProto()), listType(tTime)},
  106. // DATE / DATE ARRAY
  107. {d1, dateProto(d1), tDate},
  108. {NullDate{d1, true}, dateProto(d1), tDate},
  109. {NullDate{civil.Date{}, false}, nullProto(), tDate},
  110. {[]civil.Date(nil), nullProto(), listType(tDate)},
  111. {[]civil.Date{d1, d2}, listProto(dateProto(d1), dateProto(d2)), listType(tDate)},
  112. {[]NullDate{{d1, true}, {civil.Date{}, false}}, listProto(dateProto(d1), nullProto()), listType(tDate)},
  113. // GenericColumnValue
  114. {GenericColumnValue{tString, stringProto("abc")}, stringProto("abc"), tString},
  115. {GenericColumnValue{tString, nullProto()}, nullProto(), tString},
  116. // not actually valid (stringProto inside int list), but demonstrates pass-through.
  117. {
  118. GenericColumnValue{
  119. Type: listType(tInt),
  120. Value: listProto(intProto(5), nullProto(), stringProto("bcd")),
  121. },
  122. listProto(intProto(5), nullProto(), stringProto("bcd")),
  123. listType(tInt),
  124. },
  125. // placeholder
  126. {CommitTimestamp, stringProto(commitTimestampPlaceholderString), tTime},
  127. } {
  128. got, gotType, err := encodeValue(test.in)
  129. if err != nil {
  130. t.Fatalf("#%d: got error during encoding: %v, want nil", i, err)
  131. }
  132. if !testEqual(got, test.want) {
  133. t.Errorf("#%d: got encode result: %v, want %v", i, got, test.want)
  134. }
  135. if !testEqual(gotType, test.wantType) {
  136. t.Errorf("#%d: got encode type: %v, want %v", i, gotType, test.wantType)
  137. }
  138. }
  139. }
  140. type encodeTest struct {
  141. desc string
  142. in interface{}
  143. want *proto3.Value
  144. wantType *sppb.Type
  145. }
  146. func checkStructEncoding(desc string, got *proto3.Value, gotType *sppb.Type,
  147. want *proto3.Value, wantType *sppb.Type, t *testing.T) {
  148. if !testEqual(got, want) {
  149. t.Errorf("Test %s: got encode result: %v, want %v", desc, got, want)
  150. }
  151. if !testEqual(gotType, wantType) {
  152. t.Errorf("Test %s: got encode type: %v, want %v", desc, gotType, wantType)
  153. }
  154. }
  155. // Testcase code
  156. func encodeStructValue(test encodeTest, t *testing.T) {
  157. got, gotType, err := encodeValue(test.in)
  158. if err != nil {
  159. t.Fatalf("Test %s: got error during encoding: %v, want nil", test.desc, err)
  160. }
  161. checkStructEncoding(test.desc, got, gotType, test.want, test.wantType, t)
  162. }
  163. func TestEncodeStructValuePointers(t *testing.T) {
  164. type structf struct {
  165. F int `spanner:"ff2"`
  166. }
  167. nestedStructProto := structType(mkField("ff2", intType()))
  168. type testType struct {
  169. Stringf string
  170. Structf *structf
  171. ArrStructf []*structf
  172. }
  173. testTypeProto := structType(
  174. mkField("Stringf", stringType()),
  175. mkField("Structf", nestedStructProto),
  176. mkField("ArrStructf", listType(nestedStructProto)))
  177. for _, test := range []encodeTest{
  178. {
  179. "Pointer to Go struct with pointers-to-(array)-struct fields.",
  180. &testType{"hello", &structf{50}, []*structf{&structf{30}, &structf{40}}},
  181. listProto(
  182. stringProto("hello"),
  183. listProto(intProto(50)),
  184. listProto(
  185. listProto(intProto(30)),
  186. listProto(intProto(40)))),
  187. testTypeProto,
  188. },
  189. {
  190. "Nil pointer to Go struct representing a NULL struct value.",
  191. (*testType)(nil),
  192. nullProto(),
  193. testTypeProto,
  194. },
  195. {
  196. "Slice of pointers to Go structs with NULL and non-NULL elements.",
  197. []*testType{
  198. (*testType)(nil),
  199. &testType{"hello", nil, []*structf{nil, &structf{40}}},
  200. &testType{"world", &structf{70}, nil},
  201. },
  202. listProto(
  203. nullProto(),
  204. listProto(
  205. stringProto("hello"),
  206. nullProto(),
  207. listProto(nullProto(), listProto(intProto(40)))),
  208. listProto(
  209. stringProto("world"),
  210. listProto(intProto(70)),
  211. nullProto())),
  212. listType(testTypeProto),
  213. },
  214. {
  215. "Nil slice of pointers to structs representing a NULL array of structs.",
  216. []*testType(nil),
  217. nullProto(),
  218. listType(testTypeProto),
  219. },
  220. {
  221. "Empty slice of pointers to structs representing an empty array of structs.",
  222. []*testType{},
  223. listProto(),
  224. listType(testTypeProto),
  225. },
  226. } {
  227. encodeStructValue(test, t)
  228. }
  229. }
  230. func TestEncodeStructValueErrors(t *testing.T) {
  231. type Embedded struct {
  232. A int
  233. }
  234. type embedded struct {
  235. B bool
  236. }
  237. x := 0
  238. for _, test := range []struct {
  239. desc string
  240. in interface{}
  241. wantErr error
  242. }{
  243. {
  244. "Unsupported embedded fields.",
  245. struct{ Embedded }{Embedded{10}},
  246. errUnsupportedEmbeddedStructFields("Embedded"),
  247. },
  248. {
  249. "Unsupported pointer to embedded fields.",
  250. struct{ *Embedded }{&Embedded{10}},
  251. errUnsupportedEmbeddedStructFields("Embedded"),
  252. },
  253. {
  254. "Unsupported embedded + unexported fields.",
  255. struct {
  256. int
  257. *bool
  258. embedded
  259. }{10, nil, embedded{false}},
  260. errUnsupportedEmbeddedStructFields("int"),
  261. },
  262. {
  263. "Unsupported type.",
  264. (**struct{})(nil),
  265. errEncoderUnsupportedType((**struct{})(nil)),
  266. },
  267. {
  268. "Unsupported type.",
  269. 3,
  270. errEncoderUnsupportedType(3),
  271. },
  272. {
  273. "Unsupported type.",
  274. &x,
  275. errEncoderUnsupportedType(&x),
  276. },
  277. } {
  278. _, _, got := encodeStruct(test.in)
  279. if got == nil || !testEqual(test.wantErr, got) {
  280. t.Errorf("Test: %s, expected error %v during decoding, got %v", test.desc, test.wantErr, got)
  281. }
  282. }
  283. }
  284. func TestEncodeStructValueArrayStructFields(t *testing.T) {
  285. type structf struct {
  286. Intff int
  287. }
  288. structfType := structType(mkField("Intff", intType()))
  289. for _, test := range []encodeTest{
  290. {
  291. "Unnamed array-of-struct-typed field.",
  292. struct {
  293. Intf int
  294. ArrStructf []structf `spanner:""`
  295. }{10, []structf{structf{1}, structf{2}}},
  296. listProto(
  297. intProto(10),
  298. listProto(
  299. listProto(intProto(1)),
  300. listProto(intProto(2)))),
  301. structType(
  302. mkField("Intf", intType()),
  303. mkField("", listType(structfType))),
  304. },
  305. {
  306. "Null array-of-struct-typed field.",
  307. struct {
  308. Intf int
  309. ArrStructf []structf
  310. }{10, []structf(nil)},
  311. listProto(intProto(10), nullProto()),
  312. structType(
  313. mkField("Intf", intType()),
  314. mkField("ArrStructf", listType(structfType))),
  315. },
  316. {
  317. "Array-of-struct-typed field representing empty array.",
  318. struct {
  319. Intf int
  320. ArrStructf []structf
  321. }{10, []structf{}},
  322. listProto(intProto(10), listProto([]*proto3.Value{}...)),
  323. structType(
  324. mkField("Intf", intType()),
  325. mkField("ArrStructf", listType(structfType))),
  326. },
  327. {
  328. "Array-of-struct-typed field with nullable struct elements.",
  329. struct {
  330. Intf int
  331. ArrStructf []*structf
  332. }{
  333. 10,
  334. []*structf{(*structf)(nil), &structf{1}},
  335. },
  336. listProto(
  337. intProto(10),
  338. listProto(
  339. nullProto(),
  340. listProto(intProto(1)))),
  341. structType(
  342. mkField("Intf", intType()),
  343. mkField("ArrStructf", listType(structfType))),
  344. },
  345. } {
  346. encodeStructValue(test, t)
  347. }
  348. }
  349. func TestEncodeStructValueStructFields(t *testing.T) {
  350. type structf struct {
  351. Intff int
  352. }
  353. structfType := structType(mkField("Intff", intType()))
  354. for _, test := range []encodeTest{
  355. {
  356. "Named struct-type field.",
  357. struct {
  358. Intf int
  359. Structf structf
  360. }{10, structf{10}},
  361. listProto(intProto(10), listProto(intProto(10))),
  362. structType(
  363. mkField("Intf", intType()),
  364. mkField("Structf", structfType)),
  365. },
  366. {
  367. "Unnamed struct-type field.",
  368. struct {
  369. Intf int
  370. Structf structf `spanner:""`
  371. }{10, structf{10}},
  372. listProto(intProto(10), listProto(intProto(10))),
  373. structType(
  374. mkField("Intf", intType()),
  375. mkField("", structfType)),
  376. },
  377. {
  378. "Duplicate struct-typed field.",
  379. struct {
  380. Structf1 structf `spanner:""`
  381. Structf2 structf `spanner:""`
  382. }{structf{10}, structf{20}},
  383. listProto(listProto(intProto(10)), listProto(intProto(20))),
  384. structType(
  385. mkField("", structfType),
  386. mkField("", structfType)),
  387. },
  388. {
  389. "Null struct-typed field.",
  390. struct {
  391. Intf int
  392. Structf *structf
  393. }{10, nil},
  394. listProto(intProto(10), nullProto()),
  395. structType(
  396. mkField("Intf", intType()),
  397. mkField("Structf", structfType)),
  398. },
  399. {
  400. "Empty struct-typed field.",
  401. struct {
  402. Intf int
  403. Structf struct{}
  404. }{10, struct{}{}},
  405. listProto(intProto(10), listProto([]*proto3.Value{}...)),
  406. structType(
  407. mkField("Intf", intType()),
  408. mkField("Structf", structType([]*sppb.StructType_Field{}...))),
  409. },
  410. } {
  411. encodeStructValue(test, t)
  412. }
  413. }
  414. func TestEncodeStructValueFieldNames(t *testing.T) {
  415. type embedded struct {
  416. B bool
  417. }
  418. for _, test := range []encodeTest{
  419. {
  420. "Duplicate fields.",
  421. struct {
  422. Field1 int `spanner:"field"`
  423. DupField1 int `spanner:"field"`
  424. }{10, 20},
  425. listProto(intProto(10), intProto(20)),
  426. structType(
  427. mkField("field", intType()),
  428. mkField("field", intType())),
  429. },
  430. {
  431. "Duplicate Fields (different types).",
  432. struct {
  433. IntField int `spanner:"field"`
  434. StringField string `spanner:"field"`
  435. }{10, "abc"},
  436. listProto(intProto(10), stringProto("abc")),
  437. structType(
  438. mkField("field", intType()),
  439. mkField("field", stringType())),
  440. },
  441. {
  442. "Duplicate unnamed fields.",
  443. struct {
  444. Dup int `spanner:""`
  445. Dup1 int `spanner:""`
  446. }{10, 20},
  447. listProto(intProto(10), intProto(20)),
  448. structType(
  449. mkField("", intType()),
  450. mkField("", intType())),
  451. },
  452. {
  453. "Named and unnamed fields.",
  454. struct {
  455. Field string
  456. Field1 int `spanner:""`
  457. Field2 string `spanner:"field"`
  458. }{"abc", 10, "def"},
  459. listProto(stringProto("abc"), intProto(10), stringProto("def")),
  460. structType(
  461. mkField("Field", stringType()),
  462. mkField("", intType()),
  463. mkField("field", stringType())),
  464. },
  465. {
  466. "Ignored unexported fields.",
  467. struct {
  468. Field int
  469. field bool
  470. Field1 string `spanner:"field"`
  471. }{10, false, "abc"},
  472. listProto(intProto(10), stringProto("abc")),
  473. structType(
  474. mkField("Field", intType()),
  475. mkField("field", stringType())),
  476. },
  477. {
  478. "Ignored unexported struct/slice fields.",
  479. struct {
  480. a []*embedded
  481. b []embedded
  482. c embedded
  483. d *embedded
  484. Field1 string `spanner:"field"`
  485. }{nil, nil, embedded{}, nil, "def"},
  486. listProto(stringProto("def")),
  487. structType(
  488. mkField("field", stringType())),
  489. },
  490. } {
  491. encodeStructValue(test, t)
  492. }
  493. }
  494. func TestEncodeStructValueBasicFields(t *testing.T) {
  495. StructTypeProto := structType(
  496. mkField("Stringf", stringType()),
  497. mkField("Intf", intType()),
  498. mkField("Boolf", boolType()),
  499. mkField("Floatf", floatType()),
  500. mkField("Bytef", bytesType()),
  501. mkField("Timef", timeType()),
  502. mkField("Datef", dateType()))
  503. for _, test := range []encodeTest{
  504. {
  505. "Basic types.",
  506. struct {
  507. Stringf string
  508. Intf int
  509. Boolf bool
  510. Floatf float64
  511. Bytef []byte
  512. Timef time.Time
  513. Datef civil.Date
  514. }{"abc", 300, false, 3.45, []byte("foo"), t1, d1},
  515. listProto(
  516. stringProto("abc"),
  517. intProto(300),
  518. boolProto(false),
  519. floatProto(3.45),
  520. bytesProto([]byte("foo")),
  521. timeProto(t1),
  522. dateProto(d1)),
  523. StructTypeProto,
  524. },
  525. {
  526. "Basic types null values.",
  527. struct {
  528. Stringf NullString
  529. Intf NullInt64
  530. Boolf NullBool
  531. Floatf NullFloat64
  532. Bytef []byte
  533. Timef NullTime
  534. Datef NullDate
  535. }{
  536. NullString{"abc", false},
  537. NullInt64{4, false},
  538. NullBool{false, false},
  539. NullFloat64{5.6, false},
  540. nil,
  541. NullTime{t1, false},
  542. NullDate{d1, false},
  543. },
  544. listProto(
  545. nullProto(),
  546. nullProto(),
  547. nullProto(),
  548. nullProto(),
  549. nullProto(),
  550. nullProto(),
  551. nullProto()),
  552. StructTypeProto,
  553. },
  554. } {
  555. encodeStructValue(test, t)
  556. }
  557. }
  558. func TestEncodeStructValueArrayFields(t *testing.T) {
  559. StructTypeProto := structType(
  560. mkField("Stringf", listType(stringType())),
  561. mkField("Intf", listType(intType())),
  562. mkField("Int64f", listType(intType())),
  563. mkField("Boolf", listType(boolType())),
  564. mkField("Floatf", listType(floatType())),
  565. mkField("Bytef", listType(bytesType())),
  566. mkField("Timef", listType(timeType())),
  567. mkField("Datef", listType(dateType())))
  568. for _, test := range []encodeTest{
  569. {
  570. "Arrays of basic types with non-nullable elements",
  571. struct {
  572. Stringf []string
  573. Intf []int
  574. Int64f []int64
  575. Boolf []bool
  576. Floatf []float64
  577. Bytef [][]byte
  578. Timef []time.Time
  579. Datef []civil.Date
  580. }{
  581. []string{"abc", "def"},
  582. []int{4, 67},
  583. []int64{5, 68},
  584. []bool{false, true},
  585. []float64{3.45, 0.93},
  586. [][]byte{[]byte("foo"), nil},
  587. []time.Time{t1, t2},
  588. []civil.Date{d1, d2},
  589. },
  590. listProto(
  591. listProto(stringProto("abc"), stringProto("def")),
  592. listProto(intProto(4), intProto(67)),
  593. listProto(intProto(5), intProto(68)),
  594. listProto(boolProto(false), boolProto(true)),
  595. listProto(floatProto(3.45), floatProto(0.93)),
  596. listProto(bytesProto([]byte("foo")), nullProto()),
  597. listProto(timeProto(t1), timeProto(t2)),
  598. listProto(dateProto(d1), dateProto(d2))),
  599. StructTypeProto,
  600. },
  601. {
  602. "Arrays of basic types with nullable elements.",
  603. struct {
  604. Stringf []NullString
  605. Intf []NullInt64
  606. Int64f []NullInt64
  607. Boolf []NullBool
  608. Floatf []NullFloat64
  609. Bytef [][]byte
  610. Timef []NullTime
  611. Datef []NullDate
  612. }{
  613. []NullString{NullString{"abc", false}, NullString{"def", true}},
  614. []NullInt64{NullInt64{4, false}, NullInt64{67, true}},
  615. []NullInt64{NullInt64{5, false}, NullInt64{68, true}},
  616. []NullBool{NullBool{true, false}, NullBool{false, true}},
  617. []NullFloat64{NullFloat64{3.45, false}, NullFloat64{0.93, true}},
  618. [][]byte{[]byte("foo"), nil},
  619. []NullTime{NullTime{t1, false}, NullTime{t2, true}},
  620. []NullDate{NullDate{d1, false}, NullDate{d2, true}},
  621. },
  622. listProto(
  623. listProto(nullProto(), stringProto("def")),
  624. listProto(nullProto(), intProto(67)),
  625. listProto(nullProto(), intProto(68)),
  626. listProto(nullProto(), boolProto(false)),
  627. listProto(nullProto(), floatProto(0.93)),
  628. listProto(bytesProto([]byte("foo")), nullProto()),
  629. listProto(nullProto(), timeProto(t2)),
  630. listProto(nullProto(), dateProto(d2))),
  631. StructTypeProto,
  632. },
  633. {
  634. "Null arrays of basic types.",
  635. struct {
  636. Stringf []NullString
  637. Intf []NullInt64
  638. Int64f []NullInt64
  639. Boolf []NullBool
  640. Floatf []NullFloat64
  641. Bytef [][]byte
  642. Timef []NullTime
  643. Datef []NullDate
  644. }{
  645. nil,
  646. nil,
  647. nil,
  648. nil,
  649. nil,
  650. nil,
  651. nil,
  652. nil,
  653. },
  654. listProto(
  655. nullProto(),
  656. nullProto(),
  657. nullProto(),
  658. nullProto(),
  659. nullProto(),
  660. nullProto(),
  661. nullProto(),
  662. nullProto()),
  663. StructTypeProto,
  664. },
  665. } {
  666. encodeStructValue(test, t)
  667. }
  668. }
  669. // Test decoding Values.
  670. func TestDecodeValue(t *testing.T) {
  671. for i, test := range []struct {
  672. in *proto3.Value
  673. t *sppb.Type
  674. want interface{}
  675. fail bool
  676. }{
  677. // STRING
  678. {stringProto("abc"), stringType(), "abc", false},
  679. {nullProto(), stringType(), "abc", true},
  680. {stringProto("abc"), stringType(), NullString{"abc", true}, false},
  681. {nullProto(), stringType(), NullString{}, false},
  682. // STRING ARRAY with []NullString
  683. {
  684. listProto(stringProto("abc"), nullProto(), stringProto("bcd")),
  685. listType(stringType()),
  686. []NullString{{"abc", true}, {}, {"bcd", true}},
  687. false,
  688. },
  689. {nullProto(), listType(stringType()), []NullString(nil), false},
  690. // STRING ARRAY with []string
  691. {
  692. listProto(stringProto("abc"), stringProto("bcd")),
  693. listType(stringType()),
  694. []string{"abc", "bcd"},
  695. false,
  696. },
  697. // BYTES
  698. {bytesProto([]byte("ab")), bytesType(), []byte("ab"), false},
  699. {nullProto(), bytesType(), []byte(nil), false},
  700. // BYTES ARRAY
  701. {listProto(bytesProto([]byte("ab")), nullProto()), listType(bytesType()), [][]byte{[]byte("ab"), nil}, false},
  702. {nullProto(), listType(bytesType()), [][]byte(nil), false},
  703. //INT64
  704. {intProto(15), intType(), int64(15), false},
  705. {nullProto(), intType(), int64(0), true},
  706. {intProto(15), intType(), NullInt64{15, true}, false},
  707. {nullProto(), intType(), NullInt64{}, false},
  708. // INT64 ARRAY with []NullInt64
  709. {listProto(intProto(91), nullProto(), intProto(87)), listType(intType()), []NullInt64{{91, true}, {}, {87, true}}, false},
  710. {nullProto(), listType(intType()), []NullInt64(nil), false},
  711. // INT64 ARRAY with []int64
  712. {listProto(intProto(91), intProto(87)), listType(intType()), []int64{91, 87}, false},
  713. // BOOL
  714. {boolProto(true), boolType(), true, false},
  715. {nullProto(), boolType(), true, true},
  716. {boolProto(true), boolType(), NullBool{true, true}, false},
  717. {nullProto(), boolType(), NullBool{}, false},
  718. // BOOL ARRAY with []NullBool
  719. {listProto(boolProto(true), boolProto(false), nullProto()), listType(boolType()), []NullBool{{true, true}, {false, true}, {}}, false},
  720. {nullProto(), listType(boolType()), []NullBool(nil), false},
  721. // BOOL ARRAY with []bool
  722. {listProto(boolProto(true), boolProto(false)), listType(boolType()), []bool{true, false}, false},
  723. // FLOAT64
  724. {floatProto(3.14), floatType(), 3.14, false},
  725. {nullProto(), floatType(), 0.00, true},
  726. {floatProto(3.14), floatType(), NullFloat64{3.14, true}, false},
  727. {nullProto(), floatType(), NullFloat64{}, false},
  728. // FLOAT64 ARRAY with []NullFloat64
  729. {
  730. listProto(floatProto(math.Inf(1)), floatProto(math.Inf(-1)), nullProto(), floatProto(3.1)),
  731. listType(floatType()),
  732. []NullFloat64{{math.Inf(1), true}, {math.Inf(-1), true}, {}, {3.1, true}},
  733. false,
  734. },
  735. {nullProto(), listType(floatType()), []NullFloat64(nil), false},
  736. // FLOAT64 ARRAY with []float64
  737. {
  738. listProto(floatProto(math.Inf(1)), floatProto(math.Inf(-1)), floatProto(3.1)),
  739. listType(floatType()),
  740. []float64{math.Inf(1), math.Inf(-1), 3.1},
  741. false,
  742. },
  743. // TIMESTAMP
  744. {timeProto(t1), timeType(), t1, false},
  745. {timeProto(t1), timeType(), NullTime{t1, true}, false},
  746. {nullProto(), timeType(), NullTime{}, false},
  747. // TIMESTAMP ARRAY with []NullTime
  748. {listProto(timeProto(t1), timeProto(t2), timeProto(t3), nullProto()), listType(timeType()), []NullTime{{t1, true}, {t2, true}, {t3, true}, {}}, false},
  749. {nullProto(), listType(timeType()), []NullTime(nil), false},
  750. // TIMESTAMP ARRAY with []time.Time
  751. {listProto(timeProto(t1), timeProto(t2), timeProto(t3)), listType(timeType()), []time.Time{t1, t2, t3}, false},
  752. // DATE
  753. {dateProto(d1), dateType(), d1, false},
  754. {dateProto(d1), dateType(), NullDate{d1, true}, false},
  755. {nullProto(), dateType(), NullDate{}, false},
  756. // DATE ARRAY with []NullDate
  757. {listProto(dateProto(d1), dateProto(d2), nullProto()), listType(dateType()), []NullDate{{d1, true}, {d2, true}, {}}, false},
  758. {nullProto(), listType(dateType()), []NullDate(nil), false},
  759. // DATE ARRAY with []civil.Date
  760. {listProto(dateProto(d1), dateProto(d2)), listType(dateType()), []civil.Date{d1, d2}, false},
  761. // STRUCT ARRAY
  762. // STRUCT schema is equal to the following Go struct:
  763. // type s struct {
  764. // Col1 NullInt64
  765. // Col2 []struct {
  766. // SubCol1 float64
  767. // SubCol2 string
  768. // }
  769. // }
  770. {
  771. in: listProto(
  772. listProto(
  773. intProto(3),
  774. listProto(
  775. listProto(floatProto(3.14), stringProto("this")),
  776. listProto(floatProto(0.57), stringProto("siht")),
  777. ),
  778. ),
  779. listProto(
  780. nullProto(),
  781. nullProto(),
  782. ),
  783. nullProto(),
  784. ),
  785. t: listType(
  786. structType(
  787. mkField("Col1", intType()),
  788. mkField(
  789. "Col2",
  790. listType(
  791. structType(
  792. mkField("SubCol1", floatType()),
  793. mkField("SubCol2", stringType()),
  794. ),
  795. ),
  796. ),
  797. ),
  798. ),
  799. want: []NullRow{
  800. {
  801. Row: Row{
  802. fields: []*sppb.StructType_Field{
  803. mkField("Col1", intType()),
  804. mkField(
  805. "Col2",
  806. listType(
  807. structType(
  808. mkField("SubCol1", floatType()),
  809. mkField("SubCol2", stringType()),
  810. ),
  811. ),
  812. ),
  813. },
  814. vals: []*proto3.Value{
  815. intProto(3),
  816. listProto(
  817. listProto(floatProto(3.14), stringProto("this")),
  818. listProto(floatProto(0.57), stringProto("siht")),
  819. ),
  820. },
  821. },
  822. Valid: true,
  823. },
  824. {
  825. Row: Row{
  826. fields: []*sppb.StructType_Field{
  827. mkField("Col1", intType()),
  828. mkField(
  829. "Col2",
  830. listType(
  831. structType(
  832. mkField("SubCol1", floatType()),
  833. mkField("SubCol2", stringType()),
  834. ),
  835. ),
  836. ),
  837. },
  838. vals: []*proto3.Value{
  839. nullProto(),
  840. nullProto(),
  841. },
  842. },
  843. Valid: true,
  844. },
  845. {},
  846. },
  847. fail: false,
  848. },
  849. {
  850. in: listProto(
  851. listProto(
  852. intProto(3),
  853. listProto(
  854. listProto(floatProto(3.14), stringProto("this")),
  855. listProto(floatProto(0.57), stringProto("siht")),
  856. ),
  857. ),
  858. listProto(
  859. nullProto(),
  860. nullProto(),
  861. ),
  862. nullProto(),
  863. ),
  864. t: listType(
  865. structType(
  866. mkField("Col1", intType()),
  867. mkField(
  868. "Col2",
  869. listType(
  870. structType(
  871. mkField("SubCol1", floatType()),
  872. mkField("SubCol2", stringType()),
  873. ),
  874. ),
  875. ),
  876. ),
  877. ),
  878. want: []*struct {
  879. Col1 NullInt64
  880. StructCol []*struct {
  881. SubCol1 NullFloat64
  882. SubCol2 string
  883. } `spanner:"Col2"`
  884. }{
  885. {
  886. Col1: NullInt64{3, true},
  887. StructCol: []*struct {
  888. SubCol1 NullFloat64
  889. SubCol2 string
  890. }{
  891. {
  892. SubCol1: NullFloat64{3.14, true},
  893. SubCol2: "this",
  894. },
  895. {
  896. SubCol1: NullFloat64{0.57, true},
  897. SubCol2: "siht",
  898. },
  899. },
  900. },
  901. {
  902. Col1: NullInt64{},
  903. StructCol: []*struct {
  904. SubCol1 NullFloat64
  905. SubCol2 string
  906. }(nil),
  907. },
  908. nil,
  909. },
  910. fail: false,
  911. },
  912. // GenericColumnValue
  913. {stringProto("abc"), stringType(), GenericColumnValue{stringType(), stringProto("abc")}, false},
  914. {nullProto(), stringType(), GenericColumnValue{stringType(), nullProto()}, false},
  915. // not actually valid (stringProto inside int list), but demonstrates pass-through.
  916. {
  917. in: listProto(intProto(5), nullProto(), stringProto("bcd")),
  918. t: listType(intType()),
  919. want: GenericColumnValue{
  920. Type: listType(intType()),
  921. Value: listProto(intProto(5), nullProto(), stringProto("bcd")),
  922. },
  923. fail: false,
  924. },
  925. } {
  926. gotp := reflect.New(reflect.TypeOf(test.want))
  927. if err := decodeValue(test.in, test.t, gotp.Interface()); err != nil {
  928. if !test.fail {
  929. t.Errorf("%d: cannot decode %v(%v): %v", i, test.in, test.t, err)
  930. }
  931. continue
  932. }
  933. if test.fail {
  934. t.Errorf("%d: decoding %v(%v) succeeds unexpectedly, want error", i, test.in, test.t)
  935. continue
  936. }
  937. got := reflect.Indirect(gotp).Interface()
  938. if !testEqual(got, test.want) {
  939. t.Errorf("%d: unexpected decoding result - got %v, want %v", i, got, test.want)
  940. continue
  941. }
  942. }
  943. }
  944. // Test error cases for decodeValue.
  945. func TestDecodeValueErrors(t *testing.T) {
  946. var s string
  947. for i, test := range []struct {
  948. in *proto3.Value
  949. t *sppb.Type
  950. v interface{}
  951. }{
  952. {nullProto(), stringType(), nil},
  953. {nullProto(), stringType(), 1},
  954. {timeProto(t1), timeType(), &s},
  955. } {
  956. err := decodeValue(test.in, test.t, test.v)
  957. if err == nil {
  958. t.Errorf("#%d: want error, got nil", i)
  959. }
  960. }
  961. }
  962. // Test NaN encoding/decoding.
  963. func TestNaN(t *testing.T) {
  964. // Decode NaN value.
  965. f := 0.0
  966. nf := NullFloat64{}
  967. // To float64
  968. if err := decodeValue(floatProto(math.NaN()), floatType(), &f); err != nil {
  969. t.Errorf("decodeValue returns %q for %v, want nil", err, floatProto(math.NaN()))
  970. }
  971. if !math.IsNaN(f) {
  972. t.Errorf("f = %v, want %v", f, math.NaN())
  973. }
  974. // To NullFloat64
  975. if err := decodeValue(floatProto(math.NaN()), floatType(), &nf); err != nil {
  976. t.Errorf("decodeValue returns %q for %v, want nil", err, floatProto(math.NaN()))
  977. }
  978. if !math.IsNaN(nf.Float64) || !nf.Valid {
  979. t.Errorf("f = %v, want %v", f, NullFloat64{math.NaN(), true})
  980. }
  981. // Encode NaN value
  982. // From float64
  983. v, _, err := encodeValue(math.NaN())
  984. if err != nil {
  985. t.Errorf("encodeValue returns %q for NaN, want nil", err)
  986. }
  987. x, ok := v.GetKind().(*proto3.Value_NumberValue)
  988. if !ok {
  989. t.Errorf("incorrect type for v.GetKind(): %T, want *proto3.Value_NumberValue", v.GetKind())
  990. }
  991. if !math.IsNaN(x.NumberValue) {
  992. t.Errorf("x.NumberValue = %v, want %v", x.NumberValue, math.NaN())
  993. }
  994. // From NullFloat64
  995. v, _, err = encodeValue(NullFloat64{math.NaN(), true})
  996. if err != nil {
  997. t.Errorf("encodeValue returns %q for NaN, want nil", err)
  998. }
  999. x, ok = v.GetKind().(*proto3.Value_NumberValue)
  1000. if !ok {
  1001. t.Errorf("incorrect type for v.GetKind(): %T, want *proto3.Value_NumberValue", v.GetKind())
  1002. }
  1003. if !math.IsNaN(x.NumberValue) {
  1004. t.Errorf("x.NumberValue = %v, want %v", x.NumberValue, math.NaN())
  1005. }
  1006. }
  1007. func TestGenericColumnValue(t *testing.T) {
  1008. for _, test := range []struct {
  1009. in GenericColumnValue
  1010. want interface{}
  1011. fail bool
  1012. }{
  1013. {GenericColumnValue{stringType(), stringProto("abc")}, "abc", false},
  1014. {GenericColumnValue{stringType(), stringProto("abc")}, 5, true},
  1015. {GenericColumnValue{listType(intType()), listProto(intProto(91), nullProto(), intProto(87))}, []NullInt64{{91, true}, {}, {87, true}}, false},
  1016. {GenericColumnValue{intType(), intProto(42)}, GenericColumnValue{intType(), intProto(42)}, false}, // trippy! :-)
  1017. } {
  1018. gotp := reflect.New(reflect.TypeOf(test.want))
  1019. if err := test.in.Decode(gotp.Interface()); err != nil {
  1020. if !test.fail {
  1021. t.Errorf("cannot decode %v to %v: %v", test.in, test.want, err)
  1022. }
  1023. continue
  1024. }
  1025. if test.fail {
  1026. t.Errorf("decoding %v to %v succeeds unexpectedly", test.in, test.want)
  1027. }
  1028. // Test we can go backwards as well.
  1029. v, err := newGenericColumnValue(test.want)
  1030. if err != nil {
  1031. t.Errorf("NewGenericColumnValue failed: %v", err)
  1032. continue
  1033. }
  1034. if !testEqual(*v, test.in) {
  1035. t.Errorf("unexpected encode result - got %v, want %v", v, test.in)
  1036. }
  1037. }
  1038. }
  1039. func TestDecodeStruct(t *testing.T) {
  1040. stype := &sppb.StructType{Fields: []*sppb.StructType_Field{
  1041. {Name: "Id", Type: stringType()},
  1042. {Name: "Time", Type: timeType()},
  1043. }}
  1044. lv := listValueProto(stringProto("id"), timeProto(t1))
  1045. type (
  1046. S1 struct {
  1047. Id string
  1048. Time time.Time
  1049. }
  1050. S2 struct {
  1051. Id string
  1052. Time string
  1053. }
  1054. )
  1055. var (
  1056. s1 S1
  1057. s2 S2
  1058. )
  1059. for i, test := range []struct {
  1060. ptr interface{}
  1061. want interface{}
  1062. fail bool
  1063. }{
  1064. {
  1065. ptr: &s1,
  1066. want: &S1{Id: "id", Time: t1},
  1067. },
  1068. {
  1069. ptr: &s2,
  1070. fail: true,
  1071. },
  1072. } {
  1073. err := decodeStruct(stype, lv, test.ptr)
  1074. if (err != nil) != test.fail {
  1075. t.Errorf("#%d: got error %v, wanted fail: %v", i, err, test.fail)
  1076. }
  1077. if err == nil && !testEqual(test.ptr, test.want) {
  1078. t.Errorf("#%d: got %+v, want %+v", i, test.ptr, test.want)
  1079. }
  1080. }
  1081. }