-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests.py
492 lines (358 loc) · 17.8 KB
/
tests.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import datetime
import tzlocal
import os
import random
import string
import requests
import unittest
import challonge
username = None
api_key = None
def _get_random_name():
return "pychal_" + "".join(random.choice(string.ascii_lowercase) for _ in range(0, 15))
class APITestCase(unittest.TestCase):
def test_set_credentials(self):
challonge.set_credentials(username, api_key)
self.assertEqual(challonge.api._credentials["user"], username)
self.assertEqual(challonge.api._credentials["api_key"], api_key)
def test_get_credentials(self):
challonge.api._credentials["user"] = username
challonge.api._credentials["api_key"] = api_key
self.assertEqual(challonge.get_credentials(), (username, api_key))
def test_get_local_timezone(self):
tz = challonge.get_timezone()
local_tz = tzlocal.get_localzone()
self.assertEqual(tz, local_tz)
def test_set_get_timezone(self):
test_tz = "Asia/Seoul"
challonge.set_timezone(test_tz)
tz = challonge.get_timezone()
self.assertEqual(str(tz), test_tz)
def test_call(self):
challonge.set_credentials(username, api_key)
self.assertNotEqual(challonge.fetch("GET", "tournaments"), "")
class TournamentsTestCase(unittest.TestCase):
def setUp(self):
challonge.set_credentials(username, api_key)
self.random_name = _get_random_name()
self.t = challonge.tournaments.create(self.random_name, self.random_name)
def tearDown(self):
challonge.tournaments.destroy(self.t["id"])
def test_index(self):
ts = challonge.tournaments.index()
ts = list(filter(lambda x: x["id"] == self.t["id"], ts))
self.assertEqual(len(ts), 1)
self.assertEqual(self.t, ts[0])
def test_index_filter_by_state(self):
ts = challonge.tournaments.index(state="pending")
ts = list(filter(lambda x: x["id"] == self.t["id"], ts))
self.assertEqual(len(ts), 1)
self.assertEqual(self.t, ts[0])
ts = challonge.tournaments.index(state="in_progress")
ts = list(filter(lambda x: x["id"] == self.t["id"], ts))
self.assertEqual(ts, [])
def test_index_filter_by_created(self):
ts = challonge.tournaments.index(
created_after=datetime.datetime.now().date() - datetime.timedelta(days=1)
)
ts = filter(lambda x: x["id"] == self.t["id"], ts)
self.assertTrue(self.t["id"] in map(lambda x: x["id"], ts))
def test_show(self):
self.assertEqual(challonge.tournaments.show(self.t["id"]), self.t)
def test_update_name(self):
challonge.tournaments.update(self.t["id"], name="Test!")
t = challonge.tournaments.show(self.t["id"])
self.assertEqual(t["name"], "Test!")
t.pop("name")
self.t.pop("name")
self.assertTrue(t["updated_at"] >= self.t["updated_at"])
t.pop("updated_at")
self.t.pop("updated_at")
self.assertEqual(t, self.t)
def test_update_private(self):
challonge.tournaments.update(self.t["id"], private=True)
t = challonge.tournaments.show(self.t["id"])
self.assertEqual(t["private"], True)
def test_update_type(self):
challonge.tournaments.update(self.t["id"], tournament_type="round robin")
t = challonge.tournaments.show(self.t["id"])
self.assertEqual(t["tournament_type"], "round robin")
def test_open(self):
challonge.tournaments.update(self.t["id"], prediction_method=1)
challonge.participants.create(self.t["id"], "#1")
challonge.participants.create(self.t["id"], "#2")
challonge.tournaments.open_for_predictions(self.t["id"])
t = challonge.tournaments.show(self.t["id"])
self.assertEqual(t["state"], "accepting_predictions")
def test_start(self):
# we have to add participants in order to start()
self.assertRaises(challonge.ChallongeException, challonge.tournaments.start, self.t["id"])
self.assertEqual(self.t["started_at"], None)
challonge.participants.create(self.t["id"], "#1")
challonge.participants.create(self.t["id"], "#2")
challonge.tournaments.start(self.t["id"])
t = challonge.tournaments.show(self.t["id"])
self.assertNotEqual(t["started_at"], None)
def test_finalize(self):
challonge.participants.create(self.t["id"], "#1")
challonge.participants.create(self.t["id"], "#2")
challonge.tournaments.start(self.t["id"])
ms = challonge.matches.index(self.t["id"])
self.assertEqual(ms[0]["state"], "open")
challonge.matches.update(
self.t["id"], ms[0]["id"], scores_csv="3-2,4-1,2-2", winner_id=ms[0]["player1_id"]
)
challonge.tournaments.finalize(self.t["id"])
t = challonge.tournaments.show(self.t["id"])
self.assertNotEqual(t["completed_at"], None)
def test_reset(self):
# have to add participants in order to start()
challonge.participants.create(self.t["id"], "#1")
challonge.participants.create(self.t["id"], "#2")
challonge.tournaments.start(self.t["id"])
# we can't add participants to a started tournament...
self.assertRaises(
challonge.ChallongeException, challonge.participants.create, self.t["id"], "name"
)
challonge.tournaments.reset(self.t["id"])
# but we can add participants to a reset tournament
p = challonge.participants.create(self.t["id"], "name")
challonge.participants.destroy(self.t["id"], p["id"])
class ParticipantsTestCase(unittest.TestCase):
def setUp(self):
challonge.set_credentials(username, api_key)
self.t_name = _get_random_name()
self.ps_names = [_get_random_name(), _get_random_name()]
self.t = challonge.tournaments.create(self.t_name, self.t_name)
self.ps = challonge.participants.bulk_add(self.t["id"], self.ps_names)
def tearDown(self):
challonge.tournaments.destroy(self.t["id"])
def test_index(self):
ps = challonge.participants.index(self.t["id"])
self.assertEqual(len(ps), 2)
self.assertTrue(self.ps[0] == ps[0] or self.ps[0] == ps[1])
self.assertTrue(self.ps[1] == ps[0] or self.ps[1] == ps[1])
def test_show(self):
p1 = challonge.participants.show(self.t["id"], self.ps[0]["id"])
self.assertEqual(p1["id"], self.ps[0]["id"])
def test_create(self):
new_player = challonge.participants.create(self.t["id"], _get_random_name())
res = challonge.participants.show(self.t["id"], new_player["id"])
self.assertEqual(res, new_player)
def test_create_with_number_names(self):
player_with_only_numbers_in_name = "".join([str(random.randint(0, 9)) for _ in range(0, 9)])
new_player = challonge.participants.create(self.t["id"], player_with_only_numbers_in_name)
res = challonge.participants.show(self.t["id"], new_player["id"])
self.assertEqual(res["name"], player_with_only_numbers_in_name)
def test_update(self):
challonge.participants.update(self.t["id"], self.ps[0]["id"], misc="Test!")
p1 = challonge.participants.show(self.t["id"], self.ps[0]["id"])
self.assertEqual(p1["misc"], "Test!")
self.ps[0].pop("misc")
p1.pop("misc")
self.assertTrue(p1["updated_at"] >= self.ps[0]["updated_at"])
self.ps[0].pop("updated_at")
p1.pop("updated_at")
self.assertEqual(self.ps[0], p1)
@unittest.skip("Skipping because of API Issues")
def test_check_in_and_undo_check_in(self):
timezone = challonge.get_timezone()
# Get the local time plus 30 minutes.
test_date = datetime.datetime.now(tz=timezone) + datetime.timedelta(minutes=30)
challonge.tournaments.update(self.t["id"], check_in_duration=30, start_at=test_date)
challonge.participants.check_in(self.t["id"], self.ps[0]["id"])
challonge.participants.check_in(self.t["id"], self.ps[1]["id"])
p1 = challonge.participants.show(self.t["id"], self.ps[0]["id"])
p2 = challonge.participants.show(self.t["id"], self.ps[1]["id"])
self.assertTrue(p1["checked_in"])
self.assertTrue(p2["checked_in"])
# check the undo process
challonge.participants.undo_check_in(self.t["id"], self.ps[0]["id"])
challonge.participants.undo_check_in(self.t["id"], self.ps[0]["id"])
p1 = challonge.participants.show(self.t["id"], self.ps[0]["id"])
p2 = challonge.participants.show(self.t["id"], self.ps[0]["id"])
self.assertFalse(p1["checked_in"])
self.assertFalse(p2["checked_in"])
def test_destroy_before_tournament_start(self):
# delete participant before the start of the tournament
challonge.participants.destroy(self.t["id"], self.ps[0]["id"])
p = challonge.participants.index(self.t["id"])
self.assertEqual(len(p), 1)
def test_destroy_after_tournament_start(self):
# delete participant after the start of the tournament
challonge.tournaments.start(self.t["id"])
challonge.participants.destroy(self.t["id"], self.ps[1]["id"])
p2 = challonge.participants.show(self.t["id"], self.ps[1]["id"])
self.assertFalse(p2["active"])
def test_randomize(self):
# randomize has a 50% chance of actually being different than
# current seeds, so we're just verifying that the method runs at all
challonge.participants.randomize(self.t["id"])
class MatchesTestCase(unittest.TestCase):
def setUp(self):
challonge.set_credentials(username, api_key)
self.t_name = _get_random_name()
self.t = challonge.tournaments.create(self.t_name, self.t_name)
self.ps = challonge.participants.bulk_add(
self.t["id"], [_get_random_name(), _get_random_name()]
)
challonge.tournaments.start(self.t["id"])
def tearDown(self):
challonge.tournaments.destroy(self.t["id"])
def test_index(self):
ms = challonge.matches.index(self.t["id"])
self.assertEqual(len(ms), 1)
m = ms[0]
ps = set((self.ps[0]["id"], self.ps[1]["id"]))
self.assertEqual(ps, set((m["player1_id"], m["player2_id"])))
self.assertEqual(m["state"], "open")
def test_show(self):
ms = challonge.matches.index(self.t["id"])
for m in ms:
self.assertEqual(m, challonge.matches.show(self.t["id"], m["id"]))
def test_update_reopen(self):
ms = challonge.matches.index(self.t["id"])
m = ms[0]
self.assertEqual(m["state"], "open")
challonge.matches.update(
self.t["id"], m["id"], scores_csv="3-2,4-1,2-2", winner_id=m["player1_id"]
)
m = challonge.matches.show(self.t["id"], m["id"])
self.assertEqual(m["state"], "complete")
challonge.matches.reopen(self.t["id"], m["id"])
m = challonge.matches.show(self.t["id"], m["id"])
self.assertEqual(m["state"], "open")
def test_mark_as_underway(self):
ms = challonge.matches.index(self.t["id"])
m = ms[0]
challonge.matches.mark_as_underway(self.t["id"], m["id"])
m = challonge.matches.show(self.t["id"], m["id"])
self.assertIsInstance(m["underway_at"], datetime.datetime)
def test_unmark_as_underway(self):
ms = challonge.matches.index(self.t["id"])
m = ms[0]
challonge.matches.mark_as_underway(self.t["id"], m["id"])
challonge.matches.unmark_as_underway(self.t["id"], m["id"])
m = challonge.matches.show(self.t["id"], m["id"])
self.assertIsNone(m["underway_at"])
class AttachmentsTestCase(unittest.TestCase):
def setUp(self):
challonge.set_credentials(username, api_key)
self.t_name = _get_random_name()
self.t = challonge.tournaments.create(self.t_name, self.t_name, accept_attachments=True)
self.ps = challonge.participants.bulk_add(
self.t["id"], [_get_random_name(), _get_random_name()]
)
challonge.tournaments.start(self.t["id"])
self.match = challonge.matches.index(self.t["id"])[0]
def tearDown(self):
challonge.tournaments.destroy(self.t["id"])
def test_index(self):
challonge.attachments.create(self.t["id"], self.match["id"], url="http://test.com")
challonge.attachments.create(self.t["id"], self.match["id"], url="http://test2.com")
a = challonge.attachments.index(self.t["id"], self.match["id"])
self.assertEqual(len(a), 2)
def test_create_url(self):
a = challonge.attachments.create(self.t["id"], self.match["id"], url="http://test.com")
self.assertEqual(a["url"], "http://test.com")
def test_create_description(self):
a = challonge.attachments.create(self.t["id"], self.match["id"], description="test text!")
self.assertEqual(a["description"], "test text!")
def test_create_url_with_description(self):
a = challonge.attachments.create(
self.t["id"], self.match["id"], url="http://test.com", description="just a test"
)
self.assertEqual(a["url"], "http://test.com")
self.assertEqual(a["description"], "just a test")
@unittest.skip("Skipping because of API Issues")
def test_create_file(self):
image = requests.get("http://lorempixel.com/300/300/")
a1 = challonge.attachments.create(self.t["id"], self.match["id"], asset=image)
a2 = challonge.attachments.show(self.t["id"], self.match["id"], a1["id"])
self.assertEqual(a1["asset"], a2["asset"])
@unittest.skip("Skipping because of API Issues")
def test_create_file_with_description(self):
image = requests.get("http://lorempixel.com/300/300/")
a1 = challonge.attachments.create(
self.t["id"], self.match["id"], asset=image, description="just a test"
)
a2 = challonge.attachments.show(self.t["id"], self.match["id"], a1["id"])
self.assertEqual(a1["asset"], a2["asset"])
def test_update_url(self):
a = challonge.attachments.create(self.t["id"], self.match["id"], url="http://test.com")
challonge.attachments.update(
self.t["id"], self.match["id"], a["id"], url="https://newtest.com"
)
a = challonge.attachments.show(self.t["id"], self.match["id"], a["id"])
self.assertEqual(a["url"], "https://newtest.com")
def test_update_description(self):
a = challonge.attachments.create(self.t["id"], self.match["id"], description="test text!")
challonge.attachments.update(
self.t["id"], self.match["id"], a["id"], description="This is an updated test!"
)
a = challonge.attachments.show(self.t["id"], self.match["id"], a["id"])
self.assertEqual(a["description"], "This is an updated test!")
def test_update_url_with_description(self):
a = challonge.attachments.create(
self.t["id"], self.match["id"], url="http://test.com", description="hello there!"
)
challonge.attachments.update(
self.t["id"],
self.match["id"],
a["id"],
url="http://newtest.com",
description="added a new url!",
)
a = challonge.attachments.show(self.t["id"], self.match["id"], a["id"])
self.assertEqual(a["url"], "http://newtest.com")
self.assertEqual(a["description"], "added a new url!")
@unittest.skip("Skipping because of API Issues")
def test_update_file(self):
image = requests.get("http://lorempixel.com/300/300/")
a1 = challonge.attachments.create(self.t["id"], self.match["id"], asset=image)
image = requests.get("http://lorempixel.com/300/300/")
challonge.attachments.update(self.t["id"], self.match["id"], a1["id"], asset=image)
a2 = challonge.attachments.show(self.t["id"], self.match["id"], a1["id"])
self.assertNotEqual(a1["asset"], a2["asset"])
@unittest.skip("Skipping because of API Issues")
def test_update_file_with_description(self):
image = requests.get("http://lorempixel.com/300/300/")
a1 = challonge.attachments.create(
self.t["id"], self.match["id"], asset=image, description="just a test"
)
image = requests.get("http://lorempixel.com/300/300/")
challonge.attachments.update(
self.t["id"], self.match["id"], a1["id"], asset=image, description="just a second test"
)
a2 = challonge.attachments.show(self.t["id"], self.match["id"], a1["id"])
self.assertNotEqual(a1["asset"], a2["asset"])
self.assertNotEqual(a1["description"], a2["description"])
@unittest.skip("Skipping because of API Issues")
def test_update_file_only_description(self):
image = requests.get("http://lorempixel.com/300/300/")
a1 = challonge.attachments.create(
self.t["id"], self.match["id"], asset=image, description="just a test"
)
image = requests.get("http://lorempixel.com/300/300/")
challonge.attachments.update(
self.t["id"], self.match["id"], a1["id"], description="just a second test"
)
a2 = challonge.attachments.show(self.t["id"], self.match["id"], a1["id"])
self.assertEqual(a1["asset"], a2["asset"])
self.assertNotEqual(a1["description"], a2["description"])
def test_destroy(self):
a = challonge.attachments.create(
self.t["id"], self.match["id"], url="http://test.com", description="just a test"
)
challonge.attachments.destroy(self.t["id"], self.match["id"], a["id"])
a = challonge.attachments.index(self.t["id"], self.match["id"])
self.assertEqual(a, [])
if __name__ == "__main__":
username = os.environ.get("CHALLONGE_USER")
api_key = os.environ.get("CHALLONGE_KEY")
if not username or not api_key:
raise RuntimeError(
"You must add CHALLONGE_USER and CHALLONGE_KEY \
to your environment variables to run the test suite"
)
unittest.main()