Skip to content

Commit 4822fe9

Browse files
Eliminate bqiface and complete test coverage in query package (#22)
* Add complete unit tests * Remove local bqiface package * Move bigQueryImpl.Query to query package
1 parent 4907b9d commit 4822fe9

File tree

3 files changed

+91
-31
lines changed

3 files changed

+91
-31
lines changed

query/bigquery_runner.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,40 @@
44
package query
55

66
import (
7+
"context"
78
"math"
89
"sort"
910
"strings"
1011

1112
"cloud.google.com/go/bigquery"
12-
"github.com/m-lab/prometheus-bigquery-exporter/query/bqiface"
13+
"github.com/GoogleCloudPlatform/google-cloud-go-testing/bigquery/bqiface"
1314
"github.com/m-lab/prometheus-bigquery-exporter/sql"
15+
"google.golang.org/api/iterator"
1416
)
1517

18+
type bigQueryImpl struct {
19+
bqiface.Client
20+
}
21+
22+
func (b *bigQueryImpl) Query(query string, visit func(row map[string]bigquery.Value) error) error {
23+
q := b.Client.Query(query)
24+
it, err := q.Read(context.Background())
25+
if err != nil {
26+
return err
27+
}
28+
var row map[string]bigquery.Value
29+
for err = it.Next(&row); err == nil; err = it.Next(&row) {
30+
err2 := visit(row)
31+
if err2 != nil {
32+
return err2
33+
}
34+
}
35+
if err != iterator.Done {
36+
return err
37+
}
38+
return nil
39+
}
40+
1641
// BQRunner is a concerete implementation of QueryRunner for BigQuery.
1742
type BQRunner struct {
1843
runner runner
@@ -26,8 +51,8 @@ type runner interface {
2651
// NewBQRunner creates a new QueryRunner instance.
2752
func NewBQRunner(client *bigquery.Client) *BQRunner {
2853
return &BQRunner{
29-
runner: &bqiface.BigQueryImpl{
30-
Client: client,
54+
runner: &bigQueryImpl{
55+
Client: bqiface.AdaptClient(client),
3156
},
3257
}
3358
}

query/bigquery_runner_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"cloud.google.com/go/bigquery"
1010
"github.com/m-lab/prometheus-bigquery-exporter/sql"
11+
12+
"github.com/m-lab/go/cloud/bqfake"
1113
)
1214

1315
func TestRowToMetric(t *testing.T) {
@@ -164,3 +166,64 @@ func TestBQRunner_Query(t *testing.T) {
164166
func TestNewBQRunner(t *testing.T) {
165167
NewBQRunner(nil)
166168
}
169+
170+
func TestBigQueryImpl_Query(t *testing.T) {
171+
tests := []struct {
172+
name string
173+
config bqfake.QueryConfig
174+
query string
175+
visit func(row map[string]bigquery.Value) error
176+
wantErr bool
177+
}{
178+
{
179+
name: "success-iteration",
180+
config: bqfake.QueryConfig{
181+
RowIteratorConfig: bqfake.RowIteratorConfig{
182+
Rows: []map[string]bigquery.Value{{"value": 1.234}},
183+
},
184+
},
185+
visit: func(row map[string]bigquery.Value) error {
186+
return nil
187+
},
188+
},
189+
{
190+
name: "visit-error",
191+
config: bqfake.QueryConfig{
192+
RowIteratorConfig: bqfake.RowIteratorConfig{
193+
Rows: []map[string]bigquery.Value{{"value": 1.234}},
194+
},
195+
},
196+
visit: func(row map[string]bigquery.Value) error {
197+
return fmt.Errorf("Fake visit error")
198+
},
199+
wantErr: true,
200+
},
201+
{
202+
name: "read-error",
203+
config: bqfake.QueryConfig{
204+
ReadErr: fmt.Errorf("This is a fake read error"),
205+
},
206+
wantErr: true,
207+
},
208+
{
209+
name: "iterator-error",
210+
config: bqfake.QueryConfig{
211+
RowIteratorConfig: bqfake.RowIteratorConfig{
212+
IterErr: fmt.Errorf("This is a fake iterator error"),
213+
},
214+
},
215+
wantErr: true,
216+
},
217+
}
218+
for _, tt := range tests {
219+
t.Run(tt.name, func(t *testing.T) {
220+
client := bqfake.NewQueryReadClient(tt.config)
221+
b := &bigQueryImpl{
222+
Client: client,
223+
}
224+
if err := b.Query(tt.query, tt.visit); (err != nil) != tt.wantErr {
225+
t.Errorf("bigQueryImpl.Query() error = %v, wantErr %v", err, tt.wantErr)
226+
}
227+
})
228+
}
229+
}

query/bqiface/bqiface.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)