File: create_table_test.go

package info (click to toggle)
golang-google-cloud 0.9.0-10
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 4,964 kB
  • sloc: sh: 98; makefile: 82; awk: 67
file content (103 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 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 (
	"reflect"
	"testing"
	"time"

	"golang.org/x/net/context"
	bq "google.golang.org/api/bigquery/v2"
)

type createTableRecorder struct {
	conf *createTableConf
	service
}

func (rec *createTableRecorder) createTable(ctx context.Context, conf *createTableConf) error {
	rec.conf = conf
	return nil
}

func TestCreateTableOptions(t *testing.T) {
	s := &createTableRecorder{}
	c := &Client{
		projectID: "p",
		service:   s,
	}
	ds := c.Dataset("d")
	table := ds.Table("t")
	exp := time.Now()
	q := "query"
	if err := table.Create(context.Background(), TableExpiration(exp), ViewQuery(q), UseStandardSQL()); err != nil {
		t.Fatalf("err calling Table.Create: %v", err)
	}
	want := createTableConf{
		projectID:      "p",
		datasetID:      "d",
		tableID:        "t",
		expiration:     exp,
		viewQuery:      q,
		useStandardSQL: true,
	}
	if !reflect.DeepEqual(*s.conf, want) {
		t.Errorf("createTableConf: got:\n%v\nwant:\n%v", *s.conf, want)
	}

	sc := Schema{fieldSchema("desc", "name", "STRING", false, true)}
	if err := table.Create(context.Background(), TableExpiration(exp), sc); err != nil {
		t.Fatalf("err calling Table.Create: %v", err)
	}
	want = createTableConf{
		projectID:  "p",
		datasetID:  "d",
		tableID:    "t",
		expiration: exp,
		// No need for an elaborate schema, that is tested in schema_test.go.
		schema: &bq.TableSchema{
			Fields: []*bq.TableFieldSchema{
				bqTableFieldSchema("desc", "name", "STRING", "REQUIRED"),
			},
		},
	}
	if !reflect.DeepEqual(*s.conf, want) {
		t.Errorf("createTableConf: got:\n%v\nwant:\n%v", *s.conf, want)
	}

	partitionCases := []struct {
		timePartitioning   TimePartitioning
		expectedExpiration time.Duration
	}{
		{TimePartitioning{}, time.Duration(0)},
		{TimePartitioning{time.Second}, time.Second},
	}

	for _, c := range partitionCases {
		if err := table.Create(context.Background(), c.timePartitioning); err != nil {
			t.Fatalf("err calling Table.Create: %v", err)
		}
		want = createTableConf{
			projectID:        "p",
			datasetID:        "d",
			tableID:          "t",
			timePartitioning: &TimePartitioning{c.expectedExpiration},
		}
		if !reflect.DeepEqual(*s.conf, want) {
			t.Errorf("createTableConf: got:\n%v\nwant:\n%v", *s.conf, want)
		}
	}
}