This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
integration_test.go
244 lines (209 loc) · 6.16 KB
/
integration_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
package sqlgen
import (
"context"
"database/sql"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/samsarahq/thunder/batch"
"github.com/samsarahq/thunder/internal/testfixtures"
"github.com/stretchr/testify/assert"
)
func setup() (*testfixtures.TestDatabase, *DB, error) {
testDb, err := testfixtures.NewTestDatabase()
if err != nil {
return nil, nil, err
}
if _, err = testDb.Exec(`
CREATE TABLE users (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
uuid VARCHAR(255),
mood VARCHAR(255)
)
`); err != nil {
return nil, nil, err
}
schema := NewSchema()
schema.MustRegisterType("users", AutoIncrement, User{})
return testDb, NewDB(testDb.DB, schema), nil
}
type User struct {
Id int64 `sql:",primary"`
Name string
Uuid testfixtures.CustomType
Mood *testfixtures.CustomType
}
type Complex struct {
Id int64 `sql:",primary"`
Name string
Text []byte `sql:",string"`
Blob []byte `sql:",binary"`
Mappings map[string]string `sql:",json"`
}
func TestTagOverrides(t *testing.T) {
schema := NewSchema()
err := schema.RegisterType("complex", AutoIncrement, Complex{})
assert.NoError(t, err)
}
func TestContextDeadlineEnforced(t *testing.T) {
tdb, db, err := setup()
if err != nil {
t.Fatal(err)
}
defer tdb.Close()
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
if _, err = db.QueryExecer(ctx).ExecContext(ctx, "DO SLEEP(1)"); err == nil || err != context.DeadlineExceeded {
t.Errorf("Expected context.DeadlineExceeded, got: %s", err)
}
}
func TestIntegrationBasic(t *testing.T) {
tdb, db, err := setup()
if err != nil {
t.Fatal(err)
}
defer tdb.Close()
mood := testfixtures.CustomType{'f', 'o', 'o', 'o', 'o', 'o', 'o'}
if _, err := db.InsertRow(context.Background(), &User{Name: "Bob", Uuid: testfixtures.CustomType{'1', '1', '2', '3', '8', '4', '9', '1', '2', '9', '3'}, Mood: &mood}); err != nil {
t.Error(err)
}
var users []*User
if err := db.Query(context.Background(), &users, nil, nil); err != nil {
t.Error(err)
}
assert.Equal(t, []*User{
{
Id: 1,
Name: "Bob",
Uuid: testfixtures.CustomType{'1', '1', '2', '3', '8', '4', '9', '1', '2', '9', '3'},
Mood: &mood,
},
}, users)
}
// TestContextCancelBeforeRowsScan demonstrates we don't
// always get context.Canceled back from sql library. This
// affects our error handling and we need to be aware of it.
func TestContextCancelBeforeRowsScan(t *testing.T) {
testDb, err := testfixtures.NewTestDatabase()
if err != nil {
t.Fatal(err)
}
defer testDb.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rows, err := testDb.QueryContext(ctx, `select "foo"`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
// When we cancel the context after rows.Next() returns true,
// database/sql.(*Rows).initContextClose monitors the context
// and closes rows asynchronously, and subsequent rows.Scan()
// returns errors.New("sql: Rows are closed") instead of
// context.Canceled.
for rows.Next() {
cancel()
time.Sleep(1000 * time.Millisecond)
var foo string
err := rows.Scan(&foo)
// err is not context.Canceled.
if err == nil || err.Error() != "sql: Rows are closed" {
t.Fatalf("expecting 'sql: Rows are closed' from rows.Scan(), got %v", err)
}
}
if err := rows.Err(); err != context.Canceled {
t.Fatalf("expecting context.Canceled from rows.Err(), got %v", err)
}
}
// TestBatchFilter shows sqlgen batching matcher does not match if
// filter type does not exactly match column type.
func TestBatchFilter(t *testing.T) {
tdb, db, err := setup()
if err != nil {
t.Fatal(err)
}
defer tdb.Close()
if _, err := db.InsertRow(context.Background(), &User{
Id: 1,
Name: "Bob",
Uuid: testfixtures.CustomType{'1', '1', '2', '3', '8', '4', '9', '1', '2', '9', '3'},
Mood: &testfixtures.CustomType{'f', 'o', 'o', 'o', 'o', 'o', 'o'},
}); err != nil {
t.Fatal(err)
}
ctx := batch.WithBatching(context.Background())
var user *User
// Only int64 works because id field is int64.
if err := db.QueryRow(ctx, &user, Filter{"id": int64(1)}, nil); err != nil {
t.Fatal(err)
}
// Other int variants don't work.
if err := db.QueryRow(ctx, &user, Filter{"id": int32(1)}, nil); err != sql.ErrNoRows {
t.Fatalf("expecting sql.ErrNoRows, got: %v", err)
}
if err := db.QueryRow(ctx, &user, Filter{"id": int16(1)}, nil); err != sql.ErrNoRows {
t.Fatalf("expecting sql.ErrNoRows, got: %v", err)
}
if err := db.QueryRow(ctx, &user, Filter{"id": int8(1)}, nil); err != sql.ErrNoRows {
t.Fatalf("expecting sql.ErrNoRows, got: %v", err)
}
if err := db.QueryRow(ctx, &user, Filter{"id": int(1)}, nil); err != sql.ErrNoRows {
t.Fatalf("expecting sql.ErrNoRows, got: %v", err)
}
// Unsigned int does not work.
if err := db.QueryRow(ctx, &user, Filter{"id": uint(1)}, nil); err != sql.ErrNoRows {
t.Fatalf("expecting sql.ErrNoRows, got: %v", err)
}
// String does not work either.
if err := db.QueryRow(ctx, &user, Filter{"id": "1"}, nil); err != sql.ErrNoRows {
t.Fatalf("expecting sql.ErrNoRows, got: %v", err)
}
}
func Benchmark(b *testing.B) {
tdb, db, err := setup()
if err != nil {
b.Fatal(err)
}
defer tdb.Close()
ctx := context.Background()
mood := testfixtures.CustomType{'f', 'o', 'o', 'o', 'o', 'o', 'o'}
user := &User{
Id: 1,
Name: "Bob",
Uuid: testfixtures.CustomType{'1', '1', '2', '3', '8', '4', '9', '1', '2', '9', '3'},
Mood: &mood,
}
if _, err := db.InsertRow(ctx, user); err != nil {
b.Fatal(err)
}
benchmarks := []struct {
name string
fn func() error
}{
{"Read", func() error {
user := &User{}
return db.QueryRow(ctx, &user, nil, nil)
}},
{"Read_Where", func() error {
user := &User{}
return db.QueryRow(ctx, &user, Filter{"name": "Bob"}, nil)
}},
{"Create", func() error {
_, err := db.InsertRow(ctx, user)
return err
}},
{"Update", func() error { return db.UpdateRow(ctx, user) }},
{"Delete", func() error { return db.DeleteRow(ctx, user) }},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := bm.fn(); err != nil {
b.Error(err)
}
}
})
}
}