copy_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 defaultCopyJob() *bq.Job {
  22. return &bq.Job{
  23. Configuration: &bq.JobConfiguration{
  24. Copy: &bq.JobConfigurationTableCopy{
  25. DestinationTable: &bq.TableReference{
  26. ProjectId: "d-project-id",
  27. DatasetId: "d-dataset-id",
  28. TableId: "d-table-id",
  29. },
  30. SourceTables: []*bq.TableReference{
  31. {
  32. ProjectId: "s-project-id",
  33. DatasetId: "s-dataset-id",
  34. TableId: "s-table-id",
  35. },
  36. },
  37. },
  38. },
  39. }
  40. }
  41. func TestCopy(t *testing.T) {
  42. testCases := []struct {
  43. dst *Table
  44. src Tables
  45. options []Option
  46. want *bq.Job
  47. }{
  48. {
  49. dst: &Table{
  50. ProjectID: "d-project-id",
  51. DatasetID: "d-dataset-id",
  52. TableID: "d-table-id",
  53. },
  54. src: Tables{
  55. {
  56. ProjectID: "s-project-id",
  57. DatasetID: "s-dataset-id",
  58. TableID: "s-table-id",
  59. },
  60. },
  61. want: defaultCopyJob(),
  62. },
  63. {
  64. dst: &Table{
  65. ProjectID: "d-project-id",
  66. DatasetID: "d-dataset-id",
  67. TableID: "d-table-id",
  68. },
  69. src: Tables{
  70. {
  71. ProjectID: "s-project-id",
  72. DatasetID: "s-dataset-id",
  73. TableID: "s-table-id",
  74. },
  75. },
  76. options: []Option{CreateNever, WriteTruncate},
  77. want: func() *bq.Job {
  78. j := defaultCopyJob()
  79. j.Configuration.Copy.CreateDisposition = "CREATE_NEVER"
  80. j.Configuration.Copy.WriteDisposition = "WRITE_TRUNCATE"
  81. return j
  82. }(),
  83. },
  84. }
  85. for _, tc := range testCases {
  86. s := &testService{}
  87. c := &Client{
  88. service: s,
  89. }
  90. if _, err := c.Copy(context.Background(), tc.dst, tc.src, tc.options...); err != nil {
  91. t.Errorf("err calling cp: %v", err)
  92. continue
  93. }
  94. if !reflect.DeepEqual(s.Job, tc.want) {
  95. t.Errorf("copying: got:\n%v\nwant:\n%v", s.Job, tc.want)
  96. }
  97. }
  98. }