-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbqfake.go
169 lines (145 loc) · 4.33 KB
/
bqfake.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Package bqfake provides tools to construct fake bigquery datasets, tables, query responses, etc.
package bqfake
/* Outline:
The shadow components implement the corresponding bqiface interface, but instead of wrapping
bigquery objects, they implement fakes that can be constructed incrementally.
*/
import (
"context"
"errors"
"fmt"
"log"
"cloud.google.com/go/bigquery"
"github.com/GoogleCloudPlatform/google-cloud-go-testing/bigquery/bqiface"
"google.golang.org/api/iterator"
)
// Table implements part of the bqiface.Table interface required for basic testing
// Other parts of the interface should be implemented as needed.
type Table struct {
bqiface.Table
ds Dataset
name string
// NOTE: TableType is used to indicate if this is initialized
metadata *bigquery.TableMetadata
}
// ProjectID implements the bqiface method.
func (tbl Table) ProjectID() string {
return tbl.ds.ProjectID()
}
// DatasetID implements the bqiface method.
func (tbl Table) DatasetID() string {
return tbl.ds.DatasetID()
}
// TableID implements the bqiface method.
func (tbl Table) TableID() string {
return tbl.name
}
// FullyQualifiedName implements the bqiface method.
func (tbl Table) FullyQualifiedName() string {
return tbl.ProjectID() + ":" + tbl.DatasetID() + "." + tbl.name
}
// Metadata implements the bqiface method.
func (tbl Table) Metadata(ctx context.Context) (*bigquery.TableMetadata, error) {
if tbl.metadata == nil {
return nil, errors.New("Table object incorrectly initialized")
}
if tbl.metadata.Type == "" {
log.Printf("Metadata %p %v\n", tbl.metadata, tbl.metadata)
msg := fmt.Sprintf("Error 404: Not found: Table %s, notFound", tbl.FullyQualifiedName())
return nil, errors.New(msg)
}
log.Printf("Metadata %p %v\n", tbl.metadata, tbl.metadata)
return tbl.metadata, nil
}
// Create implements the bqiface method.
func (tbl Table) Create(ctx context.Context, meta *bigquery.TableMetadata) error {
log.Println("Create", meta)
if tbl.metadata == nil {
return errors.New("Table object incorrectly initialized")
}
if tbl.metadata.Type != "" {
return errors.New("TODO: should return a table exists error")
}
*tbl.metadata = *meta
if tbl.metadata.Type == "" {
tbl.metadata.Type = "TABLE"
}
log.Printf("Metadata %p %v\n", tbl.metadata, tbl.metadata)
return nil
}
// Dataset implements part of the bqiface.Dataset interface.
type Dataset struct {
bqiface.Dataset
tables map[string]*Table
}
// Table implements the bqiface method.
func (ds Dataset) Table(name string) bqiface.Table {
t, ok := ds.tables[name]
if !ok {
t = &Table{ds: ds, name: name, metadata: &bigquery.TableMetadata{}}
// TODO is this better? t = &Table{ds: ds.Dataset, name: name, metadata: &pm}
ds.tables[name] = t
}
return t
}
// ClientConfig contains configuration for injecting result and error values.
type ClientConfig struct {
QueryConfig
}
// QueryConfig contains configuration for injecting query results and error values.
type QueryConfig struct {
ReadErr error
RowIteratorConfig
}
// RowIteratorConfig contains configuration for injecting row iteration results and error values.
type RowIteratorConfig struct {
IterErr error
Rows []map[string]bigquery.Value
}
// Query implements parts of bqiface.Query to allow some very basic
// unit tests.
type Query struct {
bqiface.Query
config QueryConfig
}
func (q Query) SetQueryConfig(bqiface.QueryConfig) {
log.Println("SetQueryConfig not implemented")
}
func (q Query) Run(context.Context) (bqiface.Job, error) {
log.Println("Run not implemented")
return Job{}, nil
}
func (q Query) Read(context.Context) (bqiface.RowIterator, error) {
if q.config.ReadErr != nil {
return nil, q.config.ReadErr
}
return &RowIterator{config: q.config.RowIteratorConfig}, nil
}
// Job implements parts of bqiface.Job to allow some very basic
// unit tests.
type Job struct {
bqiface.Job
}
func (j Job) Wait(context.Context) (*bigquery.JobStatus, error) {
log.Println("Wait not implemented")
return nil, nil
}
type RowIterator struct {
bqiface.RowIterator
config RowIteratorConfig
index int
}
func (r *RowIterator) Next(dst interface{}) error {
// Check config for an error.
if r.config.IterErr != nil {
return r.config.IterErr
}
// Allow an empty config to return Done.
if r.index >= len(r.config.Rows) {
return iterator.Done
}
v := dst.(*map[string]bigquery.Value)
*v = r.config.Rows[r.index]
r.index++
return nil
}