-
Notifications
You must be signed in to change notification settings - Fork 2
/
testsql_test.go
131 lines (104 loc) · 2.61 KB
/
testsql_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
package testsql
import (
"strings"
"testing"
"database/sql"
"github.com/go-sql-driver/mysql"
)
func newTS() *TestSQL {
dsn := "testsql:password@tcp(localhost:3306)/test_testsql"
tableSchemaPath := "./db/schema.sql"
dirPath := "./db/fixtures"
ts := New(dsn, tableSchemaPath, dirPath)
return ts
}
func TestTS_New(t *testing.T) {
dsn := "testsql:password@tcp(localhost:3306)/test_testsql"
tableSchemaPath := "./db/schema.sql"
dirPath := "./db/fixtures"
ts := New(dsn, tableSchemaPath, dirPath)
// check ts db name
cfg, err := mysql.ParseDSN(ts.Config.DSN)
if err != nil {
t.Errorf(err.Error())
}
if !strings.HasPrefix(cfg.DBName, "test_testsql") {
t.Errorf("db name error, %s not start with %s", cfg.DBName, "test_testsql")
}
// check fixture path
if ts.Config.FixtureDirPath != dirPath {
t.Errorf("fixture path error, %s != %s", ts.Config.FixtureDirPath, dirPath)
}
}
func TestTS_Exec(t *testing.T) {
ts := newTS()
sqlString := `
INSERT INTO users (id, name)
VALUES
(1, 'foo')
`
ts.Exec(sqlString)
var name string
err := ts.DB.QueryRow("select name from users limit 1").Scan(&name)
if err != nil {
t.Errorf(err.Error())
}
if name != "foo" {
t.Errorf("")
}
}
func TestTS_Use(t *testing.T) {
ts := newTS()
ts.Use("users.sql")
var name string
err := ts.DB.QueryRow("select name from users limit 1").Scan(&name)
if err != nil {
t.Errorf(err.Error())
}
if name != "foo" {
t.Errorf("")
}
}
func TestTS_Clear(t *testing.T) {
ts := newTS()
ts.Use("users.sql")
ts.Clear()
var name string
err := ts.DB.QueryRow("select name from users limit 1").Scan(&name)
if err == nil {
t.Errorf("TestTS Clear failed, get db result: %s", name)
}
if err.Error() != "sql: no rows in result set" {
t.Errorf("TestTS Clear failed, err: %s", err.Error())
}
}
func TestTS_DropTestDB(t *testing.T) {
ts := newTS()
// new sql.DB
conf, err := mysql.ParseDSN(ts.Config.DSN)
if err != nil {
t.Errorf(err.Error())
}
TestDBName := conf.DBName
conf.DBName = ""
dsn := conf.FormatDSN()
db, err := sql.Open("mysql", dsn)
if err != nil {
t.Errorf(err.Error())
}
// check if database exists
var name string
err = db.QueryRow("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", TestDBName).Scan(&name)
if err != nil {
t.Errorf(err.Error())
}
if name != TestDBName {
t.Errorf("TestTS DropTestDB failed, get db name: %s", name)
}
// call TS.DropTestDB and check again
ts.DropTestDB()
err = db.QueryRow("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", TestDBName).Scan(&name)
if err == nil {
t.Errorf("TestTS DropTestDB failed")
}
}