forked from mehdisadeghi/clashogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_everything.py
351 lines (273 loc) · 11.9 KB
/
test_everything.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'''Clashogram tests.'''
import os
import json
import gettext
import unittest
from unittest.mock import MagicMock
from clashogram.__main__ import WarMonitor
from clashogram.models import WarStats, ClanInfo, WarInfo, WarStats
from clashogram.formatters import MessageFactory
from clashogram.api import CoCAPI
from clashogram.notifiers import TelegramNotifier
class ClanInfoTestCase(unittest.TestCase):
def setUp(self):
self.claninfo = ClanInfo({'location': {'name': 'Iran',
'isCountry': 'true',
'countryCode': 'IR'},
'warWinStreak': 0,
'isWarLogPublic': True})
def test_location(self):
assert self.claninfo.location == 'Iran'
def test_notset_location(self):
claninfo = ClanInfo({})
assert claninfo.location == ''
assert claninfo.country_flag_imoji == ''
def test_country_imoji(self):
assert self.claninfo.country_flag_imoji == '🇮🇷'
def test_winstreak(self):
assert self.claninfo.winstreak == 0
def test_is_warlog_public(self):
assert self.claninfo.is_warlog_public == True
class WarInfoTestCase(unittest.TestCase):
def setUp(self):
self.warinfo = WarInfo(
json.loads(open(os.path.join('data', 'inWar_40.json'),
'r', encoding='utf8').read()))
self.op_member = {
"tag": "#2GCR2YLP8",
"name": "captain spock",
"townhallLevel": 9,
"mapPosition": 18,
"opponentAttacks": 2,
"bestOpponentAttack": {
"attackerTag": "#G0QPL0LQ",
"defenderTag": "#2GCR2YLP8",
"stars": 3,
"destructionPercentage": 100,
"order": 78
}
}
self.clan_member = {
"tag": "#9QVR8R29C",
"name": "VAHID",
"townhallLevel": 7,
"mapPosition": 35,
"opponentAttacks": 2,
"bestOpponentAttack": {
"attackerTag": "#P0C92YP99",
"defenderTag": "#9QVR8R29C",
"stars": 3,
"destructionPercentage": 100,
"order": 3
}
}
def test_start_time(self):
assert self.warinfo.start_time == '20170603T191148.000Z'
def test_team_size(self):
assert self.warinfo.team_size == 40
def test_get_ordered_attacks(self):
ordered_attacks = self.warinfo.get_ordered_attacks()
assert len(ordered_attacks) == 126
def test_player_count(self):
assert len(self.warinfo.players) == 80
def test_get_player_attacks(self):
player = self.warinfo.players['#2GCR2YLP8']
assert self.warinfo.get_player_attacks(player) == []
def test_get_player_info(self):
with self.assertRaises(Exception):
self.warinfo.players['#2GCZZZZP8']
def test_is_not_in_war(self):
assert not self.warinfo.is_not_in_war()
def test_is_in_preparation(self):
assert not self.warinfo.is_in_preparation()
def test_is_in_war(self):
assert self.warinfo.is_in_war()
def test_is_war_over(self):
assert not self.warinfo.is_war_over()
def test_is_clan_member(self):
self.assertFalse(self.warinfo.is_clan_member(self.op_member))
self.assertTrue(self.warinfo.is_clan_member(self.clan_member))
def test_is_win(self):
self.assertTrue(self.warinfo.is_win())
def test_is_draw(self):
self.assertFalse(self.warinfo.is_draw())
def test_create_war_id(self):
self.assertEqual(self.warinfo.create_war_id(),
"#YVL0C8UY#JC0L922Y20170602T201148.000Z")
class WarInfoNotInWarTestCase(unittest.TestCase):
def setUp(self):
self.warinfo = WarInfo(json.loads(
open(os.path.join('data', 'notInWar.json'),
'r', encoding='utf8').read()))
def test_clan_stats(self):
self.assertEqual(self.warinfo.clan_level, 0)
self.assertEqual(self.warinfo.clan_destruction, 0)
self.assertEqual(self.warinfo.clan_stars, 0)
self.assertEqual(self.warinfo.clan_attacks, 0)
def test_op_stats(self):
self.assertEqual(self.warinfo.op_level, 0)
self.assertEqual(self.warinfo.op_destruction, 0)
self.assertEqual(self.warinfo.op_stars, 0)
self.assertEqual(self.warinfo.op_attacks, 0)
def test_players(self):
self.assertEqual(self.warinfo.players, {})
class WarStatsTestCase(unittest.TestCase):
def setUp(self):
warinfo = WarInfo(json.loads(
open(os.path.join('data', 'warEnded_50.json'),
'r', encoding='utf8').read()))
self.stats = WarStats(warinfo)
self.attack161 = {
"destructionPercentage": 53,
"attackerTag": "#9YUVL0CU",
"order": 161,
"stars": 2,
"defenderTag": "#228U8G88L"
}
self.attack150 = {
"destructionPercentage": 100,
"attackerTag": "#2Q02GYCYV",
"order": 150,
"stars": 3,
"defenderTag": "#2Y0C8YPYU"
}
def test_first_attack_stats(self):
stats = self.stats.calculate_war_stats_sofar(1)
self.assertEqual(stats['clan_destruction'], 0)
self.assertEqual(stats['op_destruction'], 1.76)
self.assertEqual(stats['clan_stars'], 0)
self.assertEqual(stats['op_stars'], 2)
self.assertEqual(stats['clan_used_attacks'], 0)
self.assertEqual(stats['op_used_attacks'], 1)
def test_42th_attack_stats(self):
stats = self.stats.calculate_war_stats_sofar(42)
self.assertEqual(stats['clan_destruction'], 44.16)
self.assertEqual(stats['op_destruction'], 26.56)
self.assertEqual(stats['clan_stars'], 61)
self.assertEqual(stats['op_stars'], 37)
self.assertEqual(stats['clan_used_attacks'], 27)
self.assertEqual(stats['op_used_attacks'], 15)
def test_last_attack_stats(self):
stats = self.stats.calculate_war_stats_sofar(162)
self.assertEqual(stats['clan_destruction'], 96.72)
self.assertEqual(stats['op_destruction'], 98.90)
self.assertEqual(stats['clan_stars'], 142)
self.assertEqual(stats['op_stars'], 147)
self.assertEqual(stats['clan_used_attacks'], 87)
self.assertEqual(stats['op_used_attacks'], 75)
def test_attack_destruction(self):
self.assertEqual(
self.stats.get_attack_new_destruction(self.attack161), 0)
self.assertEqual(
self.stats.get_attack_new_destruction(self.attack150), 3)
def test_attack_new_stars(self):
self.assertEqual(self.stats.get_attack_new_stars(self.attack161), 0)
self.assertEqual(self.stats.get_attack_new_stars(self.attack150), 1)
class MessageFactoryTestCase(unittest.TestCase):
def setUp(self):
self.msg_factory = MessageFactory(None, None)
self.setlocale_en()
def _setlocale(self, language):
os.environ['LANGUAGE'] = language
gettext.bindtextdomain('messages',
localedir=os.path.join(os.curdir, 'locales'))
gettext.textdomain('messages')
def setlocale_en(self):
self._setlocale('en_US.UTF-8')
def setlocale_fa(self):
self._setlocale('fa_IR.UTF-8')
def test_format_time_default(self):
os.environ['LANG'] = 'en'
os.environ['LANGUAGE'] = 'en'
timestr = self.msg_factory.format_time('20170603T191148.000Z')
self.assertEqual(timestr, 'Sat, 03 Jun 2017 19:11:48')
def test_format_time_fa(self):
os.environ['LANG'] = 'fa'
timestr = self.msg_factory.format_time('20170603T191148.000Z')
self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸')
def test_format_time_fa_IR(self):
os.environ['LANGUAGE'] = 'fa_IR'
timestr = self.msg_factory.format_time('20170603T191148.000Z')
self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸')
def test_format_time_fa_IR_locale(self):
self.setlocale_fa()
timestr = self.msg_factory.format_time('20170603T191148.000Z')
self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸')
class WarMonitorTestCase(unittest.TestCase):
def setUp(self):
coc_api = CoCAPI(None)
self.warinfo = self.get_warinfo()
our_claninfo = ClanInfo({'location': {'name': 'Iran',
'isCountry': 'true',
'countryCode': 'IR'},
'warWinStreak': 0})
coc_api.get_currentwar = MagicMock(return_value=self.warinfo)
coc_api.get_claninfo = MagicMock(return_value=our_claninfo)
notifier = TelegramNotifier(None, None)
notifier.send = MagicMock()
self.monitor = WarMonitor({}, coc_api, '', notifier)
self.monitor.update()
self.clan_attack = {
"attackerTag": "#98VVJ8LV8",
"defenderTag": "#8CCLRP2JC",
"stars": 3,
"destructionPercentage": 100,
"order": 10
}
def get_warinfo(self):
raise NotImplementedError()
class WarMonitorInWarTestCase(WarMonitorTestCase):
def get_warinfo(self):
return WarInfo(json.loads(
open(os.path.join('data', 'inWar_40.json'),
'r', encoding='utf8').read()))
def test_send_preparation_msg(self):
self.monitor.send_preparation_msg()
self.assertTrue(self.monitor.is_msg_sent('preparation_msg'))
self.assertTrue(self.monitor.is_msg_sent('players_msg'))
def test_send_war_msg(self):
self.monitor.send_war_msg()
self.assertTrue(self.monitor.is_msg_sent('war_msg'))
def test_is_attack_msg_sent(self):
self.assertTrue(self.monitor.is_msg_sent(
self.monitor.get_attack_id(self.clan_attack)))
def test_get_attack_id(self):
self.assertEqual(self.monitor.get_attack_id(self.clan_attack),
'attack98VVJ8LV88CCLRP2JC')
def test_is_war_over_msg_sent(self):
self.assertFalse(self.monitor.is_msg_sent('war_over_msg'))
def test_mark_msg_as_sent(self):
self.monitor.mark_msg_as_sent('my_msg')
self.assertTrue(self.monitor.is_msg_sent('my_msg'))
self.assertFalse(self.monitor.is_msg_sent('nonexistent_msg'))
def test_full_destruction_msg_sent(self):
self.assertFalse(self.monitor.is_msg_sent('clan_full_destruction'))
def test_op_destruction_msg_sent(self):
self.assertFalse(self.monitor.is_msg_sent('op_full_destruction'))
class WarMonitorFullDestructionTestCase(WarMonitorTestCase):
def get_warinfo(self):
return WarInfo(json.loads(
open(os.path.join('data', 'full_destruction.json'),
'r', encoding='utf8').read()))
def test_full_destruction_msg_sent(self):
self.assertTrue(self.monitor.is_msg_sent('clan_full_destruction'))
class WarMonitorOpFullDestructionTestCase(WarMonitorTestCase):
def get_warinfo(self):
return WarInfo(json.loads(
open(os.path.join('data', 'op_full_destruction.json'),
'r', encoding='utf8').read()))
def test_op_full_destruction_msg_sent(self):
self.assertTrue(self.monitor.is_msg_sent('op_full_destruction'))
class WarMonitorOnWarOverTestCase(WarMonitorTestCase):
def get_warinfo(self):
return WarInfo(json.loads(
open(os.path.join('data', 'warEnded_50.json'),
'r', encoding='utf8').read()))
def test_reset_on_ended_war(self):
with self.assertRaises(ValueError):
self.monitor.is_msg_sent('war_over_msg')
def test_is_war_over_msg_sent(self):
self.monitor.warinfo = self.warinfo
self.assertTrue(self.monitor.is_msg_sent('war_over_msg'))
if __name__ == '__main__':
unittest.main()