-
Notifications
You must be signed in to change notification settings - Fork 2k
/
testing.go
291 lines (267 loc) · 7.15 KB
/
testing.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
285
286
287
288
289
290
291
/*
Copyright 2019 The Vitess Authors.
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
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 sqltypes
import (
crand "crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// Functions in this file should only be used for testing.
// This is an experiment to see if test code bloat can be
// reduced and readability improved.
// MakeTestFields builds a []*querypb.Field for testing.
//
// fields := sqltypes.MakeTestFields(
// "a|b",
// "int64|varchar",
// )
//
// The field types are as defined in querypb and are case
// insensitive. Column delimiters must be used only to sepearate
// strings and not at the beginning or the end.
func MakeTestFields(names, types string) []*querypb.Field {
n := split(names)
t := split(types)
var fields []*querypb.Field
for i := range n {
fields = append(fields, &querypb.Field{
Name: n[i],
Type: querypb.Type(querypb.Type_value[strings.ToUpper(t[i])]),
})
}
return fields
}
// MakeTestResult builds a *sqltypes.Result object for testing.
//
// result := sqltypes.MakeTestResult(
// fields,
// " 1|a",
// "10|abcd",
// )
//
// The field type values are set as the types for the rows built.
// Spaces are trimmed from row values. "null" is treated as NULL.
func MakeTestResult(fields []*querypb.Field, rows ...string) *Result {
result := &Result{
Fields: fields,
}
if len(rows) > 0 {
result.Rows = make([][]Value, len(rows))
}
for i, row := range rows {
result.Rows[i] = make([]Value, len(fields))
for j, col := range split(row) {
if strings.ToLower(col) == "null" {
result.Rows[i][j] = NULL
continue
}
result.Rows[i][j] = MakeTrusted(fields[j].Type, []byte(col))
}
}
return result
}
// MakeTestStreamingResults builds a list of results for streaming.
//
// results := sqltypes.MakeStreamingResults(
// fields,
// "1|a",
// "2|b",
// "---",
// "c|c",
// )
//
// The first result contains only the fields. Subsequent results
// are built using the field types. Every input that starts with a "-"
// is treated as streaming delimiter for one result. A final
// delimiter must not be supplied.
func MakeTestStreamingResults(fields []*querypb.Field, rows ...string) []*Result {
var results []*Result
results = append(results, &Result{Fields: fields})
start := 0
cur := 0
// Add a final streaming delimiter to simplify the loop below.
rows = append(rows, "-")
for cur < len(rows) {
if rows[cur][0] != '-' {
cur++
continue
}
result := MakeTestResult(fields, rows[start:cur]...)
result.Fields = nil
result.RowsAffected = 0
results = append(results, result)
start = cur + 1
cur = start
}
return results
}
// TestBindVariable makes a *querypb.BindVariable from any.
// It panics on invalid input.
// This function should only be used for testing.
func TestBindVariable(v any) *querypb.BindVariable {
if v == nil {
return NullBindVariable
}
bv, err := BuildBindVariable(v)
if err != nil {
panic(err)
}
return bv
}
// TestValue builds a Value from typ and val.
// This function should only be used for testing.
func TestValue(typ querypb.Type, val string) Value {
return MakeTrusted(typ, []byte(val))
}
// TestTuple builds a tuple Value from a list of Values.
// This function should only be used for testing.
func TestTuple(vals ...Value) Value {
return Value{
typ: uint16(Tuple),
val: encodeTuple(vals),
}
}
// PrintResults prints []*Results into a string.
// This function should only be used for testing.
func PrintResults(results []*Result) string {
var b strings.Builder
for i, r := range results {
if i == 0 {
fmt.Fprintf(&b, "%v", r)
continue
}
fmt.Fprintf(&b, ", %v", r)
}
return b.String()
}
func split(str string) []string {
return strings.Split(str, "|")
}
func TestRandomValues() (Value, Value) {
if rand.Int()%2 == 0 {
// create a single value, and turn it into two different types
v := rand.Int()
return randomNumericType(v), randomNumericType(v)
}
// just produce two arbitrary random values and compare
return randomNumericType(rand.Int()), randomNumericType(rand.Int())
}
func randomNumericType(i int) Value {
r := rand.Intn(len(numericTypes))
return numericTypes[r](i)
}
var numericTypes = []func(int) Value{
func(i int) Value { return NULL },
func(i int) Value { return NewInt8(int8(i)) },
func(i int) Value { return NewInt32(int32(i)) },
func(i int) Value { return NewInt64(int64(i)) },
func(i int) Value { return NewUint64(uint64(i)) },
func(i int) Value { return NewUint32(uint32(i)) },
func(i int) Value { return NewFloat64(float64(i)) },
func(i int) Value { return NewDecimal(fmt.Sprintf("%d", i)) },
func(i int) Value { return NewVarChar(fmt.Sprintf("%d", i)) },
func(i int) Value { return NewVarChar(fmt.Sprintf(" %f aa", float64(i))) },
}
type RandomGenerator func() Value
func randomBytes() []byte {
b := make([]byte, rand.Intn(128))
_, _ = crand.Read(b)
return b
}
var RandomGenerators = map[Type]RandomGenerator{
Null: func() Value {
return NULL
},
Int8: func() Value {
return NewInt8(int8(rand.Intn(255)))
},
Int32: func() Value {
return NewInt32(rand.Int31())
},
Int64: func() Value {
return NewInt64(rand.Int63())
},
Uint32: func() Value {
return NewUint32(rand.Uint32())
},
Uint64: func() Value {
return NewUint64(rand.Uint64())
},
Float64: func() Value {
return NewFloat64(rand.ExpFloat64())
},
Decimal: func() Value {
dec := fmt.Sprintf("%d.%d", rand.Intn(999999999), rand.Intn(999999999))
if rand.Int()&0x1 == 1 {
dec = "-" + dec
}
return NewDecimal(dec)
},
VarChar: func() Value {
return NewVarChar(base64.StdEncoding.EncodeToString(randomBytes()))
},
VarBinary: func() Value {
return NewVarBinary(string(randomBytes()))
},
Date: func() Value {
return NewDate(randTime().Format(time.DateOnly))
},
Datetime: func() Value {
return NewDatetime(randTime().Format(time.DateTime))
},
Timestamp: func() Value {
return NewTimestamp(randTime().Format(time.DateTime))
},
Time: func() Value {
return NewTime(randTime().Format(time.TimeOnly))
},
TypeJSON: func() Value {
var j string
switch rand.Intn(6) {
case 0:
j = "null"
case 1:
i := rand.Int63()
if rand.Int()&0x1 == 1 {
i = -i
}
j = strconv.FormatInt(i, 10)
case 2:
j = strconv.FormatFloat(rand.NormFloat64(), 'g', -1, 64)
case 3:
j = strconv.Quote(hex.EncodeToString(randomBytes()))
case 4:
j = "true"
case 5:
j = "false"
}
v, err := NewJSON(j)
if err != nil {
panic(err)
}
return v
},
}
func randTime() time.Time {
min := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2070, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
delta := max - min
sec := rand.Int63n(delta) + min
return time.Unix(sec, 0)
}