forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
112 lines (95 loc) · 2.76 KB
/
driver_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
package sqlmock
import (
"fmt"
"testing"
)
type void struct{}
func (void) Print(...interface{}) {}
func ExampleNew() {
db, mock, err := New()
if err != nil {
fmt.Println("expected no error, but got:", err)
return
}
defer db.Close()
// now we can expect operations performed on db
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
}
func TestShouldOpenConnectionIssue15(t *testing.T) {
db, mock, err := New()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
if len(pool.conns) != 1 {
t.Errorf("expected 1 connection in pool, but there is: %d", len(pool.conns))
}
smock, _ := mock.(*sqlmock)
if smock.opened != 1 {
t.Errorf("expected 1 connection on mock to be opened, but there is: %d", smock.opened)
}
// defer so the rows gets closed first
defer func() {
if smock.opened != 0 {
t.Errorf("expected no connections on mock to be opened, but there is: %d", smock.opened)
}
}()
mock.ExpectQuery("SELECT").WillReturnRows(NewRows([]string{"one", "two"}).AddRow("val1", "val2"))
rows, err := db.Query("SELECT")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
defer rows.Close()
mock.ExpectExec("UPDATE").WillReturnResult(NewResult(1, 1))
if _, err = db.Exec("UPDATE"); err != nil {
t.Errorf("unexpected error: %s", err)
}
// now there should be two connections open
if smock.opened != 2 {
t.Errorf("expected 2 connection on mock to be opened, but there is: %d", smock.opened)
}
mock.ExpectClose()
if err = db.Close(); err != nil {
t.Errorf("expected no error on close, but got: %s", err)
}
// one is still reserved for rows
if smock.opened != 1 {
t.Errorf("expected 1 connection on mock to be still reserved for rows, but there is: %d", smock.opened)
}
}
func TestTwoOpenConnectionsOnTheSameDSN(t *testing.T) {
db, mock, err := New()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
db2, mock2, err := New()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
if len(pool.conns) != 2 {
t.Errorf("expected 2 connection in pool, but there is: %d", len(pool.conns))
}
if db == db2 {
t.Errorf("expected not the same database instance, but it is the same")
}
if mock == mock2 {
t.Errorf("expected not the same mock instance, but it is the same")
}
}
func TestWrongDSN(t *testing.T) {
t.Parallel()
db, _, _ := New()
defer db.Close()
if _, err := db.Driver().Open("wrong_dsn"); err == nil {
t.Error("expected error on Open")
}
}
func TestNewDSN(t *testing.T) {
if _, _, err := NewWithDSN("sqlmock_db_99"); err != nil {
t.Errorf("expected no error on NewWithDSN, but got: %s", err)
}
}
func TestDuplicateNewDSN(t *testing.T) {
if _, _, err := NewWithDSN("sqlmock_db_1"); err == nil {
t.Error("expected error on NewWithDSN")
}
}