1// Copyright 2019 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
15package bigquery
16
17import (
18	"testing"
19	"time"
20
21	"cloud.google.com/go/internal/testutil"
22	"github.com/google/go-cmp/cmp/cmpopts"
23	bq "google.golang.org/api/bigquery/v2"
24)
25
26func TestBQToModelMetadata(t *testing.T) {
27	aTime := time.Date(2019, 3, 14, 0, 0, 0, 0, time.Local)
28	aTimeMillis := aTime.UnixNano() / 1e6
29	for _, test := range []struct {
30		in   *bq.Model
31		want *ModelMetadata
32	}{
33		{&bq.Model{}, &ModelMetadata{}},
34		{
35			&bq.Model{
36				CreationTime:            aTimeMillis,
37				Description:             "desc",
38				Etag:                    "etag",
39				ExpirationTime:          aTimeMillis,
40				EncryptionConfiguration: &bq.EncryptionConfiguration{KmsKeyName: "keyName"},
41				FriendlyName:            "fname",
42				LastModifiedTime:        aTimeMillis,
43				Location:                "loc",
44				Labels:                  map[string]string{"a": "b"},
45			},
46			&ModelMetadata{
47				CreationTime:     aTime.Truncate(time.Millisecond),
48				Description:      "desc",
49				ETag:             "etag",
50				ExpirationTime:   aTime.Truncate(time.Millisecond),
51				Name:             "fname",
52				LastModifiedTime: aTime.Truncate(time.Millisecond),
53				EncryptionConfig: &EncryptionConfig{KMSKeyName: "keyName"},
54				Location:         "loc",
55				Labels:           map[string]string{"a": "b"},
56			},
57		},
58	} {
59		got, err := bqToModelMetadata(test.in)
60		if err != nil {
61			t.Fatal(err)
62		}
63		if diff := testutil.Diff(got, test.want, cmpopts.IgnoreUnexported(ModelMetadata{})); diff != "" {
64			t.Errorf("%+v:\n, -got, +want:\n%s", test.in, diff)
65		}
66	}
67}
68
69func TestModelMetadataUpdateToBQ(t *testing.T) {
70	aTime := time.Date(2019, 3, 14, 0, 0, 0, 0, time.Local)
71	aTimeMillis := aTime.UnixNano() / 1e6
72
73	for _, test := range []struct {
74		in   ModelMetadataToUpdate
75		want *bq.Model
76	}{
77		{
78			ModelMetadataToUpdate{},
79			&bq.Model{},
80		},
81		{
82			ModelMetadataToUpdate{
83				Description: "d",
84				Name:        "n",
85			},
86			&bq.Model{
87				Description:     "d",
88				FriendlyName:    "n",
89				ForceSendFields: []string{"Description", "FriendlyName"},
90			},
91		},
92		{
93			ModelMetadataToUpdate{
94				ExpirationTime: aTime,
95			},
96			&bq.Model{
97				ExpirationTime:  aTimeMillis,
98				ForceSendFields: []string{"ExpirationTime"},
99			},
100		},
101		{
102			ModelMetadataToUpdate{
103				labelUpdater: labelUpdater{
104					setLabels:    map[string]string{"L": "V"},
105					deleteLabels: map[string]bool{"D": true},
106				},
107			},
108			&bq.Model{
109				Labels:     map[string]string{"L": "V"},
110				NullFields: []string{"Labels.D"},
111			},
112		},
113	} {
114		got, err := test.in.toBQ()
115		if err != nil {
116			t.Fatalf("%+v: %v", test.in, err)
117		}
118		if diff := testutil.Diff(got, test.want); diff != "" {
119			t.Errorf("%+v:\n-got, +want:\n%s", test.in, diff)
120		}
121	}
122}
123