query_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  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. "reflect"
  17. "testing"
  18. "golang.org/x/net/context"
  19. bq "google.golang.org/api/bigquery/v2"
  20. )
  21. func defaultQueryJob() *bq.Job {
  22. return &bq.Job{
  23. Configuration: &bq.JobConfiguration{
  24. Query: &bq.JobConfigurationQuery{
  25. DestinationTable: &bq.TableReference{
  26. ProjectId: "project-id",
  27. DatasetId: "dataset-id",
  28. TableId: "table-id",
  29. },
  30. Query: "query string",
  31. DefaultDataset: &bq.DatasetReference{
  32. ProjectId: "def-project-id",
  33. DatasetId: "def-dataset-id",
  34. },
  35. },
  36. },
  37. }
  38. }
  39. func TestQuery(t *testing.T) {
  40. testCases := []struct {
  41. dst *Table
  42. src *Query
  43. options []Option
  44. want *bq.Job
  45. }{
  46. {
  47. dst: defaultTable,
  48. src: defaultQuery,
  49. want: defaultQueryJob(),
  50. },
  51. {
  52. dst: defaultTable,
  53. src: &Query{
  54. Q: "query string",
  55. },
  56. want: func() *bq.Job {
  57. j := defaultQueryJob()
  58. j.Configuration.Query.DefaultDataset = nil
  59. return j
  60. }(),
  61. },
  62. {
  63. dst: &Table{},
  64. src: defaultQuery,
  65. want: func() *bq.Job {
  66. j := defaultQueryJob()
  67. j.Configuration.Query.DestinationTable = nil
  68. return j
  69. }(),
  70. },
  71. {
  72. dst: &Table{
  73. ProjectID: "project-id",
  74. DatasetID: "dataset-id",
  75. TableID: "table-id",
  76. },
  77. src: defaultQuery,
  78. options: []Option{CreateNever, WriteTruncate},
  79. want: func() *bq.Job {
  80. j := defaultQueryJob()
  81. j.Configuration.Query.WriteDisposition = "WRITE_TRUNCATE"
  82. j.Configuration.Query.CreateDisposition = "CREATE_NEVER"
  83. return j
  84. }(),
  85. },
  86. {
  87. dst: defaultTable,
  88. src: defaultQuery,
  89. options: []Option{DisableQueryCache()},
  90. want: func() *bq.Job {
  91. j := defaultQueryJob()
  92. f := false
  93. j.Configuration.Query.UseQueryCache = &f
  94. return j
  95. }(),
  96. },
  97. }
  98. for _, tc := range testCases {
  99. s := &testService{}
  100. c := &Client{
  101. service: s,
  102. }
  103. if _, err := c.Copy(context.Background(), tc.dst, tc.src, tc.options...); err != nil {
  104. t.Errorf("err calling query: %v", err)
  105. continue
  106. }
  107. if !reflect.DeepEqual(s.Job, tc.want) {
  108. t.Errorf("querying: got:\n%v\nwant:\n%v", s.Job, tc.want)
  109. }
  110. }
  111. }