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.
 
 
 

197 lines
6.9 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. "testing"
  17. "time"
  18. "cloud.google.com/go/civil"
  19. "github.com/golang/protobuf/proto"
  20. proto3 "github.com/golang/protobuf/ptypes/struct"
  21. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  22. )
  23. func TestConvertParams(t *testing.T) {
  24. st := Statement{
  25. SQL: "SELECT id from t_foo WHERE col = @var",
  26. Params: map[string]interface{}{"var": nil},
  27. }
  28. var (
  29. t1, _ = time.Parse(time.RFC3339Nano, "2016-11-15T15:04:05.999999999Z")
  30. // Boundaries
  31. t2, _ = time.Parse(time.RFC3339Nano, "0001-01-01T00:00:00.000000000Z")
  32. t3, _ = time.Parse(time.RFC3339Nano, "9999-12-31T23:59:59.999999999Z")
  33. d1, _ = civil.ParseDate("2016-11-15")
  34. // Boundaries
  35. d2, _ = civil.ParseDate("0001-01-01")
  36. d3, _ = civil.ParseDate("9999-12-31")
  37. )
  38. type staticStruct struct {
  39. Field int `spanner:"field"`
  40. }
  41. var (
  42. s1 = staticStruct{10}
  43. s2 = staticStruct{20}
  44. )
  45. for _, test := range []struct {
  46. val interface{}
  47. wantField *proto3.Value
  48. wantType *sppb.Type
  49. }{
  50. // bool
  51. {true, boolProto(true), boolType()},
  52. {NullBool{true, true}, boolProto(true), boolType()},
  53. {NullBool{true, false}, nullProto(), boolType()},
  54. {[]bool(nil), nullProto(), listType(boolType())},
  55. {[]bool{}, listProto(), listType(boolType())},
  56. {[]bool{true, false}, listProto(boolProto(true), boolProto(false)), listType(boolType())},
  57. {[]NullBool(nil), nullProto(), listType(boolType())},
  58. {[]NullBool{}, listProto(), listType(boolType())},
  59. {[]NullBool{{true, true}, {}}, listProto(boolProto(true), nullProto()), listType(boolType())},
  60. // int
  61. {int(1), intProto(1), intType()},
  62. {[]int(nil), nullProto(), listType(intType())},
  63. {[]int{}, listProto(), listType(intType())},
  64. {[]int{1, 2}, listProto(intProto(1), intProto(2)), listType(intType())},
  65. // int64
  66. {int64(1), intProto(1), intType()},
  67. {NullInt64{5, true}, intProto(5), intType()},
  68. {NullInt64{5, false}, nullProto(), intType()},
  69. {[]int64(nil), nullProto(), listType(intType())},
  70. {[]int64{}, listProto(), listType(intType())},
  71. {[]int64{1, 2}, listProto(intProto(1), intProto(2)), listType(intType())},
  72. {[]NullInt64(nil), nullProto(), listType(intType())},
  73. {[]NullInt64{}, listProto(), listType(intType())},
  74. {[]NullInt64{{1, true}, {}}, listProto(intProto(1), nullProto()), listType(intType())},
  75. // float64
  76. {0.0, floatProto(0.0), floatType()},
  77. {math.Inf(1), floatProto(math.Inf(1)), floatType()},
  78. {math.Inf(-1), floatProto(math.Inf(-1)), floatType()},
  79. {math.NaN(), floatProto(math.NaN()), floatType()},
  80. {NullFloat64{2.71, true}, floatProto(2.71), floatType()},
  81. {NullFloat64{1.41, false}, nullProto(), floatType()},
  82. {[]float64(nil), nullProto(), listType(floatType())},
  83. {[]float64{}, listProto(), listType(floatType())},
  84. {[]float64{2.72, math.Inf(1)}, listProto(floatProto(2.72), floatProto(math.Inf(1))), listType(floatType())},
  85. {[]NullFloat64(nil), nullProto(), listType(floatType())},
  86. {[]NullFloat64{}, listProto(), listType(floatType())},
  87. {[]NullFloat64{{2.72, true}, {}}, listProto(floatProto(2.72), nullProto()), listType(floatType())},
  88. // string
  89. {"", stringProto(""), stringType()},
  90. {"foo", stringProto("foo"), stringType()},
  91. {NullString{"bar", true}, stringProto("bar"), stringType()},
  92. {NullString{"bar", false}, nullProto(), stringType()},
  93. {[]string(nil), nullProto(), listType(stringType())},
  94. {[]string{}, listProto(), listType(stringType())},
  95. {[]string{"foo", "bar"}, listProto(stringProto("foo"), stringProto("bar")), listType(stringType())},
  96. {[]NullString(nil), nullProto(), listType(stringType())},
  97. {[]NullString{}, listProto(), listType(stringType())},
  98. {[]NullString{{"foo", true}, {}}, listProto(stringProto("foo"), nullProto()), listType(stringType())},
  99. // bytes
  100. {[]byte{}, bytesProto([]byte{}), bytesType()},
  101. {[]byte{1, 2, 3}, bytesProto([]byte{1, 2, 3}), bytesType()},
  102. {[]byte(nil), nullProto(), bytesType()},
  103. {[][]byte(nil), nullProto(), listType(bytesType())},
  104. {[][]byte{}, listProto(), listType(bytesType())},
  105. {[][]byte{{1}, []byte(nil)}, listProto(bytesProto([]byte{1}), nullProto()), listType(bytesType())},
  106. // date
  107. {d1, dateProto(d1), dateType()},
  108. {NullDate{civil.Date{}, false}, nullProto(), dateType()},
  109. {[]civil.Date(nil), nullProto(), listType(dateType())},
  110. {[]civil.Date{}, listProto(), listType(dateType())},
  111. {[]civil.Date{d1, d2, d3}, listProto(dateProto(d1), dateProto(d2), dateProto(d3)), listType(dateType())},
  112. {[]NullDate{{d2, true}, {}}, listProto(dateProto(d2), nullProto()), listType(dateType())},
  113. // timestamp
  114. {t1, timeProto(t1), timeType()},
  115. {NullTime{}, nullProto(), timeType()},
  116. {[]time.Time(nil), nullProto(), listType(timeType())},
  117. {[]time.Time{}, listProto(), listType(timeType())},
  118. {[]time.Time{t1, t2, t3}, listProto(timeProto(t1), timeProto(t2), timeProto(t3)), listType(timeType())},
  119. {[]NullTime{{t2, true}, {}}, listProto(timeProto(t2), nullProto()), listType(timeType())},
  120. // Struct
  121. {
  122. s1,
  123. listProto(intProto(10)),
  124. structType(mkField("field", intType())),
  125. },
  126. {
  127. (*struct {
  128. F1 civil.Date `spanner:""`
  129. F2 bool
  130. })(nil),
  131. nullProto(),
  132. structType(
  133. mkField("", dateType()),
  134. mkField("F2", boolType())),
  135. },
  136. // Array-of-struct
  137. {
  138. []staticStruct{s1, s2},
  139. listProto(listProto(intProto(10)), listProto(intProto(20))),
  140. listType(structType(mkField("field", intType()))),
  141. },
  142. } {
  143. st.Params["var"] = test.val
  144. gotParams, gotParamTypes, gotErr := st.convertParams()
  145. if gotErr != nil {
  146. t.Error(gotErr)
  147. continue
  148. }
  149. gotParamField := gotParams.Fields["var"]
  150. if !proto.Equal(gotParamField, test.wantField) {
  151. // handle NaN
  152. if test.wantType.Code == floatType().Code && proto.MarshalTextString(gotParamField) == proto.MarshalTextString(test.wantField) {
  153. continue
  154. }
  155. t.Errorf("%#v: got %v, want %v\n", test.val, gotParamField, test.wantField)
  156. }
  157. gotParamType := gotParamTypes["var"]
  158. if !proto.Equal(gotParamType, test.wantType) {
  159. t.Errorf("%#v: got %v, want %v\n", test.val, gotParamType, test.wantField)
  160. }
  161. }
  162. // Verify type error reporting.
  163. for _, test := range []struct {
  164. val interface{}
  165. wantErr error
  166. }{
  167. {
  168. nil,
  169. errBindParam("var", nil, errNilParam),
  170. },
  171. } {
  172. st.Params["var"] = test.val
  173. _, _, gotErr := st.convertParams()
  174. if !testEqual(gotErr, test.wantErr) {
  175. t.Errorf("value %#v:\ngot: %v\nwant: %v", test.val, gotErr, test.wantErr)
  176. }
  177. }
  178. }
  179. func TestNewStatement(t *testing.T) {
  180. s := NewStatement("query")
  181. if got, want := s.SQL, "query"; got != want {
  182. t.Errorf("got %q, want %q", got, want)
  183. }
  184. }