2017-05-11 22:39:54 +08:00
|
|
|
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bigquery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"cloud.google.com/go/internal/pretty"
|
2017-09-30 22:27:27 +08:00
|
|
|
"cloud.google.com/go/internal/testutil"
|
2017-05-11 22:39:54 +08:00
|
|
|
bq "google.golang.org/api/bigquery/v2"
|
|
|
|
)
|
|
|
|
|
2018-01-16 21:20:59 +08:00
|
|
|
var (
|
|
|
|
hyphen = "-"
|
|
|
|
fc = FileConfig{
|
2017-05-11 22:39:54 +08:00
|
|
|
SourceFormat: CSV,
|
2018-01-16 21:20:59 +08:00
|
|
|
AutoDetect: true,
|
2017-05-11 22:39:54 +08:00
|
|
|
MaxBadRecords: 7,
|
|
|
|
IgnoreUnknownValues: true,
|
|
|
|
Schema: Schema{
|
|
|
|
stringFieldSchema(),
|
|
|
|
nestedFieldSchema(),
|
|
|
|
},
|
2018-01-16 21:20:59 +08:00
|
|
|
CSVOptions: CSVOptions{
|
|
|
|
Quote: hyphen,
|
|
|
|
FieldDelimiter: "\t",
|
|
|
|
SkipLeadingRows: 8,
|
|
|
|
AllowJaggedRows: true,
|
|
|
|
AllowQuotedNewlines: true,
|
|
|
|
Encoding: UTF_8,
|
|
|
|
},
|
2017-05-11 22:39:54 +08:00
|
|
|
}
|
2018-01-16 21:20:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileConfigPopulateLoadConfig(t *testing.T) {
|
2017-05-11 22:39:54 +08:00
|
|
|
want := &bq.JobConfigurationLoad{
|
|
|
|
SourceFormat: "CSV",
|
|
|
|
FieldDelimiter: "\t",
|
|
|
|
SkipLeadingRows: 8,
|
|
|
|
AllowJaggedRows: true,
|
|
|
|
AllowQuotedNewlines: true,
|
2018-01-16 21:20:59 +08:00
|
|
|
Autodetect: true,
|
2017-05-11 22:39:54 +08:00
|
|
|
Encoding: "UTF-8",
|
|
|
|
MaxBadRecords: 7,
|
|
|
|
IgnoreUnknownValues: true,
|
|
|
|
Schema: &bq.TableSchema{
|
|
|
|
Fields: []*bq.TableFieldSchema{
|
|
|
|
bqStringFieldSchema(),
|
|
|
|
bqNestedFieldSchema(),
|
|
|
|
}},
|
|
|
|
Quote: &hyphen,
|
|
|
|
}
|
|
|
|
got := &bq.JobConfigurationLoad{}
|
|
|
|
fc.populateLoadConfig(got)
|
2017-09-30 22:27:27 +08:00
|
|
|
if !testutil.Equal(got, want) {
|
2017-05-11 22:39:54 +08:00
|
|
|
t.Errorf("got:\n%v\nwant:\n%v", pretty.Value(got), pretty.Value(want))
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 21:20:59 +08:00
|
|
|
|
|
|
|
func TestFileConfigPopulateExternalDataConfig(t *testing.T) {
|
|
|
|
got := &bq.ExternalDataConfiguration{}
|
|
|
|
fc.populateExternalDataConfig(got)
|
|
|
|
|
|
|
|
want := &bq.ExternalDataConfiguration{
|
|
|
|
SourceFormat: "CSV",
|
|
|
|
Autodetect: true,
|
|
|
|
MaxBadRecords: 7,
|
|
|
|
IgnoreUnknownValues: true,
|
|
|
|
Schema: &bq.TableSchema{
|
|
|
|
Fields: []*bq.TableFieldSchema{
|
|
|
|
bqStringFieldSchema(),
|
|
|
|
bqNestedFieldSchema(),
|
|
|
|
}},
|
|
|
|
CsvOptions: &bq.CsvOptions{
|
|
|
|
AllowJaggedRows: true,
|
|
|
|
AllowQuotedNewlines: true,
|
|
|
|
Encoding: "UTF-8",
|
|
|
|
FieldDelimiter: "\t",
|
|
|
|
Quote: &hyphen,
|
|
|
|
SkipLeadingRows: 8,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if diff := testutil.Diff(got, want); diff != "" {
|
|
|
|
t.Errorf("got=-, want=+:\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|