-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathsql_test.go
More file actions
248 lines (220 loc) · 6.82 KB
/
sql_test.go
File metadata and controls
248 lines (220 loc) · 6.82 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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
//
// http://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 sql
import (
"reflect"
"testing"
"github.com/apache/beam/sdks/v2/go/pkg/beam"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/sql/sqlx"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestOptions_Add(t *testing.T) {
test := struct {
opt sqlx.Option
}{
opt: sqlx.Option{
Urn: "this is a string",
Payload: []byte{1, 2, 3, 4},
},
}
o := options{}
o.Add(test.opt)
if o.customs == nil || !reflect.DeepEqual(o.customs[len(o.customs)-1], test.opt) {
t.Errorf("options.Add(%v) failed. For the customs field in options, got %v, want %v", test.opt, o.customs, test.opt)
}
}
func TestInput(t *testing.T) {
test := struct {
inputName string
inputIn beam.PCollection
}{
inputName: "this is a string",
inputIn: beam.PCollection{},
}
o := &options{inputs: make(map[string]beam.PCollection)}
option := Input(test.inputName, test.inputIn)
if option == nil {
t.Errorf("Input(%v, %v) = %v, want not nil", test.inputName, test.inputIn, option)
}
option(o)
if o.inputs == nil || !reflect.DeepEqual(o.inputs[test.inputName], test.inputIn) {
t.Errorf("The function that Input(%v, %v) returned did not work correctly. For the inputs field in options, got %v, want %v", test.inputName, test.inputIn, o.inputs, test.inputIn)
}
}
func TestDialect(t *testing.T) {
test := struct {
dialect string
}{
dialect: "this is a string",
}
o := &options{}
option := Dialect(test.dialect)
if option == nil {
t.Errorf("Dialect(%v) = %v, want not nil", test.dialect, option)
}
option(o)
if !reflect.DeepEqual(o.dialect, test.dialect) {
t.Errorf("The function that Input(%v) returned did not work correctly. For the dialect field in options, got %v, want %v", test.dialect, o.dialect, test.dialect)
}
}
func TestExpansionAddr(t *testing.T) {
test := struct {
addr string
}{
addr: "this is a string",
}
o := &options{}
option := ExpansionAddr(test.addr)
if option == nil {
t.Errorf("ExpansionAddr(%v) = %v, want not nil", test.addr, option)
}
option(o)
if !reflect.DeepEqual(o.expansionAddr, test.addr) {
t.Errorf("The function that ExpansionAddr(%v) returned did not work correctly. For the expansionAddr field in options, got %v, want %v", test.addr, o.expansionAddr, test.addr)
}
}
// TestOutputType tests the OutputType option for setting the output type
// in an SQL transformation in Beam. It verifies both cases: when
// components are provided and when they are not. The test checks if
// the 'outType' field in the options is set correctly based on the
// output type and components.
func TestOutputType(t *testing.T) {
testCases := []struct {
name string
typ reflect.Type
components []typex.FullType
wantNil bool
}{
{
name: "output_type_without_components",
typ: reflect.TypeOf(int64(0)),
},
{
name: "output_type_with_components",
typ: reflect.TypeOf(int64(0)),
components: []typex.FullType{typex.New(reflect.TypeOf(""))},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
o := &options{}
var opt Option
if len(tc.components) > 0 {
opt = OutputType(tc.typ, tc.components...)
} else {
opt = OutputType(tc.typ)
}
opt(o)
var expected typex.FullType
if len(tc.components) > 0 {
expected = typex.New(tc.typ, tc.components...)
} else {
expected = typex.New(tc.typ)
}
opts := cmp.Options{
cmp.Comparer(func(x, y typex.FullType) bool {
// Compare only the type and components
return x.Type() == y.Type()
}),
}
if d := cmp.Diff(expected, o.outType, opts); d != "" {
t.Errorf("OutputType() failed: (-want, +got)\n%s", d)
}
})
}
}
// TestTransform tests the behavior of the Transform function
// in the context of SQL transformations. It checks that the function
// panics when an output type is missing.
func TestTransform_MissingOutputType(t *testing.T) {
p := beam.NewPipeline()
s := p.Root()
col := beam.Create(s, 1, 2, 3)
defer func() {
r := recover()
if r == nil {
t.Error("Transform() with missing output type should panic")
}
if msg, ok := r.(string); !ok || msg != "output type must be specified for sql.Transform" {
t.Errorf("Transform() unexpected panic message: %v", r)
}
}()
Transform(s, "SELECT value FROM test", Input("test", col))
}
// TestMultipleOptions tests applying multiple options at once
// and verifying that they are all correctly applied to the options object.
func TestMultipleOptions(t *testing.T) {
testCases := []struct {
name string
inputName string
dialect string
expansionAddr string
typ reflect.Type
customOpt sqlx.Option
}{
{
name: "all_options",
inputName: "test",
expansionAddr: "localhost:8080",
typ: reflect.TypeOf(int64(0)),
customOpt: sqlx.Option{Urn: "test"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
p := beam.NewPipeline()
s := p.Root()
col := beam.Create(s, 1, 2, 3)
o := &options{
inputs: make(map[string]beam.PCollection),
}
opts := []Option{
Input(tc.inputName, col),
Dialect(tc.dialect),
ExpansionAddr(tc.expansionAddr),
OutputType(tc.typ),
}
for _, opt := range opts {
opt(o)
}
o.Add(tc.customOpt)
// Construct the expected options struct
expected := &options{
inputs: map[string]beam.PCollection{
tc.inputName: col,
},
dialect: tc.dialect,
expansionAddr: tc.expansionAddr,
outType: typex.New(tc.typ),
customs: []sqlx.Option{tc.customOpt},
}
// Define a custom comparer for typex.FullType
fullTypeComparer := cmp.Comparer(func(x, y typex.FullType) bool {
return x.Type() == y.Type() // Compare only the underlying reflect.Type
})
if d := cmp.Diff(
expected,
o,
cmp.AllowUnexported(options{}),
cmpopts.IgnoreUnexported(beam.PCollection{}),
fullTypeComparer, // Use the custom comparer for typex.FullType
); d != "" {
t.Errorf("Options mismatch: (-want, +got)\n%s", d)
}
})
}
}