-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_test.go
199 lines (179 loc) · 3.64 KB
/
api_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
package govkbot
import (
"testing"
)
const (
wrongValueReturned = "Wrong value returned"
)
//func TestCall(t *testing.T) {
// r := SimpleResponse{}
// err := API.CallMethod("utils.getServerTime", H{}, &r)
// if err != nil {
// t.Error(err.Error())
// }
//}
//func TestVkAPI_Call(t *testing.T) {
// SetDebug(true)
// log.SetOutput(ioutil.Discard)
// r := SimpleResponse{}
// err := API.CallMethod("messages.get", H{}, &r)
// SetDebug(false)
// log.SetOutput(os.Stdout)
// if err == nil {
// t.Error("no error returned")
// }
//}
func TestNoJSON(t *testing.T) {
SetAPI("", "test", "")
r := SimpleResponse{}
err := API.CallMethod("nojson", H{}, &r)
if err == nil {
t.Error("no error returned")
}
if _, ok := err.(*ResponseError); !ok {
t.Error("wrong error type")
}
}
func TestVkError(t *testing.T) {
SetAPI("", "test", "")
r := SimpleResponse{}
err := API.CallMethod("vkerr", H{}, &r)
if err == nil {
t.Error("no error returned")
}
if _, ok := err.(*VKError); !ok {
t.Error("wrong error type")
}
}
func TestVkAPI_Me(t *testing.T) {
SetAPI("", "test", "")
me, err := API.Me()
if err != nil {
t.Error(err.Error())
}
if me.FullName() != "First Last" {
t.Error(me.FullName())
}
}
func TestVkAPI_GetChatInfo(t *testing.T) {
SetAPI("", "test", "")
chat, err := API.GetChatInfo(1)
if err != nil {
t.Error(err.Error())
}
if chat == nil {
t.Error("Chat info == nil")
}
}
func TestVkAPI_GetChatUsers(t *testing.T) {
SetAPI("", "test", "")
users, err := API.GetChatUsers(1)
if err != nil {
t.Error(err.Error())
}
if users == nil {
t.Error("users == nil")
}
}
func TestVkAPI_GetMessages(t *testing.T) {
SetAPI("", "test", "")
messages, err := API.GetMessages(100, 0)
if err != nil {
t.Error(err.Error())
}
if messages == nil {
t.Error("messages == nil")
}
}
func TestVkAPI_GetFriendRequests(t *testing.T) {
SetAPI("", "test", "")
_, err := API.GetFriendRequests(false)
if err != nil {
t.Error(err.Error())
}
}
func TestVkAPI_AddFriend(t *testing.T) {
SetAPI("", "test", "")
ok := API.AddFriend(1)
if !ok {
t.Error(wrongValueReturned)
}
}
func TestVkAPI_DeleteFriend(t *testing.T) {
SetAPI("", "test", "")
ok := API.DeleteFriend(1)
if !ok {
t.Error(wrongValueReturned)
}
}
func TestMessage_MarkAsRead(t *testing.T) {
SetAPI("", "test", "")
m := Message{}
err := m.MarkAsRead()
if err != nil {
t.Error(err.Error())
}
}
func TestVKBot_Reply(t *testing.T) {
SetAPI("", "test", "")
bot := API.NewBot()
m := Message{}
mid, err := bot.Reply(&m, Reply{Msg: "ok"})
if err != nil {
t.Error(err.Error())
}
if mid == 0 {
t.Error(wrongValueReturned)
}
m = Message{ChatID: 1}
mid, err = bot.Reply(&m, Reply{Msg: "ok"})
if err != nil {
t.Error(err.Error())
}
if mid == 0 {
t.Error(wrongValueReturned)
}
}
func TestVkAPI_SendChatMessage(t *testing.T) {
SetAPI("", "test", "")
_, err := API.SendChatMessage(1, "ok")
if err != nil {
t.Error(err.Error())
}
}
func TestVkAPI_SendMessage(t *testing.T) {
SetAPI("", "test", "")
_, err := API.SendMessage(1, "ok")
if err != nil {
t.Error(err.Error())
}
}
func TestVkAPI_User(t *testing.T) {
SetAPI("", "test", "")
u, err := API.User(1)
if err != nil {
t.Error(err.Error())
}
if u.ID == 0 {
t.Error(wrongValueReturned)
}
}
func TestVkAPI_NotifyAdmin(t *testing.T) {
SetAPI("", "test", "")
API.AdminID = 1
err := API.NotifyAdmin("ok")
if err != nil {
t.Error(err.Error())
}
}
func TestMessage_GetMentions(t *testing.T) {
testStr := "/who [id373336876|@sociobesed]"
m := Message{Body: testStr}
mentions := m.GetMentions()
if len(mentions) != 1 {
t.Error("wrong mentions count")
}
if mentions[0].ID != 373336876 {
t.Error("wrong mention")
}
}