-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlx_test.go
275 lines (240 loc) · 6.61 KB
/
mysqlx_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
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
package mysqlx
import (
"bytes"
"context"
"crypto/rand"
"database/sql"
"fmt"
"io"
"math"
"testing"
"time"
)
func anySlice[T any](in []T) []any {
r := make([]any, len(in))
for i, x := range in {
r[i] = any(x)
}
return r
}
func anySlicePtr[T any](out []T) []any {
r := make([]any, len(out))
for i := range out {
r[i] = &out[i]
}
return r
}
var roundTripSelects = [...]string{
0: "",
1: "SELECT ?",
2: "SELECT ?, ?",
3: "SELECT ?, ?, ?",
4: "SELECT ?, ?, ?, ?",
5: "SELECT ?, ?, ?, ?, ?",
6: "SELECT ?, ?, ?, ?, ?, ?",
7: "SELECT ?, ?, ?, ?, ?, ?, ?",
8: "SELECT ?, ?, ?, ?, ?, ?, ?, ?",
9: "SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?",
}
func roundTrip[T any](t *testing.T, in []T) []T {
t.Helper()
out := make([]T, len(in))
query(t, roundTripSelects[len(in)], anySlice(in), func(rows *sql.Rows) error { return rows.Scan(anySlicePtr(out)...) })
return out
}
func roundTripComparable[T comparable](t *testing.T, in []T) {
actual := roundTrip(t, in)
if len(in) != len(actual) {
t.Fatalf("slice len expected %d, got %d", len(in), len(actual))
}
for i, x := range in {
if a := actual[i]; a != x {
t.Fatalf("index %d expected %v, got %v", i, in, a)
}
}
}
func TestNull(t *testing.T) {
out := any(42)
query(t, roundTripSelects[1], []any{nil}, func(rows *sql.Rows) error { return rows.Scan(&out) })
if !isNil(out) {
t.Fatalf("expected nil, got %T(%v)", out, out)
}
}
func TestTypes(t *testing.T) {
roundTripComparable(t, []bool{false, true})
roundTripComparable(t, []uint64{0, math.MaxUint64})
roundTripComparable(t, []int64{math.MinInt64, 0, math.MaxInt64})
// @TODO math.MaxFloat32 appears to get truncated on a roundtrip
roundTripComparable(t, []float32{0, math.SmallestNonzeroFloat32, math.MaxFloat32 - 3.5e+32})
// @TODO math.MaxFloat64 appears to get truncated on a roundtrip
roundTripComparable(t, []float64{0, math.SmallestNonzeroFloat64, math.MaxFloat64 - 3.1348623157e+302})
roundTripComparable(t, []string{"", "abc", "abcdef"})
// out := roundTrip(t, [][]byte{{}, {0x00}, []byte("abcdef")})
}
func TestDuration(t *testing.T) {
var actual []byte
tests := []struct {
time.Duration
expected string
}{
{0, "0 00 00"},
{1 * time.Second, "0 00 01"},
{59 * time.Second, "0 00 59"},
{1 * time.Minute, "0 01 00"},
{59 * time.Minute, "0 59 00"},
{1 * time.Hour, "1 00 00"},
{24 * time.Hour, "24 00 00"},
{839*time.Hour - time.Second, "838 59 59"},
{-1 * time.Second, "-0 00 01"},
{-59 * time.Second, "-0 00 59"},
{-1 * time.Minute, "-0 01 00"},
{-59 * time.Minute, "-0 59 00"},
{-1 * time.Hour, "-1 00 00"},
{-24 * time.Hour, "-24 00 00"},
{-839*time.Hour + time.Second, "-838 59 59"},
}
for _, tt := range tests {
query(t, "SELECT TIME_FORMAT(?, '%k %i %s')", []any{tt.Duration}, func(rows *sql.Rows) error { return rows.Scan(&actual) })
if string(actual) != tt.expected {
t.Fatalf("expected %q got %q", tt.expected, actual)
}
}
}
func TestLargeBlob(t *testing.T) {
const (
minSize = 1 << 10
maxSize = 4 << 20
)
sizes := []int{minSize, 10240, 1 << 20, maxSize}
expected := make([]byte, maxSize)
for _, n := range sizes {
if _, err := io.ReadFull(rand.Reader, expected[n-minSize:n]); err != nil {
t.Fatalf("failed to generate random blob: %v", err)
}
}
for _, n := range sizes {
t.Run(fmt.Sprintf("%d", n), func(t *testing.T) {
actual := roundTrip(t, [][]byte{expected[:n]})
if len(actual[0]) != n || !bytes.Equal(expected[:n], actual[0]) {
t.Fatalf("expected %s...%d got %s...%d", expected[:9], len(expected), actual[0][:9], len(actual[0]))
}
})
}
}
func TestRowsAffected(t *testing.T) {
db := NewDB(t)
defer db.Close()
_, err := db.ExecContext(context.Background(), "DROP TABLE IF EXISTS rowsAffected")
assertNoError(t, err)
_, err = db.ExecContext(context.Background(), "CREATE TABLE rowsAffected(ID INT)")
assertNoError(t, err)
{
r, err := db.ExecContext(context.Background(), "INSERT INTO rowsAffected(ID) VALUES(?)", 42)
assertNoError(t, err)
n, err := r.RowsAffected()
assertNoError(t, err)
if n != int64(1) {
t.Fatalf("RowsAffected() expected 1, got %v", n)
}
}
{
r, err := db.ExecContext(context.Background(), "UPDATE rowsAffected SET ID = ? WHERE ID = ?", 3, 9)
assertNoError(t, err)
n, err := r.RowsAffected()
assertNoError(t, err)
if n != int64(0) {
t.Fatalf("RowsAffected() expected int64(0), got %T(%v)", n, n)
}
}
{
r, err := db.ExecContext(context.Background(), "UPDATE rowsAffected SET ID = ? WHERE ID = ?", 3, 42)
assertNoError(t, err)
n, err := r.RowsAffected()
assertNoError(t, err)
if n != int64(1) {
t.Fatalf("RowsAffected() expected int64(0), got %T(%v)", n, n)
}
}
}
func TestMultipleResultsets(t *testing.T) {
const (
A int64 = 42
B = "testing"
)
var (
a int64
b string
)
db := NewDB(t)
defer db.Close()
rows, err := db.Query("CALL spMultipleResultsets(?, ?)", A, B)
assertNoError(t, err)
if !rows.Next() {
t.Fatal("rows.Next() returned false, expected true")
}
assertNoError(t, rows.Scan(&a))
if a != A {
t.Fatalf("expected %q got %T(%q)", A, a, a)
}
if rows.Next() {
t.Fatal("rows.Next() returned true, expected false")
}
if !rows.NextResultSet() {
t.Fatal("rows.NextResultSet() returned false, expected true")
}
if !rows.Next() {
t.Fatal("rows.Next() returned false, expected true")
}
assertNoError(t, rows.Scan(&b))
if b != B {
t.Fatalf("expected %q got %T(%q)", B, b, b)
}
if rows.Next() {
t.Fatal("rows.Next() returned true, expected false")
}
assertNoError(t, rows.Close())
}
func TestBeginTx(t *testing.T) {
// t.Skip("Can not determine current transaction's isolation level: https://bugs.mysql.com/bug.php?id=53341")
isos := []sql.IsolationLevel{
sql.LevelDefault,
sql.LevelReadUncommitted,
sql.LevelReadCommitted,
sql.LevelRepeatableRead,
sql.LevelSnapshot,
sql.LevelSerializable,
}
db := NewDB(t)
defer db.Close()
for _, level := range isos {
{
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: level})
assertNoError(t, err)
tx.Rollback()
}
{
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: level, ReadOnly: true})
assertNoError(t, err)
tx.Rollback()
}
}
}
/*
func TestQueryTimeout(t *testing.T) {
db := NewDB(t)
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
_, err := db.ExecContext(ctx, "SELECT SLEEP(3)")
if err != context.DeadlineExceeded {
t.Errorf("ExecContext expected to fail with DeadlineExceeded but it returned %v", err)
}
{
var val int64
rows, err := db.Query("SELECT 42")
requireNoError(t, err)
require.True(t, rows.Next())
requireNoError(t, rows.Scan(&val))
}
}
*/