-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
223 lines (160 loc) · 6.34 KB
/
test.py
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
import datetime
import telegram
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
)
from main import (
process_location,
location,
process_list_groups,
list_groups,
link_group,
delete_group_link,
)
# Location
def test_process_location(mocker):
mocker.patch("redis_client.set_location", return_value=1)
mocker.patch("telegram.Message.reply_text", return_value="Hello World")
message = telegram.Message(
message_id=123,
date=datetime.date(2021, 1, 1),
chat=telegram.Chat(id=456, type="simple_chat"),
location=telegram.Location(56.311527, 38.135115),
from_user=telegram.User(666, "test_user", False),
)
update = telegram.Update(update_id=1, message=message)
resp = process_location(update, None)
print("process_location result => \n{}\n".format(resp))
assert resp == "Hello World"
assert resp != "0"
def test_process_1(mocker):
mocker.patch("redis_client.set_location", return_value=1)
chat_id = 123
longitude = 56.311527
latitude = 38.135115
msg = location(chat_id, longitude, latitude)
print("location message => \n{}\n".format(msg))
assert msg is not None
def test_process_0(mocker):
mocker.patch("redis_client.set_location", return_value=0)
chat_id = 123
longitude = 56.311527
latitude = 38.135115
msg = location(chat_id, longitude, latitude)
print("location message => \n{}\n".format(msg))
assert not msg.__contains__("Error")
def test_process_negative(mocker):
mocker.patch("redis_client.set_location", return_value=-1)
chat_id = 123
longitude = 56.311527
latitude = 38.135115
msg = location(chat_id, longitude, latitude)
print("location message => \n{}\n".format(msg))
assert msg.__contains__("Error")
# List Groups
def test_list_groups(mocker):
mocker.patch("redis_client.get_location", return_value=[56.311527, 38.135115])
mocker.patch(
"redis_client.search_groups_within_radius",
return_value=[[100, "group1"], [200, "group2"]],
)
mocker.patch("redis_client.get_description", return_value="cool group")
chat_id = 456
radius = 100
resp = list_groups(chat_id=chat_id, radius=radius)
print("\ntest_list_groups response => {}\n".format(resp))
assert resp.__contains__("group1")
assert resp.__contains__("group2")
assert not resp.__contains__("group3")
def test_list_when_groups_get_location_0(mocker):
mocker.patch("redis_client.get_location", return_value=0)
mocker.patch(
"redis_client.search_groups_within_radius",
return_value=[[100, "group1"], [200, "group2"]],
)
mocker.patch("redis_client.get_description", return_value="cool group")
chat_id = 456
radius = 100
resp = list_groups(chat_id=chat_id, radius=radius)
print("\ntest_list_groups response => {}\n".format(resp))
assert resp.__contains__("Error")
assert not resp.__contains__("group1")
assert not resp.__contains__("group2")
def test_list_when_groups_search_groups_within_radius_0(mocker):
mocker.patch("redis_client.get_location", return_value=[56.311527, 38.135115])
mocker.patch(
"redis_client.search_groups_within_radius",
return_value=0,
)
mocker.patch("redis_client.get_description", return_value="cool group")
chat_id = 456
radius = 100
resp = list_groups(chat_id=chat_id, radius=radius)
print("\ntest_list_groups response => {}\n".format(resp))
assert resp.__contains__("Error")
assert not resp.__contains__("group1")
assert not resp.__contains__("group2")
# Link group
def test_link_group(mocker):
mocker.patch("redis_client.get_location", return_value=[56.311527, 38.135115])
mocker.patch("redis_client.link_group", return_value=1)
chat_id = 123
group = "test_group"
description = "desc"
resp = link_group(chat_id, group, description)
print("test_link_group => {}".format(resp))
assert not resp.__contains__("Error")
assert resp.__contains__("linked")
def test_link_group_when_no_location_found(mocker):
mocker.patch("redis_client.get_location", return_value=[])
mocker.patch("redis_client.link_group", return_value=1)
chat_id = 123
group = "test_group"
description = "desc"
resp = link_group(chat_id, group, description)
print("\ntest_link_group_when_no_location_found => {}".format(resp))
assert resp.__contains__("Error")
def test_link_group_when_error_has_occured(mocker):
mocker.patch("redis_client.get_location", return_value=[56.311527, 38.135115])
mocker.patch("redis_client.link_group", return_value=-1)
chat_id = 123
group = "test_group"
description = "desc"
resp = link_group(chat_id, group, description)
print("\ntest_link_group_when_error_has_occured => {}".format(resp))
assert resp.__contains__("Error while creating")
def test_link_group_when_group_already_exists(mocker):
mocker.patch("redis_client.get_location", return_value=[56.311527, 38.135115])
mocker.patch("redis_client.link_group", return_value=0)
chat_id = 123
group = "test_group"
description = "desc"
resp = link_group(chat_id, group, description)
print("\ntest_link_group_when_group_already_exists => {}".format(resp))
assert resp.__contains__("already exists")
# Delete group link
def test_delete_group_link_when_group_exists(mocker):
mocker.patch("redis_client.delete_group_link", return_value=1)
admin_id = 123
group_name = "test_group"
resp = delete_group_link(admin_id, group_name)
print("\ntest_delete_group_link_when_group_exists => {}".format(resp))
assert not resp.__contains__("Error")
assert resp.__contains__("deleted")
def test_delete_group_link_when_group_is_not_exists(mocker):
mocker.patch("redis_client.delete_group_link", return_value=0)
admin_id = 123
group_name = "test_group"
resp = delete_group_link(admin_id, group_name)
print("\ntest_delete_group_link_when_group_is_not_exists => {}".format(resp))
assert resp.__contains__("Error")
def test_delete_group_link_when_error(mocker):
mocker.patch("redis_client.delete_group_link", return_value=-1)
admin_id = 123
group_name = "test_group"
resp = delete_group_link(admin_id, group_name)
print("\ntest_delete_group_link_when_error => {}".format(resp))
assert resp.__contains__("Error")