-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_test.go
More file actions
109 lines (98 loc) · 2.65 KB
/
basic_test.go
File metadata and controls
109 lines (98 loc) · 2.65 KB
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
package main
import (
"fmt"
"testing"
"github.com/DATA-DOG/go-sqlmock"
_ "github.com/go-sql-driver/mysql"
)
func Test_recordStats(t *testing.T) {
type args struct {
userID int64
productID int64
}
testArgs := args{userID: 2, productID: 3}
updateQueryRgx := `UPDATE products SET views = views \+ 1`
insertQueryRgx := `INSERT INTO product_viewers \(user_id, product_id\) VALUES \(\?, \?\)`
tests := []struct {
name string
mockClosure func(mock sqlmock.Sqlmock)
args args
wantErr bool
}{
{
name: "success case",
mockClosure: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(updateQueryRgx).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(insertQueryRgx).
WithArgs(testArgs.userID, testArgs.productID).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
},
args: testArgs,
wantErr: false,
},
{
name: "failure on begin",
mockClosure: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin().WillReturnError(fmt.Errorf("some error"))
},
args: testArgs,
wantErr: true,
},
{
name: "failure on update",
mockClosure: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(updateQueryRgx).
WillReturnError(fmt.Errorf("some error"))
mock.ExpectRollback()
},
args: testArgs,
wantErr: true,
},
{
name: "failure on insert",
mockClosure: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(updateQueryRgx).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(insertQueryRgx).
WithArgs(testArgs.userID, testArgs.productID).
WillReturnError(fmt.Errorf("some error"))
mock.ExpectRollback()
},
args: testArgs,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
// Running tests in parallel :)
t.Parallel()
// db and mock for current iteration
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
// Set mock expectations
tt.mockClosure(mock)
// Testing function
if err := recordStats(db, tt.args.userID, tt.args.productID); (err != nil) != tt.wantErr {
t.Errorf("recordStats() error = %v, wantErr %v", err, tt.wantErr)
}
// Closing mock connection
mock.ExpectClose()
// Explicit closing instead of deferred in order to check ExpectationsWereMet
if err = db.Close(); err != nil {
t.Error(err)
}
// Checking all expectations were met
if err = mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
})
}
}