-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
89 lines (66 loc) · 1.52 KB
/
db_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
package db
import (
"errors"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
)
func Test_Unit_AddDbSuccess(t *testing.T) {
AddMock("test")
db := Get("test")
if db == nil {
t.Error(errors.New("db should not be nil"))
}
}
func Test_Unit_GetNilDb(t *testing.T) {
db := Get("test-nil")
if db != nil {
t.Error(errors.New("db should be nil"))
}
}
func Test_Unit_ConnNilDb(t *testing.T) {
db := Conn("test-nil2")
if db != nil {
t.Error(errors.New("db should be nil"))
}
}
func Test_Unit_AddDupilicateDbSuccess(t *testing.T) {
AddMock("test")
AddMock("test")
d := Get("test")
if d == nil {
t.Error(errors.New("db should not be nil"))
}
}
func Test_Unit_AddMySqlDbSuccess(t *testing.T) {
AddMySQL("test-mysql", "host", "1234", "name", "user", "pass")
d := Get("test-mysql")
if d == nil {
t.Error(errors.New("db should not be nil"))
}
}
func Test_Unit_AddPostgresDbSuccess(t *testing.T) {
AddPostgres("test-pg", "host", "1234", "name", "user", "pass", "disable")
d := Get("test-pg")
if d == nil {
t.Error(errors.New("db should not be nil"))
}
}
func Test_Unit_CheckDbSuccess(t *testing.T) {
AddMock("test-1")
_db := Get("test-1")
if _db == nil {
t.Error("_db is nil")
}
rows := sqlmock.NewRows([]string{"NOW()"}).
AddRow(time.Time{})
_db.Mock.ExpectQuery(`^SELECT (.+)`).
WillReturnRows(rows)
err := Check("test-1")
if err != nil {
t.Error("unexpected error %v", err)
}
if err := _db.Mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}