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.
 
 
 

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