forked from DATA-DOG/go-txdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_go18_test.go
64 lines (52 loc) · 1.32 KB
/
db_go18_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
// +build go1.8
package txdb
import (
"context"
"database/sql"
"testing"
_ "github.com/go-sql-driver/mysql"
)
func TestShouldGetMultiRowSet(t *testing.T) {
t.Parallel()
db, err := sql.Open("txdb", "multiRows")
if err != nil {
t.Fatalf("failed to open a mysql connection, have you run 'make test'? err: %s", err)
}
defer db.Close()
rows, err := db.QueryContext(context.Background(), "SELECT username FROM users; SELECT COUNT(*) FROM users;")
if err != nil {
t.Fatalf("failed to query users: %s", err)
}
defer rows.Close()
var users []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
t.Fatalf("unexpected row scan err: %v", err)
}
users = append(users, name)
}
if !rows.NextResultSet() {
t.Fatal("expected next result set")
}
if !rows.Next() {
t.Fatal("expected next result set - row")
}
var count int
if err := rows.Scan(&count); err != nil {
t.Fatalf("unexpected row scan err: %v", err)
}
if count != len(users) {
t.Fatal("unexpected number of users")
}
}
func TestShouldBeAbleToPingWithContext(t *testing.T) {
db, err := sql.Open("txdb", "ping")
if err != nil {
t.Fatalf("failed to open a mysql connection, have you run 'make test'? err: %s", err)
}
defer db.Close()
if err := db.PingContext(context.Background()); err != nil {
t.Fatal(err)
}
}