1// Copyright 2015 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
20	"cloud.google.com/go/internal/testutil"
21	"github.com/google/go-cmp/cmp"
22	bq "google.golang.org/api/bigquery/v2"
23)
24
25func defaultExtractJob() *bq.Job {
26	return &bq.Job{
27		JobReference: &bq.JobReference{JobId: "RANDOM", ProjectId: "client-project-id"},
28		Configuration: &bq.JobConfiguration{
29			Extract: &bq.JobConfigurationExtract{
30				SourceTable: &bq.TableReference{
31					ProjectId: "client-project-id",
32					DatasetId: "dataset-id",
33					TableId:   "table-id",
34				},
35				DestinationUris: []string{"uri"},
36			},
37		},
38	}
39}
40
41func defaultGCS() *GCSReference {
42	return &GCSReference{
43		URIs: []string{"uri"},
44	}
45}
46
47func TestExtract(t *testing.T) {
48	defer fixRandomID("RANDOM")()
49	c := &Client{
50		projectID: "client-project-id",
51	}
52
53	testCases := []struct {
54		dst    *GCSReference
55		src    *Table
56		config ExtractConfig
57		want   *bq.Job
58	}{
59		{
60			dst:  defaultGCS(),
61			src:  c.Dataset("dataset-id").Table("table-id"),
62			want: defaultExtractJob(),
63		},
64		{
65			dst: defaultGCS(),
66			src: c.Dataset("dataset-id").Table("table-id"),
67			config: ExtractConfig{
68				DisableHeader: true,
69				Labels:        map[string]string{"a": "b"},
70			},
71			want: func() *bq.Job {
72				j := defaultExtractJob()
73				j.Configuration.Labels = map[string]string{"a": "b"}
74				f := false
75				j.Configuration.Extract.PrintHeader = &f
76				return j
77			}(),
78		},
79		{
80			dst: func() *GCSReference {
81				g := NewGCSReference("uri")
82				g.Compression = Gzip
83				g.DestinationFormat = JSON
84				g.FieldDelimiter = "\t"
85				return g
86			}(),
87			src: c.Dataset("dataset-id").Table("table-id"),
88			want: func() *bq.Job {
89				j := defaultExtractJob()
90				j.Configuration.Extract.Compression = "GZIP"
91				j.Configuration.Extract.DestinationFormat = "NEWLINE_DELIMITED_JSON"
92				j.Configuration.Extract.FieldDelimiter = "\t"
93				return j
94			}(),
95		},
96	}
97
98	for i, tc := range testCases {
99		ext := tc.src.ExtractorTo(tc.dst)
100		tc.config.Src = ext.Src
101		tc.config.Dst = ext.Dst
102		ext.ExtractConfig = tc.config
103		got := ext.newJob()
104		checkJob(t, i, got, tc.want)
105
106		jc, err := bqToJobConfig(got.Configuration, c)
107		if err != nil {
108			t.Fatalf("#%d: %v", i, err)
109		}
110		diff := testutil.Diff(jc, &ext.ExtractConfig,
111			cmp.AllowUnexported(Table{}, Client{}))
112		if diff != "" {
113			t.Errorf("#%d: (got=-, want=+:\n%s", i, diff)
114		}
115	}
116}
117