-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_test.go
79 lines (76 loc) · 1.76 KB
/
mysql_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
package gormx
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestMysql(t *testing.T) {
var (
cfg = &MysqlConfig{
User: "root",
Password: "root",
Addr: "127.0.0.1",
Dbname: "mysql",
}
errCfg = &MysqlConfig{
User: "aaa",
Password: "bbb",
Addr: "127.0.0.1",
Dbname: "mysql",
}
)
Convey("with config", t, func() {
m := NewMysql(cfg)
So(m, ShouldNotBeNil)
})
Convey("without config", t, func() {
m := NewMysql(nil)
So(m, ShouldNotBeNil)
})
Convey("not initialized", t, func() {
m := &Mysql{}
db := m.Open()
So(db, ShouldBeNil)
})
Convey("connect failed", t, func() {
m := NewMysql(errCfg)
db := m.Open()
So(m, ShouldNotBeNil)
So(db, ShouldBeNil)
})
Convey("normal", t, func() {
m := NewMysql(nil)
db := m.Open()
So(m, ShouldNotBeNil)
So(db, ShouldNotBeNil)
})
Convey("open with params", t, func() {
m := NewMysql(nil)
db := m.Open("mysql", "root", "root")
So(m, ShouldNotBeNil)
So(db, ShouldNotBeNil)
})
Convey("open with invalid params", t, func() {
m := NewMysql(nil)
db := m.Open("mysql", "root", "root")
db1 := m.Open("mysql", "root")
db2 := m.Open("mysql", "root", "root", "localhost")
db3 := m.Open("mysql", "root", "root", "localhost", "foo")
So(m, ShouldNotBeNil)
So(db, ShouldNotBeNil)
So(db1, ShouldBeNil)
So(db2, ShouldNotBeNil)
So(db3, ShouldBeNil)
})
Convey("get gorm singleton", t, func() {
m := NewMysql(nil)
db := m.DB()
So(m, ShouldNotBeNil)
So(db, ShouldNotBeNil)
})
Convey("get dsn string", t, func() {
cfg := DefaultMysqlConfig
dsn := mysqlDsn(cfg.User, cfg.Password, "tcp", cfg.Addr, cfg.Dbname)
expect := "root:root@tcp(127.0.0.1)/mysql?charset=utf8mb4&parseTime=True&loc=Local"
So(dsn, ShouldEqual, expect)
})
}