-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathrows_test.go
More file actions
225 lines (202 loc) · 6.57 KB
/
rows_test.go
File metadata and controls
225 lines (202 loc) · 6.57 KB
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spannerdriver
import (
"database/sql/driver"
"errors"
"fmt"
"io"
"reflect"
"testing"
"cloud.google.com/go/spanner"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/types/known/structpb"
)
var _ rowIterator = &testIterator{}
type testIterator struct {
metadata *sppb.ResultSetMetadata
rows []*spanner.Row
index int
}
func (t *testIterator) Next() (*spanner.Row, error) {
if t.index == len(t.rows) {
return nil, io.EOF
}
row := t.rows[t.index]
t.index++
return row, nil
}
func (t *testIterator) Stop() {
}
func (t *testIterator) Metadata() (*sppb.ResultSetMetadata, error) {
return t.metadata, nil
}
func (t *testIterator) ResultSetStats() *sppb.ResultSetStats {
return &sppb.ResultSetStats{}
}
func newRow(t *testing.T, cols []string, vals []interface{}) *spanner.Row {
row, err := spanner.NewRow(cols, vals)
if err != nil {
t.Fatalf("failed to create test row: %v", err)
}
return row
}
func TestRows_Next(t *testing.T) {
cols := []string{"COL1", "COL2", "COL3"}
it := testIterator{
metadata: &sppb.ResultSetMetadata{
RowType: &sppb.StructType{
Fields: []*sppb.StructType_Field{
{Name: "COL1", Type: &sppb.Type{Code: sppb.TypeCode_INT64}},
{Name: "COL2", Type: &sppb.Type{Code: sppb.TypeCode_STRING}},
{Name: "COL3", Type: &sppb.Type{Code: sppb.TypeCode_FLOAT64}},
},
},
},
rows: []*spanner.Row{
newRow(t, cols, []interface{}{int64(1), "Test1", 3.14 * 1.0}),
newRow(t, cols, []interface{}{int64(2), spanner.NullString{}, spanner.NullFloat64{}}),
newRow(t, cols, []interface{}{int64(3), "Test3", 3.14 * 3.0}),
},
}
rows := rows{it: &it}
dest := make([]driver.Value, 3)
values := make([][]driver.Value, 0)
for {
err := rows.Next(dest)
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
row := make([]driver.Value, 3)
copy(row, dest)
values = append(values, row)
}
if g, w := len(values), 3; g != w {
t.Fatalf("values length mismatch\nGot: %v\nWant: %v", g, w)
}
for i := int64(0); i < int64(len(values)); i++ {
if g, w := values[i][0], i+1; g != w {
t.Fatalf("COL1 value mismatch for row %d\nGot: %v\nWant: %v", i, g, w)
}
if i == 0 || i == 2 {
if g, w := values[i][1], fmt.Sprintf("Test%d", i+1); g != w {
t.Fatalf("COL2 value mismatch for row %d\nGot: %v\nWant: %v", i, g, w)
}
if g, w := values[i][2], 3.14*float64(i+1); g != w {
t.Fatalf("COL3 value mismatch for row %d\nGot: %v\nWant: %v", i, g, w)
}
} else {
if g, w := values[i][1], driver.Value(nil); g != w {
t.Fatalf("COL2 value mismatch for row %d\nGot: %v\nWant: %v", i, g, w)
}
if g, w := values[i][2], driver.Value(nil); g != w {
t.Fatalf("COL3 value mismatch for row %d\nGot: %v\nWant: %v", i, g, w)
}
}
}
}
func TestRows_Next_Unsupported(t *testing.T) {
unspecifiedType := &sppb.Type{Code: sppb.TypeCode_TYPE_CODE_UNSPECIFIED}
it := testIterator{
metadata: &sppb.ResultSetMetadata{
RowType: &sppb.StructType{
Fields: []*sppb.StructType_Field{
{Name: "COL1", Type: unspecifiedType},
},
},
},
rows: []*spanner.Row{
newRow(t, []string{"COL1"}, []any{
spanner.GenericColumnValue{
Type: unspecifiedType,
Value: &structpb.Value{},
},
}),
},
}
rows := rows{it: &it}
dest := make([]driver.Value, 1)
err := rows.Next(dest)
if err == nil {
t.Fatal("expected an error, but got nil")
}
const expectedError = "unsupported type TYPE_CODE_UNSPECIFIED, use spannerdriver.ExecOptions{DecodeOption: spannerdriver.DecodeOptionProto} to return the underlying protobuf value"
if err.Error() != expectedError {
t.Fatalf("expected error %q, but got %q", expectedError, err.Error())
}
}
func TestEmptyRows(t *testing.T) {
r := createDriverResultRows(&result{}, false, func() {}, &ExecOptions{})
if g, w := r.Columns(), []string{"affected_rows"}; !cmp.Equal(g, w) {
t.Fatalf("columns mismatch\n Got: %v\nWant: %v", g, w)
}
if r.HasNextResultSet() {
t.Fatalf("unexpected next result set available")
}
}
func TestEmptyRowsWithMetadataAndStats(t *testing.T) {
r := createDriverResultRows(&result{}, false, func() {}, &ExecOptions{ReturnResultSetMetadata: true, ReturnResultSetStats: true})
// The first result set should contain ResultSetMetadata.
if g, w := r.Columns(), []string{"metadata"}; !cmp.Equal(g, w) {
t.Fatalf("columns mismatch\n Got: %v\nWant: %v", g, w)
}
values := make([]driver.Value, 1)
if err := r.Next(values); err != nil {
t.Fatalf("unexpected error from Next: %v", err)
}
if g, w := reflect.TypeOf(values[0]), reflect.TypeOf(&sppb.ResultSetMetadata{}); g != w {
t.Fatalf("result set metadata type mismatch\n Got: %v\nWant: %v", g, w)
}
if g, w := r.Next(values), io.EOF; !errors.Is(g, w) {
t.Fatalf("next result set mismatch\n Got: %v\nWant: %v", g, w)
}
// The second result set should contain the actual data (which is empty).
if !r.HasNextResultSet() {
t.Fatalf("missing next result set")
}
if err := r.NextResultSet(); err != nil {
t.Fatalf("unexpected error from NextResultSet: %v", err)
}
if g, w := r.Columns(), []string{"affected_rows"}; !cmp.Equal(g, w) {
t.Fatalf("columns mismatch\n Got: %v\nWant: %v", g, w)
}
// There should be no data.
if g, w := r.Next(values), io.EOF; !errors.Is(g, w) {
t.Fatalf("next result set mismatch\n Got: %v\nWant: %v", g, w)
}
// The third result set should contain ResultSetStats.
if !r.HasNextResultSet() {
t.Fatalf("missing next result set")
}
if err := r.NextResultSet(); err != nil {
t.Fatalf("unexpected error from NextResultSet: %v", err)
}
if err := r.Next(values); err != nil {
t.Fatalf("unexpected error from Next: %v", err)
}
if g, w := reflect.TypeOf(values[0]), reflect.TypeOf(&sppb.ResultSetStats{}); g != w {
t.Fatalf("result set stats type mismatch\n Got: %v\nWant: %v", g, w)
}
if g, w := r.Next(values), io.EOF; !errors.Is(g, w) {
t.Fatalf("next result set mismatch\n Got: %v\nWant: %v", g, w)
}
// There should be no more result sets.
if r.HasNextResultSet() {
t.Fatalf("unexpected next result set available")
}
}