-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydb_test.go
259 lines (230 loc) · 6.04 KB
/
mydb_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package mydb
import (
"strconv"
"testing"
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
func TestDB_Ping(t *testing.T) {
masterDB, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica2, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1, replica2)
assert.Nil(t, err)
// Success test for ping
err = db.Ping()
assert.Nil(t, err)
// master db is closed
masterDB.Close()
err = db.Ping()
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "master's db ping fail: sql: database is closed")
// replica1 db and master db are closed
replica1.Close()
err = db.Ping()
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "master's db ping fail: sql: database is closed\nreplica db 1 ping fail: sql: database is closed")
}
func TestDB_Query(t *testing.T) {
masterDB, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, mock1, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica2, mock2, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1, replica2)
assert.Nil(t, err)
rows1 := sqlmock.NewRows([]string{"col1, col2"})
rows2 := sqlmock.NewRows([]string{"col3, col4"})
mock1.ExpectQuery("Query1").WillReturnRows(rows1)
mock2.ExpectQuery("Query1").WillReturnRows(rows2)
// Success case
rs, err := db.Query("Query1")
assert.Nil(t, err)
cols, err := rs.Columns()
assert.Nil(t, err)
assert.Equal(t, cols, []string{"col3, col4"})
rs, err = db.Query("Query1")
assert.Nil(t, err)
cols, err = rs.Columns()
assert.Nil(t, err)
assert.Equal(t, cols, []string{"col1, col2"})
// replica1 is closed or under maintanace
// This test always return ["col3, col4"] from replica 2
replica1.Close()
for index := 0; index < 5; index++ {
i := strconv.Itoa(index + 2)
mock2.ExpectQuery("Query" + i).WillReturnRows(rows2)
rs, err := db.Query("Query" + i)
assert.Nil(t, err)
cols, err := rs.Columns()
assert.Nil(t, err)
assert.Equal(t, cols, []string{"col3, col4"})
}
// replica 2 is also closed
// Now no replica is available to read so this test gives error
replica2.Close()
rs, err = db.Query("Query1")
assert.Nil(t, rs)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), noReplicaAvailableError)
}
func TestDB_QueryRow(t *testing.T) {
masterDB, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1)
assert.Nil(t, err)
rows := db.QueryRow("Query1")
assert.NotNil(t, rows)
}
func TestDB_Exec(t *testing.T) {
masterDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1)
assert.Nil(t, err)
// Succes case to insert data and return id 123, only 1 row is RowsAffected
mock.ExpectExec("Query1").WillReturnResult(sqlmock.NewResult(123, 1))
rs, err := db.Exec("Query1")
assert.Nil(t, err)
id, err := rs.LastInsertId()
assert.Nil(t, err)
assert.Equal(t, id, int64(123))
rowsAffected, err := rs.RowsAffected()
assert.Nil(t, err)
assert.Equal(t, rowsAffected, int64(1))
// master db is closed , which gives error
masterDB.Close()
mock.ExpectExec("Query2").WillReturnResult(sqlmock.NewResult(123, 1))
rs, err = db.Exec("Query2")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "sql: database is closed")
assert.Empty(t, rs)
}
func TestDB_Prepare(t *testing.T) {
masterDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, mock1, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica2, mock2, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1, replica2)
assert.Nil(t, err)
rows1 := sqlmock.NewRows([]string{"col1, col2"})
rows2 := sqlmock.NewRows([]string{"col3, col4"})
mock1.ExpectPrepare("Select1").ExpectQuery().WillReturnRows(rows1)
mock2.ExpectPrepare("Select1").ExpectQuery().WillReturnRows(rows2)
stmt, err := db.Prepare("Select1")
assert.Nil(t, err)
rs, err := stmt.Query()
assert.Nil(t, err)
cols, err := rs.Columns()
assert.Nil(t, err)
assert.Equal(t, cols, []string{"col3, col4"})
stmt, err = db.Prepare("Select1")
assert.Nil(t, err)
rs, err = stmt.Query()
assert.Nil(t, err)
cols, err = rs.Columns()
assert.Nil(t, err)
assert.Equal(t, cols, []string{"col1, col2"})
replica2.Close()
mock1.ExpectPrepare("Select2").ExpectQuery().WillReturnRows(rows1)
stmt, err = db.Prepare("Select2")
assert.Nil(t, err)
rs, err = stmt.Query()
assert.Nil(t, err)
cols, err = rs.Columns()
assert.Nil(t, err)
assert.Equal(t, cols, []string{"col1, col2"})
mock.ExpectPrepare("Insert").ExpectExec().WillReturnResult(sqlmock.NewResult(123, 1))
stmt, err = db.Prepare("Insert")
assert.Nil(t, err)
result, err := stmt.Exec()
assert.Nil(t, err)
id, err := result.LastInsertId()
assert.Nil(t, err)
assert.Equal(t, id, int64(123))
rowsAffected, err := result.RowsAffected()
assert.Nil(t, err)
assert.Equal(t, rowsAffected, int64(1))
}
func TestDB_New(t *testing.T) {
masterDB, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
_, err = New(masterDB, replica1)
assert.Nil(t, err)
_, err = New(masterDB)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), noReadReplicaError)
}
func TestDB_Close(t *testing.T) {
masterDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, mock1, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1)
assert.Nil(t, err)
mock.ExpectClose().WillReturnError(nil)
mock1.ExpectClose().WillReturnError(nil)
err = db.Close()
assert.Nil(t, err)
}
func TestDB_SetConnMaxLifetime(t *testing.T) {
masterDB, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
replica1, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
db, err := New(masterDB, replica1)
assert.Nil(t, err)
db.SetMaxIdleConns(100)
db.SetConnMaxLifetime(time.Hour * 24)
db.SetMaxOpenConns(30)
}