forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.go
106 lines (86 loc) · 2.96 KB
/
e2e_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
package driver_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestQuery(t *testing.T) {
mtb, records := personMemTable("db", "person")
db := sqlOpen(t, mtb, t.Name()+"?jsonAs=object")
var name, email string
var numbers interface{}
var created time.Time
var count int
cases := []struct {
Name, Query string
Pointers Pointers
Expect Records
}{
{"Select All", "SELECT * FROM db.person", []V{&name, &email, &numbers, &created}, records},
{"Select First", "SELECT * FROM db.person LIMIT 1", []V{&name, &email, &numbers, &created}, records.Rows(0)},
{"Select Name", "SELECT name FROM db.person", []V{&name}, records.Columns(0)},
{"Select Count", "SELECT COUNT(1) FROM db.person", []V{&count}, Records{{len(records)}}},
{"Insert", `INSERT INTO db.person VALUES ('foo', 'bar', '["baz"]', NOW())`, []V{}, Records{}},
{"Select Inserted", "SELECT name, email, phone_numbers FROM db.person WHERE name = 'foo'", []V{&name, &email, &numbers}, Records{{"foo", "bar", []V{"baz"}}}},
{"Update", "UPDATE db.person SET name = 'asdf' WHERE name = 'foo'", []V{}, Records{}},
{"Delete", "DELETE FROM db.person WHERE name = 'asdf'", []V{}, Records{}},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
rows, err := db.Query(c.Query)
require.NoError(t, err, "Query")
var i int
for ; rows.Next(); i++ {
require.NoError(t, rows.Scan(c.Pointers...), "Scan")
values := c.Pointers.Values()
if i >= len(c.Expect) {
t.Errorf("Got row %d, expected %d total: %v", i+1, len(c.Expect), values)
continue
}
assert.EqualValues(t, c.Expect[i], values, "Values")
}
require.NoError(t, rows.Err(), "Rows.Err")
if i < len(c.Expect) {
t.Errorf("Expected %d row(s), got %d", len(c.Expect), i)
}
})
}
}
func TestExec(t *testing.T) {
mtb, records := personMemTable("db", "person")
db := sqlOpen(t, mtb, t.Name())
cases := []struct {
Name, Statement string
RowsAffected int
}{
{"Insert", `INSERT INTO db.person VALUES ('asdf', 'qwer', '["zxcv"]', NOW())`, 1},
{"Update", "UPDATE db.person SET name = 'foo' WHERE name = 'asdf'", 1},
{"Delete", "DELETE FROM db.person WHERE name = 'foo'", 1},
{"Delete All", "DELETE FROM db.person WHERE LENGTH(name) < 100", len(records)},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
res, err := db.Exec(c.Statement)
require.NoError(t, err, "Exec")
count, err := res.RowsAffected()
require.NoError(t, err, "RowsAffected")
assert.EqualValues(t, c.RowsAffected, count, "RowsAffected")
})
}
errCases := []struct {
Name, Statement string
Error string
}{
{"Select", "SELECT * FROM db.person", "no result"},
}
for _, c := range errCases {
t.Run(c.Name, func(t *testing.T) {
res, err := db.Exec(c.Statement)
require.NoError(t, err, "Exec")
_, err = res.RowsAffected()
require.Error(t, err, "RowsAffected")
assert.Equal(t, c.Error, err.Error())
})
}
}