dataset_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "errors"
  17. "reflect"
  18. "testing"
  19. "golang.org/x/net/context"
  20. )
  21. // readServiceStub services read requests by returning data from an in-memory list of values.
  22. type listTablesServiceStub struct {
  23. expectedProject, expectedDataset string
  24. values [][]*Table // contains pages of tables.
  25. pageTokens map[string]string // maps incoming page token to returned page token.
  26. service
  27. }
  28. func (s *listTablesServiceStub) listTables(ctx context.Context, projectID, datasetID, pageToken string) ([]*Table, string, error) {
  29. if projectID != s.expectedProject {
  30. return nil, "", errors.New("wrong project id")
  31. }
  32. if datasetID != s.expectedDataset {
  33. return nil, "", errors.New("wrong dataset id")
  34. }
  35. tables := s.values[0]
  36. s.values = s.values[1:]
  37. return tables, s.pageTokens[pageToken], nil
  38. }
  39. func TestListTables(t *testing.T) {
  40. t1 := &Table{ProjectID: "p1", DatasetID: "d1", TableID: "t1"}
  41. t2 := &Table{ProjectID: "p1", DatasetID: "d1", TableID: "t2"}
  42. t3 := &Table{ProjectID: "p1", DatasetID: "d1", TableID: "t3"}
  43. testCases := []struct {
  44. data [][]*Table
  45. pageTokens map[string]string
  46. want []*Table
  47. }{
  48. {
  49. data: [][]*Table{{t1, t2}, {t3}},
  50. pageTokens: map[string]string{"": "a", "a": ""},
  51. want: []*Table{t1, t2, t3},
  52. },
  53. {
  54. data: [][]*Table{{t1, t2}, {t3}},
  55. pageTokens: map[string]string{"": ""}, // no more pages after first one.
  56. want: []*Table{t1, t2},
  57. },
  58. }
  59. for _, tc := range testCases {
  60. c := &Client{
  61. service: &listTablesServiceStub{
  62. expectedProject: "x",
  63. expectedDataset: "y",
  64. values: tc.data,
  65. pageTokens: tc.pageTokens,
  66. },
  67. projectID: "x",
  68. }
  69. got, err := c.Dataset("y").ListTables(context.Background())
  70. if err != nil {
  71. t.Errorf("err calling ListTables: %v", err)
  72. continue
  73. }
  74. if !reflect.DeepEqual(got, tc.want) {
  75. t.Errorf("reading: got:\n%v\nwant:\n%v", got, tc.want)
  76. }
  77. }
  78. }
  79. func TestListTablesError(t *testing.T) {
  80. c := &Client{
  81. service: &listTablesServiceStub{
  82. expectedProject: "x",
  83. expectedDataset: "y",
  84. },
  85. projectID: "x",
  86. }
  87. // Test that service read errors are propagated back to the caller.
  88. // Passing "not y" as the dataset id will cause the service to return an error.
  89. _, err := c.Dataset("not y").ListTables(context.Background())
  90. if err == nil {
  91. // Read should not return an error; only Err should.
  92. t.Errorf("ListTables expected: non-nil err, got: nil")
  93. }
  94. }