-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtests.py
More file actions
271 lines (222 loc) · 7.71 KB
/
tests.py
File metadata and controls
271 lines (222 loc) · 7.71 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
from typing import NoReturn
from unittest import (
TestCase, defaultTestLoader,
main as unitmain
)
from modules import (
BetterBotBaseDataService, DataBuilder,
VkInstance, Commands
)
import patterns
import config
def make_orderer():
order = {}
def ordered(f):
order[f.__name__] = len(order)
return f
def compare(a, b):
return [1, -1][order[a] < order[b]]
return ordered, compare
ordered, compare = make_orderer()
class Test1DataService(TestCase):
"""TestCase for commands.py
"""
db = BetterBotBaseDataService('test_db')
@ordered
def test_get_or_create_user(
self
) -> NoReturn:
user_1 = db.get_or_create_user(1, None)
user_2 = db.get_or_create_user(2, None)
assert user_1.name == 'Пользователь'
assert user_1.uid == 1
user_1.programming_languages = ['C#', 'C++', 'Java', 'Python']
user_1.github_profile = "Ethosa"
user_2.programming_languages = []
assert user_1.programming_languages == ['C#', 'C++', 'Java', 'Python']
user_1.karma = 100
user_2.karma = 9
user_2.supporters = [1]
db.save_user(user_1)
db.save_user(user_2)
@ordered
def test_get_users(
self
) -> NoReturn:
users = db.get_users([], None)
assert users == [{'uid': 1}, {'uid': 2}]
# this must sorts as 100 -> 0
users_sorted_by_karma = self.db.get_users(
other_keys=["karma"],
sort_key=lambda x: x["karma"]
)
assert users_sorted_by_karma[0] == {'karma': 100, 'uid': 1}
# this must sorts as 0 -> 100
users_sorted_by_karma_reversed = self.db.get_users(
other_keys=["karma"],
sort_key=lambda x: x["karma"],
reverse_sort=False
)
assert users_sorted_by_karma_reversed[0] == {'karma': 9, 'uid': 2}
class Test2DataBuilder(TestCase):
@ordered
def test_build_programming_languages(
self
) -> NoReturn:
programming_languages = DataBuilder.build_programming_languages(
db.get_user(1, None),
db
)
assert programming_languages == 'C#, C++, Java, Python'
programming_languages = DataBuilder.build_programming_languages(
db.get_user(2, None), db, default='отсутствуют'
)
assert programming_languages == 'отсутствуют'
@ordered
def test_build_github_profile(
self
) -> NoReturn:
user = db.get_user(1, None)
assert DataBuilder.build_github_profile(user, db) == f"github.com/{user.github_profile}"
@ordered
def test_build_karma(
self
) -> NoReturn:
user_1 = db.get_user(1)
user_2 = db.get_user(2)
print()
print(DataBuilder.build_karma(user_1, db))
print(DataBuilder.build_karma(user_2, db))
class Test3Commands(TestCase):
commands = Commands(VkInstance(), BetterBotBaseDataService("test_db"))
commands.peer_id = 2_000_000_001
commands.karma_enabled = True
@ordered
def test_help_message(
self
) -> NoReturn:
self.commands.help_message()
@ordered
def test_info_message(
self
) -> NoReturn:
self.commands.current_user = db.get_user(2)
self.commands.user = db.get_user(1)
self.commands.info_message()
@ordered
def test_update_command(
self
) -> NoReturn:
self.commands.update_command()
@ordered
def test_change_programming_language(
self
) -> NoReturn:
self.commands.msg = '+= c#'
self.commands.match_command(patterns.ADD_PROGRAMMING_LANGUAGE)
self.commands.change_programming_language(True)
self.commands.msg = '+= NeMeRlE'
self.commands.match_command(patterns.ADD_PROGRAMMING_LANGUAGE)
self.commands.change_programming_language(True)
self.commands.msg = '-= C#'
self.commands.match_command(patterns.REMOVE_PROGRAMMING_LANGUAGE)
self.commands.change_programming_language(False)
@ordered
def test_change_github_profile(
self
) -> NoReturn:
self.commands.msg = '+= github.com/ethosa'
self.commands.match_command(patterns.ADD_GITHUB_PROFILE)
self.commands.change_github_profile(True)
self.commands.msg = '-= github.com/ethosa'
self.commands.match_command(patterns.REMOVE_GITHUB_PROFILE)
self.commands.change_github_profile(False)
@ordered
def test_karma_message(
self
) -> NoReturn:
self.commands.karma_message()
self.commands.user = db.get_user(2)
self.commands.karma_message()
@ordered
def test_top(
self
) -> NoReturn:
self.commands.msg = 'top'
self.commands.match_command(patterns.TOP)
self.commands.top()
self.commands.top(True)
@ordered
def test_top_lang(
self
) -> NoReturn:
self.commands.msg = 'top c#'
self.commands.match_command(patterns.TOP_LANGUAGES)
self.commands.top_langs()
self.commands.top_langs(True)
self.commands.msg = 'bottom c#'
self.commands.match_command(patterns.BOTTOM_LANGUAGES)
self.commands.top_langs()
self.commands.top_langs(True)
@ordered
def test_apply_user_carma(
self
) -> NoReturn:
self.commands.user = db.get_user(1)
self.commands.apply_user_karma(self.commands.user, 5)
db.save_user(self.commands.user)
self.commands.karma_message()
@ordered
def test_apply_collective_vote(
self
) -> NoReturn:
self.commands.current_user = db.get_user(2)
self.commands.user = db.get_user(1)
self.commands.apply_collective_vote("opponents", config.NEGATIVE_VOTES_PER_KARMA, -1)
db.save_user(self.commands.user)
self.commands.karma_message()
@ordered
def test_apply_karma_change(
self
) -> NoReturn:
self.commands.apply_karma_change('-', 6)
self.commands.karma_message()
@ordered
def test_censored_words_rating_clean_message(
self
) -> NoReturn:
"""Test rating for clean message (+1)"""
self.commands.msg = "Hello, this is a clean message"
self.commands.current_user = db.get_user(1)
initial_rating = self.commands.current_user.censored_words_rating
self.commands.process_censored_words_rating()
# Should get +1 for clean message
assert self.commands.current_user.censored_words_rating == initial_rating + 1
@ordered
def test_censored_words_rating_with_censored_words(
self
) -> NoReturn:
"""Test rating for message with censored words (-1 per word)"""
self.commands.msg = "This is stupid shit"
self.commands.current_user = db.get_user(2)
initial_rating = self.commands.current_user.censored_words_rating
self.commands.process_censored_words_rating()
# Should get -2 for two censored words (stupid, shit)
assert self.commands.current_user.censored_words_rating == initial_rating - 2
@ordered
def test_censored_words_rating_message_display(
self
) -> NoReturn:
"""Test censored words rating message display"""
self.commands.user = db.get_user(1)
self.commands.from_id = 1
self.commands.censored_words_rating_message()
# Test for another user
self.commands.user = db.get_user(2)
self.commands.from_id = 1
self.commands.censored_words_rating_message()
if __name__ == '__main__':
db = BetterBotBaseDataService("test_db")
defaultTestLoader.sortTestMethodsUsing = compare
unitmain(verbosity=2)