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.
 
 
 

364 lines
9.2 KiB

  1. // Copyright 2016 Google LLC
  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 bigquery
  15. import (
  16. "errors"
  17. "math"
  18. "math/big"
  19. "reflect"
  20. "testing"
  21. "time"
  22. "github.com/google/go-cmp/cmp"
  23. "cloud.google.com/go/civil"
  24. "cloud.google.com/go/internal/testutil"
  25. "golang.org/x/net/context"
  26. bq "google.golang.org/api/bigquery/v2"
  27. )
  28. var scalarTests = []struct {
  29. val interface{} // The Go value
  30. wantVal string // paramValue's desired output
  31. wantType *bq.QueryParameterType // paramType's desired output
  32. }{
  33. {int64(0), "0", int64ParamType},
  34. {3.14, "3.14", float64ParamType},
  35. {3.14159e-87, "3.14159e-87", float64ParamType},
  36. {true, "true", boolParamType},
  37. {"string", "string", stringParamType},
  38. {"\u65e5\u672c\u8a9e\n", "\u65e5\u672c\u8a9e\n", stringParamType},
  39. {math.NaN(), "NaN", float64ParamType},
  40. {[]byte("foo"), "Zm9v", bytesParamType}, // base64 encoding of "foo"
  41. {time.Date(2016, 3, 20, 4, 22, 9, 5000, time.FixedZone("neg1-2", -3720)),
  42. "2016-03-20 04:22:09.000005-01:02",
  43. timestampParamType},
  44. {civil.Date{Year: 2016, Month: 3, Day: 20}, "2016-03-20", dateParamType},
  45. {civil.Time{Hour: 4, Minute: 5, Second: 6, Nanosecond: 789000000}, "04:05:06.789000", timeParamType},
  46. {civil.DateTime{Date: civil.Date{Year: 2016, Month: 3, Day: 20}, Time: civil.Time{Hour: 4, Minute: 5, Second: 6, Nanosecond: 789000000}},
  47. "2016-03-20 04:05:06.789000",
  48. dateTimeParamType},
  49. {big.NewRat(12345, 1000), "12.345000000", numericParamType},
  50. }
  51. type (
  52. S1 struct {
  53. A int
  54. B *S2
  55. C bool
  56. }
  57. S2 struct {
  58. D string
  59. e int
  60. }
  61. )
  62. var (
  63. s1 = S1{
  64. A: 1,
  65. B: &S2{D: "s"},
  66. C: true,
  67. }
  68. s1ParamType = &bq.QueryParameterType{
  69. Type: "STRUCT",
  70. StructTypes: []*bq.QueryParameterTypeStructTypes{
  71. {Name: "A", Type: int64ParamType},
  72. {Name: "B", Type: &bq.QueryParameterType{
  73. Type: "STRUCT",
  74. StructTypes: []*bq.QueryParameterTypeStructTypes{
  75. {Name: "D", Type: stringParamType},
  76. },
  77. }},
  78. {Name: "C", Type: boolParamType},
  79. },
  80. }
  81. s1ParamValue = bq.QueryParameterValue{
  82. StructValues: map[string]bq.QueryParameterValue{
  83. "A": sval("1"),
  84. "B": {
  85. StructValues: map[string]bq.QueryParameterValue{
  86. "D": sval("s"),
  87. },
  88. },
  89. "C": sval("true"),
  90. },
  91. }
  92. s1ParamReturnValue = map[string]interface{}{
  93. "A": int64(1),
  94. "B": map[string]interface{}{"D": "s"},
  95. "C": true,
  96. }
  97. )
  98. func sval(s string) bq.QueryParameterValue {
  99. return bq.QueryParameterValue{Value: s}
  100. }
  101. func TestParamValueScalar(t *testing.T) {
  102. for _, test := range scalarTests {
  103. got, err := paramValue(reflect.ValueOf(test.val))
  104. if err != nil {
  105. t.Errorf("%v: got %v, want nil", test.val, err)
  106. continue
  107. }
  108. want := sval(test.wantVal)
  109. if !testutil.Equal(got, want) {
  110. t.Errorf("%v:\ngot %+v\nwant %+v", test.val, got, want)
  111. }
  112. }
  113. }
  114. func TestParamValueArray(t *testing.T) {
  115. qpv := bq.QueryParameterValue{ArrayValues: []*bq.QueryParameterValue{
  116. {Value: "1"},
  117. {Value: "2"},
  118. },
  119. }
  120. for _, test := range []struct {
  121. val interface{}
  122. want bq.QueryParameterValue
  123. }{
  124. {[]int(nil), bq.QueryParameterValue{}},
  125. {[]int{}, bq.QueryParameterValue{}},
  126. {[]int{1, 2}, qpv},
  127. {[2]int{1, 2}, qpv},
  128. } {
  129. got, err := paramValue(reflect.ValueOf(test.val))
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. if !testutil.Equal(got, test.want) {
  134. t.Errorf("%#v:\ngot %+v\nwant %+v", test.val, got, test.want)
  135. }
  136. }
  137. }
  138. func TestParamValueStruct(t *testing.T) {
  139. got, err := paramValue(reflect.ValueOf(s1))
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. if !testutil.Equal(got, s1ParamValue) {
  144. t.Errorf("got %+v\nwant %+v", got, s1ParamValue)
  145. }
  146. }
  147. func TestParamValueErrors(t *testing.T) {
  148. // paramValue lets a few invalid types through, but paramType catches them.
  149. // Since we never call one without the other that's fine.
  150. for _, val := range []interface{}{nil, new([]int)} {
  151. _, err := paramValue(reflect.ValueOf(val))
  152. if err == nil {
  153. t.Errorf("%v (%T): got nil, want error", val, val)
  154. }
  155. }
  156. }
  157. func TestParamType(t *testing.T) {
  158. for _, test := range scalarTests {
  159. got, err := paramType(reflect.TypeOf(test.val))
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. if !testutil.Equal(got, test.wantType) {
  164. t.Errorf("%v (%T): got %v, want %v", test.val, test.val, got, test.wantType)
  165. }
  166. }
  167. for _, test := range []struct {
  168. val interface{}
  169. want *bq.QueryParameterType
  170. }{
  171. {uint32(32767), int64ParamType},
  172. {[]byte("foo"), bytesParamType},
  173. {[]int{}, &bq.QueryParameterType{Type: "ARRAY", ArrayType: int64ParamType}},
  174. {[3]bool{}, &bq.QueryParameterType{Type: "ARRAY", ArrayType: boolParamType}},
  175. {S1{}, s1ParamType},
  176. } {
  177. got, err := paramType(reflect.TypeOf(test.val))
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. if !testutil.Equal(got, test.want) {
  182. t.Errorf("%v (%T): got %v, want %v", test.val, test.val, got, test.want)
  183. }
  184. }
  185. }
  186. func TestParamTypeErrors(t *testing.T) {
  187. for _, val := range []interface{}{
  188. nil, uint(0), new([]int), make(chan int),
  189. } {
  190. _, err := paramType(reflect.TypeOf(val))
  191. if err == nil {
  192. t.Errorf("%v (%T): got nil, want error", val, val)
  193. }
  194. }
  195. }
  196. func TestConvertParamValue(t *testing.T) {
  197. // Scalars.
  198. for _, test := range scalarTests {
  199. pval, err := paramValue(reflect.ValueOf(test.val))
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. ptype, err := paramType(reflect.TypeOf(test.val))
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. got, err := convertParamValue(&pval, ptype)
  208. if err != nil {
  209. t.Fatalf("convertParamValue(%+v, %+v): %v", pval, ptype, err)
  210. }
  211. if !testutil.Equal(got, test.val) {
  212. t.Errorf("%#v: got %#v", test.val, got)
  213. }
  214. }
  215. // Arrays.
  216. for _, test := range []struct {
  217. pval *bq.QueryParameterValue
  218. want []interface{}
  219. }{
  220. {
  221. &bq.QueryParameterValue{},
  222. nil,
  223. },
  224. {
  225. &bq.QueryParameterValue{
  226. ArrayValues: []*bq.QueryParameterValue{{Value: "1"}, {Value: "2"}},
  227. },
  228. []interface{}{int64(1), int64(2)},
  229. },
  230. } {
  231. ptype := &bq.QueryParameterType{Type: "ARRAY", ArrayType: int64ParamType}
  232. got, err := convertParamValue(test.pval, ptype)
  233. if err != nil {
  234. t.Fatalf("%+v: %v", test.pval, err)
  235. }
  236. if !testutil.Equal(got, test.want) {
  237. t.Errorf("%+v: got %+v, want %+v", test.pval, got, test.want)
  238. }
  239. }
  240. // Structs.
  241. got, err := convertParamValue(&s1ParamValue, s1ParamType)
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. if !testutil.Equal(got, s1ParamReturnValue) {
  246. t.Errorf("got %+v, want %+v", got, s1ParamReturnValue)
  247. }
  248. }
  249. func TestIntegration_ScalarParam(t *testing.T) {
  250. roundToMicros := cmp.Transformer("RoundToMicros",
  251. func(t time.Time) time.Time { return t.Round(time.Microsecond) })
  252. c := getClient(t)
  253. for _, test := range scalarTests {
  254. gotData, gotParam, err := paramRoundTrip(c, test.val)
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. if !testutil.Equal(gotData, test.val, roundToMicros) {
  259. t.Errorf("\ngot %#v (%T)\nwant %#v (%T)", gotData, gotData, test.val, test.val)
  260. }
  261. if !testutil.Equal(gotParam, test.val, roundToMicros) {
  262. t.Errorf("\ngot %#v (%T)\nwant %#v (%T)", gotParam, gotParam, test.val, test.val)
  263. }
  264. }
  265. }
  266. func TestIntegration_OtherParam(t *testing.T) {
  267. c := getClient(t)
  268. for _, test := range []struct {
  269. val interface{}
  270. wantData interface{}
  271. wantParam interface{}
  272. }{
  273. {[]int(nil), []Value(nil), []interface{}(nil)},
  274. {[]int{}, []Value(nil), []interface{}(nil)},
  275. {
  276. []int{1, 2},
  277. []Value{int64(1), int64(2)},
  278. []interface{}{int64(1), int64(2)},
  279. },
  280. {
  281. [3]int{1, 2, 3},
  282. []Value{int64(1), int64(2), int64(3)},
  283. []interface{}{int64(1), int64(2), int64(3)},
  284. },
  285. {
  286. S1{},
  287. []Value{int64(0), nil, false},
  288. map[string]interface{}{
  289. "A": int64(0),
  290. "B": nil,
  291. "C": false,
  292. },
  293. },
  294. {
  295. s1,
  296. []Value{int64(1), []Value{"s"}, true},
  297. s1ParamReturnValue,
  298. },
  299. } {
  300. gotData, gotParam, err := paramRoundTrip(c, test.val)
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. if !testutil.Equal(gotData, test.wantData) {
  305. t.Errorf("%#v:\ngot %#v (%T)\nwant %#v (%T)",
  306. test.val, gotData, gotData, test.wantData, test.wantData)
  307. }
  308. if !testutil.Equal(gotParam, test.wantParam) {
  309. t.Errorf("%#v:\ngot %#v (%T)\nwant %#v (%T)",
  310. test.val, gotParam, gotParam, test.wantParam, test.wantParam)
  311. }
  312. }
  313. }
  314. // paramRoundTrip passes x as a query parameter to BigQuery. It returns
  315. // the resulting data value from running the query and the parameter value from
  316. // the returned job configuration.
  317. func paramRoundTrip(c *Client, x interface{}) (data Value, param interface{}, err error) {
  318. ctx := context.Background()
  319. q := c.Query("select ?")
  320. q.Parameters = []QueryParameter{{Value: x}}
  321. job, err := q.Run(ctx)
  322. if err != nil {
  323. return nil, nil, err
  324. }
  325. it, err := job.Read(ctx)
  326. if err != nil {
  327. return nil, nil, err
  328. }
  329. var val []Value
  330. err = it.Next(&val)
  331. if err != nil {
  332. return nil, nil, err
  333. }
  334. if len(val) != 1 {
  335. return nil, nil, errors.New("wrong number of values")
  336. }
  337. conf, err := job.Config()
  338. if err != nil {
  339. return nil, nil, err
  340. }
  341. return val[0], conf.(*QueryConfig).Parameters[0].Value, nil
  342. }