-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
191 lines (142 loc) · 3.98 KB
/
client_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
package tgo
import (
"errors"
"reflect"
"testing"
)
// TODO: test NewClient
// GetUpdates
func TestGetUpdates(t *testing.T) {
requestMaker := new(requestMakerMock)
c := NewClient("mockToken")
c.requestMaker = requestMaker
params := GetUpdatesParams{
Timeout: 60,
}
updates, err := c.GetUpdates(params)
if err != nil {
t.Fatal(err)
}
if !requestMaker.makeRequestCalled {
t.Fatal("makeRequest method not called as expected")
}
if requestMaker.passedMethod != "getUpdates" {
t.Fatal("passed method to requestMaker is different than expected")
}
if !reflect.DeepEqual(requestMaker.passedParams.(GetUpdatesParams), params) {
t.Fatal("passed params to requestMaker are different than expected")
}
if !reflect.DeepEqual(requestMaker.passedV.(*[]Update), updates) {
t.Fatal("passed v to requestMaker is different than expected")
}
}
func TestGetUpdatesForwardsRequestMakerErr(t *testing.T) {
requestMaker := &requestMakerMock{
errorToReturn: errors.New("mock error"),
}
c := NewClient("mockToken")
c.requestMaker = requestMaker
_, err := c.GetMe()
if err == nil || err.Error() != "mock error" {
t.Fatal("returned error is different than expected")
}
}
// GetMe
func TestGetMe(t *testing.T) {
requestMaker := new(requestMakerMock)
c := NewClient("mockToken")
c.requestMaker = requestMaker
user, err := c.GetMe()
if err != nil {
t.Fatal(err)
}
expectedUser := User{}
if *user != expectedUser {
t.Fatal("returned user is different than expected")
}
if !requestMaker.makeRequestCalled {
t.Fatal("makeRequest method not called as expected")
}
if requestMaker.passedMethod != "getMe" {
t.Fatal("passed method to requestMaker is different than expected")
}
}
func TestGetMeForwardsRequestMakerErr(t *testing.T) {
requestMaker := &requestMakerMock{
errorToReturn: errors.New("mock error"),
}
c := NewClient("mockToken")
c.requestMaker = requestMaker
params := GetUpdatesParams{
Timeout: 60,
}
_, err := c.GetUpdates(params)
if err == nil || err.Error() != "mock error" {
t.Fatal("returned error is different than expected")
}
}
// SendMessage
func TestSendMessage(t *testing.T) {
requestMaker := new(requestMakerMock)
c := NewClient("mockToken")
c.requestMaker = requestMaker
params := SendMessageParams{
ChatID: "mockChatID",
Text: "mock text",
}
message, err := c.SendMessage(params)
if err != nil {
t.Fatal(err)
}
if !requestMaker.makeRequestCalled {
t.Fatal("makeRequest method not called as expected")
}
if requestMaker.passedMethod != "sendMessage" {
t.Fatal("passed method to requestMaker is different than expected")
}
if !reflect.DeepEqual(requestMaker.passedParams.(SendMessageParams), params) {
t.Fatal("passed params to requestMaker are different than expected")
}
if !reflect.DeepEqual(requestMaker.passedV.(*Message), message) {
t.Fatal("passed v to requestMaker is different than expected")
}
}
func TestSendMessageForwardsRequestMakerErr(t *testing.T) {
requestMaker := &requestMakerMock{
errorToReturn: errors.New("mock error"),
}
c := NewClient("mockToken")
c.requestMaker = requestMaker
params := SendMessageParams{
ChatID: "mockChatID",
Text: "mock text",
}
_, err := c.SendMessage(params)
if err == nil || err.Error() != "mock error" {
t.Fatal("returned error is different than expected")
}
}
// TODO: test all client methods
// Mocks
type requestMakerMock struct {
makeRequestCalled bool
makeMultipartRequestCalled bool
passedMethod string
passedParams interface{}
passedV interface{}
errorToReturn error
}
func (rm *requestMakerMock) makeRequest(method string, params interface{}, v interface{}) error {
rm.makeRequestCalled = true
rm.passedMethod = method
rm.passedParams = params
rm.passedV = v
return rm.errorToReturn
}
func (rm *requestMakerMock) makeMultipartRequest(method string, params interface{}, v interface{}) error {
rm.makeMultipartRequestCalled = true
rm.passedMethod = method
rm.passedParams = params
rm.passedV = v
return rm.errorToReturn
}