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.
 
 
 

144 lines
3.7 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/pretty"
  18. "cloud.google.com/go/internal/testutil"
  19. )
  20. func TestExternalDataConfig(t *testing.T) {
  21. // Round-trip of ExternalDataConfig to underlying representation.
  22. for i, want := range []*ExternalDataConfig{
  23. {
  24. SourceFormat: CSV,
  25. SourceURIs: []string{"uri"},
  26. Schema: Schema{{Name: "n", Type: IntegerFieldType}},
  27. AutoDetect: true,
  28. Compression: Gzip,
  29. IgnoreUnknownValues: true,
  30. MaxBadRecords: 17,
  31. Options: &CSVOptions{
  32. AllowJaggedRows: true,
  33. AllowQuotedNewlines: true,
  34. Encoding: UTF_8,
  35. FieldDelimiter: "f",
  36. Quote: "q",
  37. SkipLeadingRows: 3,
  38. },
  39. },
  40. {
  41. SourceFormat: GoogleSheets,
  42. Options: &GoogleSheetsOptions{SkipLeadingRows: 4},
  43. },
  44. {
  45. SourceFormat: Bigtable,
  46. Options: &BigtableOptions{
  47. IgnoreUnspecifiedColumnFamilies: true,
  48. ReadRowkeyAsString: true,
  49. ColumnFamilies: []*BigtableColumnFamily{
  50. {
  51. FamilyID: "f1",
  52. Encoding: "TEXT",
  53. OnlyReadLatest: true,
  54. Type: "FLOAT",
  55. Columns: []*BigtableColumn{
  56. {
  57. Qualifier: "valid-utf-8",
  58. FieldName: "fn",
  59. OnlyReadLatest: true,
  60. Encoding: "BINARY",
  61. Type: "STRING",
  62. },
  63. },
  64. },
  65. },
  66. },
  67. },
  68. } {
  69. q := want.toBQ()
  70. got, err := bqToExternalDataConfig(&q)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if diff := testutil.Diff(got, want); diff != "" {
  75. t.Errorf("#%d: got=-, want=+:\n%s", i, diff)
  76. }
  77. }
  78. }
  79. func TestQuote(t *testing.T) {
  80. ptr := func(s string) *string { return &s }
  81. for _, test := range []struct {
  82. quote string
  83. force bool
  84. want *string
  85. }{
  86. {"", false, nil},
  87. {"", true, ptr("")},
  88. {"-", false, ptr("-")},
  89. {"-", true, ptr("")},
  90. } {
  91. o := CSVOptions{
  92. Quote: test.quote,
  93. ForceZeroQuote: test.force,
  94. }
  95. got := o.quote()
  96. if (got == nil) != (test.want == nil) {
  97. t.Errorf("%+v\ngot %v\nwant %v", test, pretty.Value(got), pretty.Value(test.want))
  98. }
  99. if got != nil && test.want != nil && *got != *test.want {
  100. t.Errorf("%+v: got %q, want %q", test, *got, *test.want)
  101. }
  102. }
  103. }
  104. func TestQualifier(t *testing.T) {
  105. b := BigtableColumn{Qualifier: "a"}
  106. q := b.toBQ()
  107. if q.QualifierString != b.Qualifier || q.QualifierEncoded != "" {
  108. t.Errorf("got (%q, %q), want (%q, %q)",
  109. q.QualifierString, q.QualifierEncoded, b.Qualifier, "")
  110. }
  111. b2, err := bqToBigtableColumn(q)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. if got, want := b2.Qualifier, b.Qualifier; got != want {
  116. t.Errorf("got %q, want %q", got, want)
  117. }
  118. const (
  119. invalidUTF8 = "\xDF\xFF"
  120. invalidEncoded = "3/8"
  121. )
  122. b = BigtableColumn{Qualifier: invalidUTF8}
  123. q = b.toBQ()
  124. if q.QualifierString != "" || q.QualifierEncoded != invalidEncoded {
  125. t.Errorf("got (%q, %q), want (%q, %q)",
  126. q.QualifierString, "", b.Qualifier, invalidEncoded)
  127. }
  128. b2, err = bqToBigtableColumn(q)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if got, want := b2.Qualifier, b.Qualifier; got != want {
  133. t.Errorf("got %q, want %q", got, want)
  134. }
  135. }