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.
 
 
 

234 lines
6.5 KiB

  1. // Copyright 2015 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. "context"
  17. "errors"
  18. "testing"
  19. "cloud.google.com/go/internal/testutil"
  20. "github.com/google/go-cmp/cmp"
  21. bq "google.golang.org/api/bigquery/v2"
  22. "google.golang.org/api/iterator"
  23. )
  24. type pageFetcherArgs struct {
  25. table *Table
  26. schema Schema
  27. startIndex uint64
  28. pageSize int64
  29. pageToken string
  30. }
  31. // pageFetcherReadStub services read requests by returning data from an in-memory list of values.
  32. type pageFetcherReadStub struct {
  33. // values and pageTokens are used as sources of data to return in response to calls to readTabledata or readQuery.
  34. values [][][]Value // contains pages / rows / columns.
  35. pageTokens map[string]string // maps incoming page token to returned page token.
  36. // arguments are recorded for later inspection.
  37. calls []pageFetcherArgs
  38. }
  39. func (s *pageFetcherReadStub) fetchPage(ctx context.Context, t *Table, schema Schema, startIndex uint64, pageSize int64, pageToken string) (*fetchPageResult, error) {
  40. s.calls = append(s.calls,
  41. pageFetcherArgs{t, schema, startIndex, pageSize, pageToken})
  42. result := &fetchPageResult{
  43. pageToken: s.pageTokens[pageToken],
  44. rows: s.values[0],
  45. }
  46. s.values = s.values[1:]
  47. return result, nil
  48. }
  49. func waitForQueryStub(context.Context, string) (Schema, uint64, error) {
  50. return nil, 1, nil
  51. }
  52. func TestRead(t *testing.T) {
  53. // The data for the service stub to return is populated for each test case in the testCases for loop.
  54. ctx := context.Background()
  55. c := &Client{projectID: "project-id"}
  56. pf := &pageFetcherReadStub{}
  57. queryJob := &Job{
  58. projectID: "project-id",
  59. jobID: "job-id",
  60. c: c,
  61. config: &bq.JobConfiguration{
  62. Query: &bq.JobConfigurationQuery{
  63. DestinationTable: &bq.TableReference{
  64. ProjectId: "project-id",
  65. DatasetId: "dataset-id",
  66. TableId: "table-id",
  67. },
  68. },
  69. },
  70. }
  71. for _, readFunc := range []func() *RowIterator{
  72. func() *RowIterator {
  73. return c.Dataset("dataset-id").Table("table-id").read(ctx, pf.fetchPage)
  74. },
  75. func() *RowIterator {
  76. it, err := queryJob.read(ctx, waitForQueryStub, pf.fetchPage)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. return it
  81. },
  82. } {
  83. testCases := []struct {
  84. data [][][]Value
  85. pageTokens map[string]string
  86. want [][]Value
  87. }{
  88. {
  89. data: [][][]Value{{{1, 2}, {11, 12}}, {{30, 40}, {31, 41}}},
  90. pageTokens: map[string]string{"": "a", "a": ""},
  91. want: [][]Value{{1, 2}, {11, 12}, {30, 40}, {31, 41}},
  92. },
  93. {
  94. data: [][][]Value{{{1, 2}, {11, 12}}, {{30, 40}, {31, 41}}},
  95. pageTokens: map[string]string{"": ""}, // no more pages after first one.
  96. want: [][]Value{{1, 2}, {11, 12}},
  97. },
  98. }
  99. for _, tc := range testCases {
  100. pf.values = tc.data
  101. pf.pageTokens = tc.pageTokens
  102. if got, ok := collectValues(t, readFunc()); ok {
  103. if !testutil.Equal(got, tc.want) {
  104. t.Errorf("reading: got:\n%v\nwant:\n%v", got, tc.want)
  105. }
  106. }
  107. }
  108. }
  109. }
  110. func collectValues(t *testing.T, it *RowIterator) ([][]Value, bool) {
  111. var got [][]Value
  112. for {
  113. var vals []Value
  114. err := it.Next(&vals)
  115. if err == iterator.Done {
  116. break
  117. }
  118. if err != nil {
  119. t.Errorf("err calling Next: %v", err)
  120. return nil, false
  121. }
  122. got = append(got, vals)
  123. }
  124. return got, true
  125. }
  126. func TestNoMoreValues(t *testing.T) {
  127. c := &Client{projectID: "project-id"}
  128. pf := &pageFetcherReadStub{
  129. values: [][][]Value{{{1, 2}, {11, 12}}},
  130. }
  131. it := c.Dataset("dataset-id").Table("table-id").read(context.Background(), pf.fetchPage)
  132. var vals []Value
  133. // We expect to retrieve two values and then fail on the next attempt.
  134. if err := it.Next(&vals); err != nil {
  135. t.Fatalf("Next: got: %v: want: nil", err)
  136. }
  137. if err := it.Next(&vals); err != nil {
  138. t.Fatalf("Next: got: %v: want: nil", err)
  139. }
  140. if err := it.Next(&vals); err != iterator.Done {
  141. t.Fatalf("Next: got: %v: want: iterator.Done", err)
  142. }
  143. }
  144. var errBang = errors.New("bang")
  145. func errorFetchPage(context.Context, *Table, Schema, uint64, int64, string) (*fetchPageResult, error) {
  146. return nil, errBang
  147. }
  148. func TestReadError(t *testing.T) {
  149. // test that service read errors are propagated back to the caller.
  150. c := &Client{projectID: "project-id"}
  151. it := c.Dataset("dataset-id").Table("table-id").read(context.Background(), errorFetchPage)
  152. var vals []Value
  153. if err := it.Next(&vals); err != errBang {
  154. t.Fatalf("Get: got: %v: want: %v", err, errBang)
  155. }
  156. }
  157. func TestReadTabledataOptions(t *testing.T) {
  158. // test that read options are propagated.
  159. s := &pageFetcherReadStub{
  160. values: [][][]Value{{{1, 2}}},
  161. }
  162. c := &Client{projectID: "project-id"}
  163. tr := c.Dataset("dataset-id").Table("table-id")
  164. it := tr.read(context.Background(), s.fetchPage)
  165. it.PageInfo().MaxSize = 5
  166. var vals []Value
  167. if err := it.Next(&vals); err != nil {
  168. t.Fatal(err)
  169. }
  170. want := []pageFetcherArgs{{
  171. table: tr,
  172. pageSize: 5,
  173. pageToken: "",
  174. }}
  175. if diff := testutil.Diff(s.calls, want, cmp.AllowUnexported(pageFetcherArgs{}, pageFetcherReadStub{}, Table{}, Client{})); diff != "" {
  176. t.Errorf("reading (got=-, want=+):\n%s", diff)
  177. }
  178. }
  179. func TestReadQueryOptions(t *testing.T) {
  180. // test that read options are propagated.
  181. c := &Client{projectID: "project-id"}
  182. pf := &pageFetcherReadStub{
  183. values: [][][]Value{{{1, 2}}},
  184. }
  185. tr := &bq.TableReference{
  186. ProjectId: "project-id",
  187. DatasetId: "dataset-id",
  188. TableId: "table-id",
  189. }
  190. queryJob := &Job{
  191. projectID: "project-id",
  192. jobID: "job-id",
  193. c: c,
  194. config: &bq.JobConfiguration{
  195. Query: &bq.JobConfigurationQuery{DestinationTable: tr},
  196. },
  197. }
  198. it, err := queryJob.read(context.Background(), waitForQueryStub, pf.fetchPage)
  199. if err != nil {
  200. t.Fatalf("err calling Read: %v", err)
  201. }
  202. it.PageInfo().MaxSize = 5
  203. var vals []Value
  204. if err := it.Next(&vals); err != nil {
  205. t.Fatalf("Next: got: %v: want: nil", err)
  206. }
  207. want := []pageFetcherArgs{{
  208. table: bqToTable(tr, c),
  209. pageSize: 5,
  210. pageToken: "",
  211. }}
  212. if !testutil.Equal(pf.calls, want, cmp.AllowUnexported(pageFetcherArgs{}, Table{}, Client{})) {
  213. t.Errorf("reading: got:\n%v\nwant:\n%v", pf.calls, want)
  214. }
  215. }