-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbqfake.go
284 lines (246 loc) · 7.09 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Package bqfake provides tools to construct fake bigquery datasets, tables, query responses, etc.
// DEPRECATED - please use cloudtest/bqfake instead.
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/googleapis/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
loader bqiface.Loader
err error
updateErr error
}
// TableOpts defines field options for Table.
type TableOpts struct {
Dataset
Name string
Metadata *bigquery.TableMetadata
Loader bqiface.Loader
Error error
UpdateErr error
}
// NewTable returns a new instance of Table.
func NewTable(opts TableOpts) *Table {
return &Table{
ds: opts.Dataset,
name: opts.Name,
metadata: opts.Metadata,
loader: opts.Loader,
err: opts.Error,
updateErr: opts.UpdateErr,
}
}
// 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
}
// Update updates the table's `Schema`.
func (tbl Table) Update(ctx context.Context, md bigquery.TableMetadataToUpdate, etag string) (*bigquery.TableMetadata, error) {
if tbl.updateErr != nil {
return nil, tbl.updateErr
}
if md.Schema != nil {
tbl.metadata.Schema = md.Schema
}
return tbl.metadata, nil
}
// LoaderFrom returns a bqiface.Loader.
func (tbl Table) LoaderFrom(src bigquery.LoadSource) bqiface.Loader {
return tbl.loader
}
// Dataset implements part of the bqiface.Dataset interface.
type Dataset struct {
bqiface.Dataset
tables map[string]*Table
metadata *bqiface.DatasetMetadata
err error
}
// NewDataset returns a new instance of Dataset.
func NewDataset(t map[string]*Table, md *bqiface.DatasetMetadata, err error) *Dataset {
return &Dataset{
tables: t,
metadata: md,
err: err,
}
}
// Metadata implements the bqiface method.
func (ds Dataset) Metadata(ctx context.Context) (*bqiface.DatasetMetadata, error) {
if ds.metadata != nil {
return ds.metadata, nil
}
return nil, errors.New("invalid dataset metadata")
}
// Create returns an error.
func (ds Dataset) Create(ctx context.Context, md *bqiface.DatasetMetadata) error {
return ds.err
}
// 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
}
// SetQueryConfig is used to set the ReadErr or RowIteratorConfig.
func (q Query) SetQueryConfig(bqiface.QueryConfig) {
log.Println("SetQueryConfig not implemented")
}
func (q Query) Run(context.Context) (bqiface.Job, error) {
status := &bigquery.JobStatus{State: 0}
job := NewJob(status, q.config, nil)
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
}
// Loader implements parts of bqiface.Loader to allow for testing.
type Loader struct {
bqiface.Loader
job *Job
err error
}
// NewLoader returns a new instance of Loader.
func NewLoader(job *Job, err error) *Loader {
return &Loader{
job: job,
err: err,
}
}
// Run returns a bqiface.Job and an error.
func (l Loader) Run(ctx context.Context) (bqiface.Job, error) {
return l.job, l.err
}
// SetLoadConfig does nothing.
func (l Loader) SetLoadConfig(config bqiface.LoadConfig) {}
// Job implements parts of bqiface.Job to allow some very basic
// unit tests.
type Job struct {
bqiface.Job
status *bigquery.JobStatus
config QueryConfig
waitErr error
}
// NewJob returns a new instance of Job.
func NewJob(status *bigquery.JobStatus, config QueryConfig, err error) *Job {
return &Job{
status: status,
config: config,
waitErr: err,
}
}
// Wait returns a *bigquery.JobStatus and an error.
func (j Job) Wait(context.Context) (*bigquery.JobStatus, error) {
return j.status, j.waitErr
}
func (j Job) LastStatus() *bigquery.JobStatus {
log.Println("LastStatus not implemented")
return nil
}
func (j Job) Read(context.Context) (bqiface.RowIterator, error) {
if j.config.ReadErr != nil {
return nil, j.config.ReadErr
}
return &RowIterator{config: j.config.RowIteratorConfig}, 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
}