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				FriendlyName:     "fname",
41				LastModifiedTime: aTimeMillis,
42				Location:         "loc",
43				Labels:           map[string]string{"a": "b"},
44			},
45			&ModelMetadata{
46				CreationTime:     aTime.Truncate(time.Millisecond),
47				Description:      "desc",
48				ETag:             "etag",
49				ExpirationTime:   aTime.Truncate(time.Millisecond),
50				Name:             "fname",
51				LastModifiedTime: aTime.Truncate(time.Millisecond),
52				Location:         "loc",
53				Labels:           map[string]string{"a": "b"},
54			},
55		},
56	} {
57		got, err := bqToModelMetadata(test.in)
58		if err != nil {
59			t.Fatal(err)
60		}
61		if diff := testutil.Diff(got, test.want, cmpopts.IgnoreUnexported(ModelMetadata{})); diff != "" {
62			t.Errorf("%+v:\n, -got, +want:\n%s", test.in, diff)
63		}
64	}
65}
66
67func TestModelMetadataUpdateToBQ(t *testing.T) {
68	aTime := time.Date(2019, 3, 14, 0, 0, 0, 0, time.Local)
69	aTimeMillis := aTime.UnixNano() / 1e6
70
71	for _, test := range []struct {
72		in   ModelMetadataToUpdate
73		want *bq.Model
74	}{
75		{
76			ModelMetadataToUpdate{},
77			&bq.Model{},
78		},
79		{
80			ModelMetadataToUpdate{
81				Description: "d",
82				Name:        "n",
83			},
84			&bq.Model{
85				Description:     "d",
86				FriendlyName:    "n",
87				ForceSendFields: []string{"Description", "FriendlyName"},
88			},
89		},
90		{
91			ModelMetadataToUpdate{
92				ExpirationTime: aTime,
93			},
94			&bq.Model{
95				ExpirationTime:  aTimeMillis,
96				ForceSendFields: []string{"ExpirationTime"},
97			},
98		},
99		{
100			ModelMetadataToUpdate{
101				labelUpdater: labelUpdater{
102					setLabels:    map[string]string{"L": "V"},
103					deleteLabels: map[string]bool{"D": true},
104				},
105			},
106			&bq.Model{
107				Labels:     map[string]string{"L": "V"},
108				NullFields: []string{"Labels.D"},
109			},
110		},
111	} {
112		got, err := test.in.toBQ()
113		if err != nil {
114			t.Fatalf("%+v: %v", test.in, err)
115		}
116		if diff := testutil.Diff(got, test.want); diff != "" {
117			t.Errorf("%+v:\n-got, +want:\n%s", test.in, diff)
118		}
119	}
120}
121