-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathresponse_test.go
212 lines (194 loc) · 5.85 KB
/
response_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
package gomocket
import (
"database/sql"
"log"
"testing"
)
var DB *sql.DB
func GetUsers(db *sql.DB) []map[string]string {
var res []map[string]string
age := 27
rows, err := db.Query("SELECT name, age FROM users WHERE age=?", age)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var name string
var age string
var colsInResult string
resultColumns, _ := rows.Columns()
for i, col := range resultColumns {
if col == "name" && i == 0 {
colsInResult = "name-age"
} else {
colsInResult = "age-name"
}
break
}
if colsInResult == "name-age" {
if err := rows.Scan(&name, &age); err != nil {
log.Fatal(err)
}
} else {
if err := rows.Scan(&age, &name); err != nil {
log.Fatal(err)
}
}
row := map[string]string{"name": name, "age": age}
res = append(res, row)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return res
}
func GetUsersWithError(db *sql.DB) error {
age := 27
_, err := db.Query("SELECT name, age FROM users WHERE age=?", age)
return err
}
func CreateUsersWithError(db *sql.DB) error {
age := 27
_, err := db.Query("INSERT INTO users (age) VALUES (?) ", age)
return err
}
func InsertRecord(db *sql.DB) int64 {
res, err := db.Exec(`INSERT INTO foo VALUES("bar", ?)`, "value")
if err != nil {
return 0
}
id, _ := res.LastInsertId()
return id
}
func TestResponses(t *testing.T) {
Catcher.Register()
db, _ := sql.Open(DriverName, "connection_string") // Could be any connection string
DB = db
commonReply := []map[string]interface{}{{"name": "FirstLast", "age": "30"}}
t.Run("Simple SELECT caught by query", func(t *testing.T) {
Catcher.Logging = true
fr := Catcher.Reset().NewMock().WithQuery(`SELECT name, age FROM users WHERE`).WithReply(commonReply)
t.Log("result", fr)
result := GetUsers(DB)
t.Log("result", result)
if len(result) != 1 {
t.Fatalf("Returned sets is not equal to 1. Received %d", len(result))
}
if result[0]["name"] != "FirstLast" {
t.Errorf("Name is not equal. Got %v", result[0]["name"])
}
})
t.Run("Simple SELECT caught by query in strict mode", func(t *testing.T) {
Catcher.Logging = false
Catcher.Reset().NewMock().WithQuery(`SELECT name, age FROM users`).StrictMatch().WithReply(commonReply)
result := GetUsers(DB)
if len(result) != 0 {
t.Errorf("Returned sets is not equal to 0. Received %d", len(result))
}
Catcher.Reset().NewMock().WithQuery(`SELECT name, age FROM users`).WithReply(commonReply)
result = GetUsers(DB)
if len(result) != 1 {
t.Errorf("Returned sets is not equal to 1. Received %d", len(result))
}
})
t.Run("Simple SELECT with direct object", func(t *testing.T) {
t.Run("Not a once", func(t *testing.T) {
Catcher.Reset()
Catcher.Attach([]*FakeResponse{
{
Pattern: "SELECT name, age FROM users WHERE",
Response: commonReply,
Once: false,
},
})
fr := Catcher.FindResponse("SELECT name, age FROM users WHERE", nil)
t.Log("result", fr)
result := GetUsers(DB)
t.Log("result", result)
if len(result) != 1 {
t.Errorf("Returned sets is not equal to 1. Received %d", len(result))
}
if result[0]["name"] != "FirstLast" {
t.Errorf("Name is not equal. Got %v", result[0]["name"])
}
})
t.Run("Once", func(t *testing.T) {
Catcher.Reset()
Catcher.Attach([]*FakeResponse{
{
Pattern: "SELECT name, age FROM users WHERE",
Response: commonReply,
Once: true,
},
})
GetUsers(DB) // Trigger once to use this mock
result := GetUsers(DB) // trigger second time to receive empty results
if len(result) != 0 {
t.Errorf("Returned sets is not equal to 0. Received %d", len(result))
}
})
})
t.Run("Catch by arguments", func(t *testing.T) {
fr := Catcher.Reset().NewMock().WithArgs(int64(27)).WithReply(commonReply)
t.Log("result", fr)
result := GetUsers(DB)
t.Log("result", result)
if len(result) != 1 {
t.Fatalf("Returned sets is not equal to 1. Received %d", len(result))
}
if result[0]["age"] != "30" {
t.Errorf("Age is not equal. Got %v", result[0]["age"])
}
})
t.Run("Exceptions and Errors", func(t *testing.T) {
t.Run("Fire Query error", func(t *testing.T) {
Catcher.Reset().NewMock().WithArgs(int64(27)).WithReply(commonReply).WithQueryException()
err := GetUsersWithError(DB)
if err == nil {
t.Fatal("Error not triggered")
}
})
t.Run("Fire Execute error", func(t *testing.T) {
Catcher.Reset().NewMock().WithQuery("INSERT INTO users (age)").WithQueryException()
err := CreateUsersWithError(DB)
if err == nil {
t.Fatal("Error not triggered")
}
})
t.Run("Fire Execute error", func(t *testing.T) {
Catcher.Reset().NewMock().WithQuery("INSERT INTO users (age)").WithError(sql.ErrNoRows)
err := CreateUsersWithError(DB)
if err == nil || err != sql.ErrNoRows {
t.Fatal("Error not triggered")
}
})
})
t.Run("Last insert id", func(t *testing.T) {
var mockedID int64
mockedID = 64
Catcher.Reset().NewMock().WithQuery("INSERT INTO foo").WithID(mockedID)
returnedID := InsertRecord(DB)
if returnedID != mockedID {
t.Fatalf("Last insert id not returned. Expected: [%v] , Got: [%v]", mockedID, returnedID)
}
})
t.Run(`Recognise both ? and $1 Postgres placeholders for raw query`, func(t *testing.T) {
t.Run("Question mark", func(t *testing.T) {
testFunc := func(db *sql.DB) string {
var name string
err := db.QueryRow(`SELECT * FROM foo WHERE a = $1 AND b = $2 AND c = $3`, "value", "value2", "value3").Scan(&name)
if err != nil {
t.Fatalf("Test function failed [%v]", err)
return ""
}
return name
}
Catcher.Reset().NewMock().WithQuery("SELECT * FROM foo ").WithReply([]map[string]interface{}{{"name": "full_name"}})
returnedName := testFunc(DB)
if returnedName != "full_name" {
t.Fatalf("Returned name mismatches. Expected: [%v] , Got: [%v]", "full_name", returnedName)
}
})
})
}