2018-01-16 14:20:59 +01:00
|
|
|
// Copyright 2017 Google Inc. All Rights Reserved.
|
2017-07-23 09:51:42 +02:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
"time"
|
|
|
|
|
2017-09-30 16:27:27 +02:00
|
|
|
"cloud.google.com/go/internal/testutil"
|
2017-07-23 09:51:42 +02:00
|
|
|
bq "google.golang.org/api/bigquery/v2"
|
|
|
|
)
|
|
|
|
|
2018-01-16 14:20:59 +01:00
|
|
|
func TestBQToTableMetadata(t *testing.T) {
|
2017-07-23 09:51:42 +02:00
|
|
|
aTime := time.Date(2017, 1, 26, 0, 0, 0, 0, time.Local)
|
|
|
|
aTimeMillis := aTime.UnixNano() / 1e6
|
|
|
|
for _, test := range []struct {
|
|
|
|
in *bq.Table
|
|
|
|
want *TableMetadata
|
|
|
|
}{
|
|
|
|
{&bq.Table{}, &TableMetadata{}}, // test minimal case
|
|
|
|
{
|
|
|
|
&bq.Table{
|
|
|
|
CreationTime: aTimeMillis,
|
|
|
|
Description: "desc",
|
|
|
|
Etag: "etag",
|
|
|
|
ExpirationTime: aTimeMillis,
|
|
|
|
FriendlyName: "fname",
|
|
|
|
Id: "id",
|
|
|
|
LastModifiedTime: uint64(aTimeMillis),
|
|
|
|
Location: "loc",
|
|
|
|
NumBytes: 123,
|
|
|
|
NumLongTermBytes: 23,
|
|
|
|
NumRows: 7,
|
|
|
|
StreamingBuffer: &bq.Streamingbuffer{
|
|
|
|
EstimatedBytes: 11,
|
|
|
|
EstimatedRows: 3,
|
|
|
|
OldestEntryTime: uint64(aTimeMillis),
|
|
|
|
},
|
|
|
|
TimePartitioning: &bq.TimePartitioning{
|
|
|
|
ExpirationMs: 7890,
|
|
|
|
Type: "DAY",
|
2018-01-16 14:20:59 +01:00
|
|
|
Field: "pfield",
|
|
|
|
},
|
2018-03-19 16:51:38 +01:00
|
|
|
EncryptionConfiguration: &bq.EncryptionConfiguration{KmsKeyName: "keyName"},
|
2018-01-16 14:20:59 +01:00
|
|
|
Type: "EXTERNAL",
|
|
|
|
View: &bq.ViewDefinition{Query: "view-query"},
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
ExternalDataConfiguration: &bq.ExternalDataConfiguration{
|
|
|
|
SourceFormat: "GOOGLE_SHEETS",
|
2017-07-23 09:51:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&TableMetadata{
|
2018-01-16 14:20:59 +01:00
|
|
|
Description: "desc",
|
|
|
|
Name: "fname",
|
|
|
|
ViewQuery: "view-query",
|
|
|
|
FullID: "id",
|
|
|
|
Type: ExternalTable,
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
ExternalDataConfig: &ExternalDataConfig{SourceFormat: GoogleSheets},
|
|
|
|
ExpirationTime: aTime.Truncate(time.Millisecond),
|
|
|
|
CreationTime: aTime.Truncate(time.Millisecond),
|
|
|
|
LastModifiedTime: aTime.Truncate(time.Millisecond),
|
|
|
|
NumBytes: 123,
|
|
|
|
NumRows: 7,
|
|
|
|
TimePartitioning: &TimePartitioning{
|
|
|
|
Expiration: 7890 * time.Millisecond,
|
|
|
|
Field: "pfield",
|
|
|
|
},
|
2017-07-23 09:51:42 +02:00
|
|
|
StreamingBuffer: &StreamingBuffer{
|
|
|
|
EstimatedBytes: 11,
|
|
|
|
EstimatedRows: 3,
|
|
|
|
OldestEntryTime: aTime,
|
|
|
|
},
|
2018-03-19 16:51:38 +01:00
|
|
|
EncryptionConfig: &EncryptionConfig{KMSKeyName: "keyName"},
|
|
|
|
ETag: "etag",
|
2017-07-23 09:51:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
2018-01-16 14:20:59 +01:00
|
|
|
got, err := bqToTableMetadata(test.in)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-30 16:27:27 +02:00
|
|
|
if diff := testutil.Diff(got, test.want); diff != "" {
|
|
|
|
t.Errorf("%+v:\n, -got, +want:\n%s", test.in, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 14:20:59 +01:00
|
|
|
func TestTableMetadataToBQ(t *testing.T) {
|
2017-09-30 16:27:27 +02:00
|
|
|
aTime := time.Date(2017, 1, 26, 0, 0, 0, 0, time.Local)
|
|
|
|
aTimeMillis := aTime.UnixNano() / 1e6
|
|
|
|
sc := Schema{fieldSchema("desc", "name", "STRING", false, true)}
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
in *TableMetadata
|
|
|
|
want *bq.Table
|
|
|
|
}{
|
|
|
|
{nil, &bq.Table{}},
|
|
|
|
{&TableMetadata{}, &bq.Table{}},
|
|
|
|
{
|
|
|
|
&TableMetadata{
|
2018-01-16 14:20:59 +01:00
|
|
|
Name: "n",
|
|
|
|
Description: "d",
|
|
|
|
Schema: sc,
|
|
|
|
ExpirationTime: aTime,
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
ExternalDataConfig: &ExternalDataConfig{SourceFormat: Bigtable},
|
2018-03-19 16:51:38 +01:00
|
|
|
EncryptionConfig: &EncryptionConfig{KMSKeyName: "keyName"},
|
2017-09-30 16:27:27 +02:00
|
|
|
},
|
|
|
|
&bq.Table{
|
|
|
|
FriendlyName: "n",
|
|
|
|
Description: "d",
|
|
|
|
Schema: &bq.TableSchema{
|
|
|
|
Fields: []*bq.TableFieldSchema{
|
|
|
|
bqTableFieldSchema("desc", "name", "STRING", "REQUIRED"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ExpirationTime: aTimeMillis,
|
2018-01-16 14:20:59 +01:00
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
ExternalDataConfiguration: &bq.ExternalDataConfiguration{SourceFormat: "BIGTABLE"},
|
2018-03-19 16:51:38 +01:00
|
|
|
EncryptionConfiguration: &bq.EncryptionConfiguration{KmsKeyName: "keyName"},
|
2017-09-30 16:27:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&TableMetadata{ViewQuery: "q"},
|
|
|
|
&bq.Table{
|
|
|
|
View: &bq.ViewDefinition{
|
|
|
|
Query: "q",
|
|
|
|
UseLegacySql: false,
|
|
|
|
ForceSendFields: []string{"UseLegacySql"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&TableMetadata{
|
|
|
|
ViewQuery: "q",
|
|
|
|
UseLegacySQL: true,
|
|
|
|
TimePartitioning: &TimePartitioning{},
|
|
|
|
},
|
|
|
|
&bq.Table{
|
|
|
|
View: &bq.ViewDefinition{
|
|
|
|
Query: "q",
|
|
|
|
UseLegacySql: true,
|
|
|
|
},
|
|
|
|
TimePartitioning: &bq.TimePartitioning{
|
|
|
|
Type: "DAY",
|
|
|
|
ExpirationMs: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&TableMetadata{
|
2018-01-16 14:20:59 +01:00
|
|
|
ViewQuery: "q",
|
|
|
|
UseStandardSQL: true,
|
|
|
|
TimePartitioning: &TimePartitioning{
|
|
|
|
Expiration: time.Second,
|
|
|
|
Field: "ofDreams",
|
|
|
|
},
|
2017-09-30 16:27:27 +02:00
|
|
|
},
|
|
|
|
&bq.Table{
|
|
|
|
View: &bq.ViewDefinition{
|
|
|
|
Query: "q",
|
|
|
|
UseLegacySql: false,
|
|
|
|
ForceSendFields: []string{"UseLegacySql"},
|
|
|
|
},
|
|
|
|
TimePartitioning: &bq.TimePartitioning{
|
|
|
|
Type: "DAY",
|
|
|
|
ExpirationMs: 1000,
|
2018-01-16 14:20:59 +01:00
|
|
|
Field: "ofDreams",
|
2017-09-30 16:27:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
2018-01-16 14:20:59 +01:00
|
|
|
got, err := test.in.toBQ()
|
2017-09-30 16:27:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v: %v", test.in, err)
|
|
|
|
}
|
|
|
|
if diff := testutil.Diff(got, test.want); diff != "" {
|
|
|
|
t.Errorf("%+v:\n-got, +want:\n%s", test.in, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
for _, in := range []*TableMetadata{
|
|
|
|
{Schema: sc, ViewQuery: "q"}, // can't have both schema and query
|
|
|
|
{UseLegacySQL: true}, // UseLegacySQL without query
|
|
|
|
{UseStandardSQL: true}, // UseStandardSQL without query
|
|
|
|
// read-only fields
|
|
|
|
{FullID: "x"},
|
|
|
|
{Type: "x"},
|
|
|
|
{CreationTime: aTime},
|
|
|
|
{LastModifiedTime: aTime},
|
|
|
|
{NumBytes: 1},
|
|
|
|
{NumRows: 1},
|
|
|
|
{StreamingBuffer: &StreamingBuffer{}},
|
|
|
|
{ETag: "x"},
|
|
|
|
} {
|
2018-01-16 14:20:59 +01:00
|
|
|
_, err := in.toBQ()
|
2017-09-30 16:27:27 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%+v: got nil, want error", in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 14:20:59 +01:00
|
|
|
func TestTableMetadataToUpdateToBQ(t *testing.T) {
|
|
|
|
aTime := time.Date(2017, 1, 26, 0, 0, 0, 0, time.Local)
|
2017-09-30 16:27:27 +02:00
|
|
|
for _, test := range []struct {
|
2018-01-16 14:20:59 +01:00
|
|
|
tm TableMetadataToUpdate
|
|
|
|
want *bq.Table
|
2017-09-30 16:27:27 +02:00
|
|
|
}{
|
2018-01-16 14:20:59 +01:00
|
|
|
{
|
|
|
|
tm: TableMetadataToUpdate{},
|
|
|
|
want: &bq.Table{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tm: TableMetadataToUpdate{
|
|
|
|
Description: "d",
|
|
|
|
Name: "n",
|
|
|
|
},
|
|
|
|
want: &bq.Table{
|
|
|
|
Description: "d",
|
|
|
|
FriendlyName: "n",
|
|
|
|
ForceSendFields: []string{"Description", "FriendlyName"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tm: TableMetadataToUpdate{
|
|
|
|
Schema: Schema{fieldSchema("desc", "name", "STRING", false, true)},
|
|
|
|
ExpirationTime: aTime,
|
|
|
|
},
|
|
|
|
want: &bq.Table{
|
|
|
|
Schema: &bq.TableSchema{
|
|
|
|
Fields: []*bq.TableFieldSchema{
|
|
|
|
bqTableFieldSchema("desc", "name", "STRING", "REQUIRED"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ExpirationTime: aTime.UnixNano() / 1e6,
|
|
|
|
ForceSendFields: []string{"Schema", "ExpirationTime"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tm: TableMetadataToUpdate{ViewQuery: "q"},
|
|
|
|
want: &bq.Table{
|
|
|
|
View: &bq.ViewDefinition{Query: "q", ForceSendFields: []string{"Query"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tm: TableMetadataToUpdate{UseLegacySQL: false},
|
|
|
|
want: &bq.Table{
|
|
|
|
View: &bq.ViewDefinition{
|
|
|
|
UseLegacySql: false,
|
|
|
|
ForceSendFields: []string{"UseLegacySql"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tm: TableMetadataToUpdate{ViewQuery: "q", UseLegacySQL: true},
|
|
|
|
want: &bq.Table{
|
|
|
|
View: &bq.ViewDefinition{
|
|
|
|
Query: "q",
|
|
|
|
UseLegacySql: true,
|
|
|
|
ForceSendFields: []string{"Query", "UseLegacySql"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tm: func() (tm TableMetadataToUpdate) {
|
|
|
|
tm.SetLabel("L", "V")
|
|
|
|
tm.DeleteLabel("D")
|
|
|
|
return tm
|
|
|
|
}(),
|
|
|
|
want: &bq.Table{
|
|
|
|
Labels: map[string]string{"L": "V"},
|
|
|
|
NullFields: []string{"Labels.D"},
|
|
|
|
},
|
|
|
|
},
|
2017-09-30 16:27:27 +02:00
|
|
|
} {
|
2018-01-16 14:20:59 +01:00
|
|
|
got := test.tm.toBQ()
|
2017-09-30 16:27:27 +02:00
|
|
|
if !testutil.Equal(got, test.want) {
|
2018-01-16 14:20:59 +01:00
|
|
|
t.Errorf("%+v:\ngot %+v\nwant %+v", test.tm, got, test.want)
|
2017-07-23 09:51:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|