-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigquery_mock_test.go
121 lines (118 loc) · 2.8 KB
/
bigquery_mock_test.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
package bigquery_mock
import (
"context"
"fmt"
"github.com/goccy/bigquery-emulator/server"
"github.com/goccy/bigquery-emulator/types"
"google.golang.org/api/iterator"
"path/filepath"
"testing"
)
func TestMockBigQueryWithYamlFixtureLoad(t *testing.T) {
projectName := "test"
// because we have two records in the yaml file
expectedResultNo := 2
ctx := context.Background()
bqClient, err := MockBigQuery(projectName, server.YAMLSource(filepath.Join("fixtures", "sample_bigquery_fixture.yaml")))
if err != nil {
t.Fatalf("error to mock bigquery %v", err)
}
query := bqClient.Query("select * from test.dataset_1.table_1")
job, err := query.Run(ctx)
if err != nil {
t.Fatalf("error to run query %v", err)
}
it, err := job.Read(ctx)
if err != nil {
t.Fatalf("error to run query %v", err)
}
type result struct {
}
var res []result
for {
var row result
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
t.Fatalf("bigquery iterator next failed %v", err)
}
res = append(res, row)
}
if len(res) != expectedResultNo {
t.Errorf("result mismatching epected %d, got %d", expectedResultNo, len(res))
}
}
func TestMockBigQueryWithInlineSourcing(t *testing.T) {
expectedResultNo := 2
ctx := context.Background()
projectName := "test2"
datasetName := "dataset_1"
tableName := "table_1"
source := server.StructSource(
types.NewProject(
projectName,
types.NewDataset(
datasetName,
types.NewTable(
tableName,
[]*types.Column{
types.NewColumn("id", types.INTEGER),
types.NewColumn("field_a", types.BOOL),
types.NewColumn("field_b", types.BOOL),
types.NewColumn("field_c", types.STRING),
types.NewColumn("field_e", types.STRING),
},
types.Data{
{
"id": 1,
"field_a": false,
"field_b": false,
"field_c": "ok",
"field_d": "yuseferi",
"field_e": "A",
}, {
"id": 3,
"field_a": false,
"field_b": true,
"field_c": "nok",
"field_d": "golang",
"field_e": "A",
},
},
),
),
),
)
bqClient, err := MockBigQuery(projectName, source)
if err != nil {
t.Fatalf("error to mock bigquery %v", err)
}
query := bqClient.Query(fmt.Sprintf("select * from %s.%s.%s", projectName, datasetName, tableName))
job, err := query.Run(ctx)
if err != nil {
t.Fatalf("error to run query %v", err)
}
it, err := job.Read(ctx)
if err != nil {
t.Fatalf("error to run query %v", err)
}
type result struct {
}
var res []result
for {
var row result
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
t.Fatalf("bigquery iterator next failed %v", err)
}
res = append(res, row)
}
if len(res) != expectedResultNo {
t.Errorf("result mismatching epected %d, got %d", expectedResultNo, len(res))
}
}