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.
 
 
 

96 lines
2.4 KiB

  1. // Copyright 2017 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. "testing"
  17. "cloud.google.com/go/internal/testutil"
  18. bq "google.golang.org/api/bigquery/v2"
  19. )
  20. func TestCreateJobRef(t *testing.T) {
  21. defer fixRandomID("RANDOM")()
  22. cNoLoc := &Client{projectID: "projectID"}
  23. cLoc := &Client{projectID: "projectID", Location: "defaultLoc"}
  24. for _, test := range []struct {
  25. in JobIDConfig
  26. client *Client
  27. want *bq.JobReference
  28. }{
  29. {
  30. in: JobIDConfig{JobID: "foo"},
  31. want: &bq.JobReference{JobId: "foo"},
  32. },
  33. {
  34. in: JobIDConfig{},
  35. want: &bq.JobReference{JobId: "RANDOM"},
  36. },
  37. {
  38. in: JobIDConfig{AddJobIDSuffix: true},
  39. want: &bq.JobReference{JobId: "RANDOM"},
  40. },
  41. {
  42. in: JobIDConfig{JobID: "foo", AddJobIDSuffix: true},
  43. want: &bq.JobReference{JobId: "foo-RANDOM"},
  44. },
  45. {
  46. in: JobIDConfig{JobID: "foo", Location: "loc"},
  47. want: &bq.JobReference{JobId: "foo", Location: "loc"},
  48. },
  49. {
  50. in: JobIDConfig{JobID: "foo"},
  51. client: cLoc,
  52. want: &bq.JobReference{JobId: "foo", Location: "defaultLoc"},
  53. },
  54. {
  55. in: JobIDConfig{JobID: "foo", Location: "loc"},
  56. client: cLoc,
  57. want: &bq.JobReference{JobId: "foo", Location: "loc"},
  58. },
  59. } {
  60. client := test.client
  61. if client == nil {
  62. client = cNoLoc
  63. }
  64. got := test.in.createJobRef(client)
  65. test.want.ProjectId = "projectID"
  66. if !testutil.Equal(got, test.want) {
  67. t.Errorf("%+v: got %+v, want %+v", test.in, got, test.want)
  68. }
  69. }
  70. }
  71. func fixRandomID(s string) func() {
  72. prev := randomIDFn
  73. randomIDFn = func() string { return s }
  74. return func() { randomIDFn = prev }
  75. }
  76. func checkJob(t *testing.T, i int, got, want *bq.Job) {
  77. if got.JobReference == nil {
  78. t.Errorf("#%d: empty job reference", i)
  79. return
  80. }
  81. if got.JobReference.JobId == "" {
  82. t.Errorf("#%d: empty job ID", i)
  83. return
  84. }
  85. d := testutil.Diff(got, want)
  86. if d != "" {
  87. t.Errorf("#%d: (got=-, want=+) %s", i, d)
  88. }
  89. }