forked from Cloud-CV/evalai-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_challenges.py
725 lines (568 loc) · 30.4 KB
/
test_challenges.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
import json
import responses
from beautifultable import BeautifulTable
from click.testing import CliRunner
from datetime import datetime
from dateutil import tz
from evalai.challenges import (challenge,
challenges)
from evalai.utils.urls import URLS
from evalai.utils.config import API_HOST_URL
from evalai.utils.common import (convert_UTC_date_to_local,
validate_date_format,
clean_data)
from tests.data import challenge_response, submission_response
from .base import BaseTestClass
class TestDisplayChallenges(BaseTestClass):
def setup(self):
challenge_data = json.loads(challenge_response.challenges)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value),
json=challenge_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.past_challenge_list.value),
json=challenge_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value),
json=challenge_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.future_challenge_list.value),
json=challenge_data, status=200)
challenges_json = challenge_data["results"]
self.output = ""
table = BeautifulTable(max_width=200)
attributes = ["id", "title", "short_description"]
columns_attributes = ["ID", "Title", "Short Description", "Creator", "Start Date", "End Date"]
table.column_headers = columns_attributes
for challenge_data in reversed(challenges_json):
values = list(map(lambda item: challenge_data[item], attributes))
creator = challenge_data["creator"]["team_name"]
start_date = convert_UTC_date_to_local(challenge_data["start_date"])
end_date = convert_UTC_date_to_local(challenge_data["end_date"])
values.extend([creator, start_date, end_date])
table.append_row(values)
self.output = str(table)
@responses.activate
def test_display_all_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges)
response = result.output.strip()
assert response == self.output
@responses.activate
def test_display_past_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges, ['past'])
response = result.output.strip()
assert response == self.output
@responses.activate
def test_display_ongoing_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges, ['ongoing'])
response = result.output.strip()
assert response == self.output
@responses.activate
def test_display_future_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges, ['future'])
response = result.output.strip()
assert response == self.output
class TestDisplayChallengeDetails(BaseTestClass):
def setup(self):
self.challenge_data = json.loads(challenge_response.challenge_details)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_details.value.format("1")),
json=self.challenge_data, status=200)
@responses.activate
def test_display_challenge_details(self):
table = BeautifulTable(max_width=200)
attributes = ["description", "submission_guidelines", "evaluation_details", "terms_and_conditions"]
column_attributes = ["Start Date", "End Date", "Description", "Submission Guidelines",
"Evaluation Details", "Terms and Conditions"]
table.column_headers = column_attributes
values = []
start_date = convert_UTC_date_to_local(self.challenge_data["start_date"]).split(" ")[0]
end_date = convert_UTC_date_to_local(self.challenge_data["end_date"]).split(" ")[0]
values.extend([start_date, end_date])
values.extend(list(map(lambda item: clean_data(self.challenge_data[item]), attributes)))
table.append_row(values)
expected = str(table)
runner = CliRunner()
result = runner.invoke(challenge, ["1"])
response = result.output.strip()
assert response == expected
class TestOngoingChallengesConditions(BaseTestClass):
@responses.activate
def test_display_ongoing_challenges_when_challenge_is_not_publicly_available(self):
challenge_data = json.loads(challenge_response.challenges)
for i in range(len(challenge_data["results"])):
challenge_data["results"][i]["published"] = False
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value),
json=challenge_data, status=200)
runner = CliRunner()
expected = "Sorry, no challenges found!"
result = runner.invoke(challenges, ['ongoing'])
assert result.output.strip() == expected
@responses.activate
def test_display_ongoing_challenges_when_challenge_is_not_approved_by_admin(self):
challenge_data = json.loads(challenge_response.challenges)
for i in range(len(challenge_data["results"])):
challenge_data["results"][i]["approved_by_admin"] = False
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value),
json=challenge_data, status=200)
runner = CliRunner()
expected = "Sorry, no challenges found!"
result = runner.invoke(challenges, ['ongoing'])
assert result.output.strip() == expected
@responses.activate
def test_display_ongoing_challenges_when_challenge_is_not_active(self):
challenge_data = json.loads(challenge_response.challenges)
for i in range(len(challenge_data["results"])):
challenge_data["results"][i]["end_date"] = "2017-06-18T20:00:00Z"
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value),
json=challenge_data, status=200)
runner = CliRunner()
expected = "Sorry, no challenges found!"
result = runner.invoke(challenges, ['ongoing'])
assert result.output.strip() == expected
class TestDisplayChallengesWithNoChallengeData(BaseTestClass):
def setup(self):
participant_team_data = json.loads(challenge_response.challenge_participant_teams)
host_team_data = json.loads(challenge_response.challenge_host_teams)
empty_leaderboard = json.loads(challenge_response.empty_leaderboard)
url = "{}{}"
challenges = '{"count": 2, "next": null, "previous": null,"results": []}'
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value),
json=json.loads(challenges), status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_teams.value),
json=participant_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.host_teams.value),
json=host_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_challenges.value).format("3"),
json=json.loads(challenges), status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.host_challenges.value).format("2"),
json=json.loads(challenges), status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_phase_split_detail.value).format("1"),
json=[], status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.leaderboard.value).format("1"),
json=empty_leaderboard, status=200)
self.output = "Sorry, no challenges found!\n"
@responses.activate
def test_display_all_challenge_lists_with_no_challenge_data(self):
runner = CliRunner()
result = runner.invoke(challenges)
response = result.output
assert response == self.output
@responses.activate
def test_display_host_challenge_list_with_no_challenge_data(self):
runner = CliRunner()
expected = "\nHosted Challenges\n\n"
self.output = "{}{}".format(expected, self.output)
result = runner.invoke(challenges, ['--host'])
response = result.output
assert response == self.output
@responses.activate
def test_display_participant_challenge_lists_with_no_challenge_data(self):
runner = CliRunner()
result = runner.invoke(challenges, ['--participant'])
response = result.output
assert response == self.output
@responses.activate
def test_display_participant_and_host_challenge_lists_with_no_challenge_data(self):
runner = CliRunner()
host_string = "\nHosted Challenges\n\n"
self.output = "{}{}{}".format(host_string, self.output, self.output)
result = runner.invoke(challenges, ['--participant', '--host'])
response = result.output
assert response == self.output
@responses.activate
def test_display_challenge_phase_splits_with_no_challenge_data(self):
runner = CliRunner()
result = runner.invoke(challenge, ['1', 'phase', '2', 'splits'])
response = result.output
assert response == "Sorry, no Challenge Phase Splits found.\n"
@responses.activate
def test_display_leaderboard_with_no_challenge_data(self):
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'leaderboard', '1'])
response = result.output.rstrip()
assert response == "Sorry, no Leaderboard results found."
class TestParticipantChallengesConditions(BaseTestClass):
@responses.activate
def test_display_participant_challenges_when_challenge_is_not_publicly_available(self):
challenge_data = json.loads(challenge_response.challenges)
participant_team_data = json.loads(challenge_response.challenge_participant_teams)
for i in range(len(challenge_data["results"])):
challenge_data["results"][i]["published"] = False
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_teams.value),
json=participant_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_challenges.value).format("3"),
json=challenge_data, status=200)
runner = CliRunner()
expected = "Sorry, no challenges found!"
result = runner.invoke(challenges, ['--participant'])
assert result.output.strip() == expected
@responses.activate
def test_display_participant_challenges_when_challenge_is_not_approved_by_admin(self):
challenge_data = json.loads(challenge_response.challenges)
participant_team_data = json.loads(challenge_response.challenge_participant_teams)
for i in range(len(challenge_data["results"])):
challenge_data["results"][i]["approved_by_admin"] = False
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_teams.value),
json=participant_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_challenges.value).format("3"),
json=challenge_data, status=200)
runner = CliRunner()
expected = "Sorry, no challenges found!"
result = runner.invoke(challenges, ['--participant'])
assert result.output.strip() == expected
@responses.activate
def test_display_participant_challenges_when_challenge_is_not_active(self):
challenge_data = json.loads(challenge_response.challenges)
participant_team_data = json.loads(challenge_response.challenge_participant_teams)
for i in range(len(challenge_data["results"])):
challenge_data["results"][i]["end_date"] = "2017-06-18T20:00:00Z"
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_teams.value),
json=participant_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_challenges.value).format("3"),
json=challenge_data, status=200)
runner = CliRunner()
expected = "Sorry, no challenges found!"
result = runner.invoke(challenges, ['--participant'])
assert result.output.strip() == expected
class TestParticipantOrHostTeamChallenges(BaseTestClass):
def setup(self):
challenge_data = json.loads(challenge_response.challenges)
host_team_data = json.loads(challenge_response.challenge_host_teams)
participant_team_data = json.loads(challenge_response.challenge_participant_teams)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_teams.value),
json=participant_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.host_teams.value),
json=host_team_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_challenges.value).format("3"),
json=challenge_data, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.host_challenges.value).format("2"),
json=challenge_data, status=200)
challenges_json = challenge_data["results"]
table = BeautifulTable(max_width=200)
attributes = ["id", "title", "short_description"]
columns_attributes = ["ID", "Title", "Short Description", "Creator", "Start Date", "End Date"]
table.column_headers = columns_attributes
for challenge_data in reversed(challenges_json):
values = list(map(lambda item: challenge_data[item], attributes))
creator = challenge_data["creator"]["team_name"]
start_date = convert_UTC_date_to_local(challenge_data["start_date"])
end_date = convert_UTC_date_to_local(challenge_data["end_date"])
values.extend([creator, start_date, end_date])
table.append_row(values)
self.output = str(table)
@responses.activate
def test_display_host_challenge_list(self):
runner = CliRunner()
expected = "\nHosted Challenges\n\n"
self.output = "{}{}".format(expected, self.output)
result = runner.invoke(challenges, ['--host'])
response = result.output.rstrip()
assert response == self.output
@responses.activate
def test_display_participant_challenge_lists(self):
runner = CliRunner()
expected = "\nParticipated Challenges\n\n"
self.output = "{}{}".format(expected, self.output)
result = runner.invoke(challenges, ['--participant'])
response = result.output.rstrip()
assert response == self.output
@responses.activate
def test_display_participant_and_host_challenge_lists(self):
runner = CliRunner()
participant_string = "\nParticipated Challenges\n\n"
host_string = "\nHosted Challenges\n\n"
self.output = "{}{}\n{}{}".format(host_string, self.output, participant_string, self.output)
result = runner.invoke(challenges, ['--participant', '--host'])
response = result.output.rstrip()
assert response == self.output
class TestDisplayChallengePhases(BaseTestClass):
def setup(self):
challenge_phase_list_json = json.loads(challenge_response.challenge_phase_list)
challenge_phase_details_json = json.loads(challenge_response.challenge_phase_details)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_phase_list.value).format('10'),
json=challenge_phase_list_json, status=200)
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_phase_detail.value).format('10', '20'),
json=challenge_phase_details_json, status=200)
self.phases = challenge_phase_list_json['results']
self.phase = challenge_phase_details_json
@responses.activate
def test_display_challenge_phase_list(self):
table = BeautifulTable(max_width=150)
attributes = ["id", "name", "challenge"]
columns_attributes = ["Phase ID", "Phase Name", "Challenge ID", "Description"]
table.column_headers = columns_attributes
for phase in self.phases:
values = list(map(lambda item: phase[item], attributes))
description = clean_data(phase["description"])
values.append(description)
table.append_row(values)
output = str(table)
runner = CliRunner()
result = runner.invoke(challenge, ['10', 'phases'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_display_challenge_phase_detail(self):
phase = self.phase
phase_title = "\n{}".format(phase["name"])
challenge_id = "Challenge ID: {}".format(str(phase["challenge"]))
phase_id = "Phase ID: {}\n\n".format(str(phase["id"]))
title = "{} {} {}".format(phase_title, challenge_id, phase_id)
description = "{}\n".format(phase["description"])
start_date = "Start Date : " + phase["start_date"].split("T")[0]
start_date = "\n{}\n".format(start_date)
end_date = "End Date : " + phase["end_date"].split("T")[0]
end_date = "\n{}\n".format(end_date)
max_submissions_per_day = "\nMaximum Submissions per day : {}\n".format(
str(phase["max_submissions_per_day"]))
max_submissions = "\nMaximum Submissions : {}\n".format(
str(phase["max_submissions"]))
codename = "\nCode Name : {}\n".format(
phase["codename"])
leaderboard_public = "\nLeaderboard Public : {}\n".format(
phase["leaderboard_public"])
is_active = "\nActive : {}\n".format(phase["is_active"])
is_public = "\nPublic : {}\n".format(phase["is_public"])
phase = "{}{}{}{}{}{}{}{}{}{}\n".format(title, description, start_date, end_date,
max_submissions_per_day, max_submissions, leaderboard_public,
codename, is_active, is_public)
runner = CliRunner()
result = runner.invoke(challenge, ['10', 'phase', '20'])
response = result.output
assert response == phase
@responses.activate
def test_display_challenge_phase_detail_with_json_flag(self):
expected = json.dumps(self.phase, indent=4, sort_keys=True)
runner = CliRunner()
result = runner.invoke(challenge, ['10', 'phase', '20', '--json'])
response = result.output.strip()
assert response == expected
class TestDisplaySubmission(BaseTestClass):
def setup(self):
json_data = json.loads(submission_response.submission)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.my_submissions.value).format("3", "7"),
json=json_data, status=200)
self.submissions = json_data["results"]
@responses.activate
def test_display_my_submission_details(self):
table = BeautifulTable(max_width=100)
attributes = ["id", "participant_team_name", "execution_time", "status"]
columns_attributes = ["ID", "Participant Team", "Execution Time(sec)", "Status", "Submitted At", "Method Name"]
table.column_headers = columns_attributes
for submission in self.submissions:
# Format date
date = datetime.strptime(submission['submitted_at'], "%Y-%m-%dT%H:%M:%S.%fZ")
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
# Convert to local timezone from UTC.
date = date.replace(tzinfo=from_zone)
converted_date = date.astimezone(to_zone)
date = converted_date.strftime('%D %r')
# Check for empty method name
method_name = submission["method_name"] if submission["method_name"] else "None"
values = list(map(lambda item: submission[item], attributes))
values.append(date)
values.append(method_name)
table.append_row(values)
output = str(table).rstrip()
runner = CliRunner()
result = runner.invoke(challenge, ['3', 'phase', '7', 'submissions'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_display_my_submission_details_with_single_argument(self):
output = ("Usage: challenge phase [OPTIONS] PHASE COMMAND [ARGS]...\n"
"\nError: Invalid value for \"PHASE\": submissions is not a valid integer\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'phase', 'submissions'])
response = result.output
assert response == output
@responses.activate
def test_display_my_submission_details_with_start_date(self):
table = BeautifulTable(max_width=100)
attributes = ["id", "participant_team_name", "execution_time", "status"]
columns_attributes = ["ID", "Participant Team", "Execution Time(sec)", "Status", "Submitted At", "Method Name"]
table.column_headers = columns_attributes
start_date = datetime.strptime('6/7/18', "%m/%d/%y")
end_date = datetime.max
for submission in self.submissions:
date = validate_date_format(submission['submitted_at'])
if (date >= start_date and date <= end_date):
# Check for empty method name
date = convert_UTC_date_to_local(submission['submitted_at'])
method_name = submission["method_name"] if submission["method_name"] else "None"
values = list(map(lambda item: submission[item], attributes))
values.append(date)
values.append(method_name)
table.append_row(values)
output = str(table).rstrip()
runner = CliRunner()
result = runner.invoke(challenge, ['3', 'phase', '7', 'submissions', '-s', '6/7/18'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_display_my_submission_details_with_end_date(self):
table = BeautifulTable(max_width=100)
attributes = ["id", "participant_team_name", "execution_time", "status"]
columns_attributes = ["ID", "Participant Team", "Execution Time(sec)", "Status", "Submitted At", "Method Name"]
table.column_headers = columns_attributes
start_date = datetime.min
end_date = datetime.strptime('6/7/18', "%m/%d/%y")
for submission in self.submissions:
date = validate_date_format(submission['submitted_at'])
if (date >= start_date and date <= end_date):
# Check for empty method name
date = convert_UTC_date_to_local(submission['submitted_at'])
method_name = submission["method_name"] if submission["method_name"] else "None"
values = list(map(lambda item: submission[item], attributes))
values.append(date)
values.append(method_name)
table.append_row(values)
output = str(table).rstrip()
runner = CliRunner()
result = runner.invoke(challenge, ['3', 'phase', '7', 'submissions', '-e', '6/7/18'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_display_my_submission_details_with_end_date_and_start_date(self):
table = BeautifulTable(max_width=100)
attributes = ["id", "participant_team_name", "execution_time", "status"]
columns_attributes = ["ID", "Participant Team", "Execution Time(sec)", "Status", "Submitted At", "Method Name"]
table.column_headers = columns_attributes
start_date = datetime.strptime('6/5/18', "%m/%d/%y")
end_date = datetime.strptime('6/9/18', "%m/%d/%y")
for submission in self.submissions:
date = validate_date_format(submission['submitted_at'])
if (date >= start_date and date <= end_date):
# Check for empty method name
date = convert_UTC_date_to_local(submission['submitted_at'])
method_name = submission["method_name"] if submission["method_name"] else "None"
values = list(map(lambda item: submission[item], attributes))
values.append(date)
values.append(method_name)
table.append_row(values)
output = str(table).rstrip()
runner = CliRunner()
result = runner.invoke(challenge, ['3', 'phase', '7', 'submissions', '-s', '6/5/18', '-e', '6/9/18'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_display_my_submission_details_with_end_date_and_start_date_without_submissions(self):
output = "Sorry, no submissions were made during this time period."
runner = CliRunner()
result = runner.invoke(challenge, ['3', 'phase', '7', 'submissions', '-s', '6/10/18', '-e', '6/15/18'])
response = result.output.strip()
assert response == output
class TestDisplayLeaderboard(BaseTestClass):
def setup(self):
json_data = json.loads(challenge_response.leaderboard)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.leaderboard.value).format("1"),
json=json_data, status=200)
self.leaderboard = json_data["results"]
@responses.activate
def test_display_leaderboard(self):
attributes = self.leaderboard[0]["leaderboard__schema"]["labels"]
table = BeautifulTable(max_width=150)
attributes = ["Rank", "Participant Team"] + attributes + ["Last Submitted"]
attributes = list(map(lambda item: str(item), attributes))
table.column_headers = attributes
for rank, result in enumerate(self.leaderboard, start=1):
name = result['submission__participant_team__team_name']
scores = result['result']
last_submitted = convert_UTC_date_to_local(result['submission__submitted_at'])
value = [rank, name] + scores + [last_submitted]
table.append_row(value)
output = str(table).rstrip()
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'leaderboard', '1'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_test_display_leaderboard_with_string_argument(self):
output = ("Usage: challenge leaderboard [OPTIONS] CPS\n"
"\nError: Invalid value for \"CPS\": two is not a valid integer\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'leaderboard', 'two'])
response = result.output
assert response == output
@responses.activate
def test_display_leaderboard_with_single_argument(self):
output = ("Usage: challenge leaderboard [OPTIONS] CPS\n"
"\nError: Missing argument \"CPS\".\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'leaderboard'])
response = result.output
assert response == output
class TestDisplayChallengePhaseSplit(BaseTestClass):
def setup(self):
json_data = json.loads(challenge_response.challenge_phase_splits)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_phase_split_detail.value).format("1"),
json=json_data, status=200)
self.splits = json_data
@responses.activate
def test_display_challenge_phase_split(self):
output = ""
table = BeautifulTable(max_width=100)
attributes = ["id", "dataset_split_name", "challenge_phase_name"]
columns_attributes = ["Challenge Phase ID", "Dataset Split", "Challenge Phase Name"]
table.column_headers = columns_attributes
for split in self.splits:
if split['visibility'] == 3:
values = list(map(lambda item: split[item], attributes))
table.append_row(values)
output = str(table)
runner = CliRunner()
result = runner.invoke(challenge, ['1', 'phase', '2', 'splits'])
response = result.output.rstrip()
assert response == output
@responses.activate
def test_display_challenge_phase_split_list_with_a_single_argument(self):
output = ("Usage: challenge phase [OPTIONS] PHASE COMMAND [ARGS]...\n"
"\nError: Missing argument \"PHASE\".\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'phase'])
response = result.output
assert response == output
@responses.activate
def test_display_my_submission_details_with_string_argument(self):
output = ("Usage: challenge phase [OPTIONS] PHASE COMMAND [ARGS]...\n"
"\nError: Invalid value for \"PHASE\": two is not a valid integer\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2', 'phase', 'two'])
response = result.output
assert response == output
class TestDisplaySubmissionWithoutSubmissionData(BaseTestClass):
def setup(self):
data = '{"count": 4, "next": null, "previous": null, "results": []}'
json_data = json.loads(data)
url = "{}{}"
responses.add(responses.GET, url.format(API_HOST_URL, URLS.my_submissions.value).format("3", "7"),
json=json_data, status=200)
@responses.activate
def test_display_my_submission_details_without_submissions(self):
expected = "\nSorry, you have not made any submissions to this challenge phase."
runner = CliRunner()
result = runner.invoke(challenge, ['3', 'phase', '7', 'submissions'])
response = result.output.rstrip()
assert response == expected
@responses.activate
def test_display_challenge_phase_split_list_with_string_argument(self):
output = ("Usage: challenge [OPTIONS] CHALLENGE COMMAND [ARGS]...\n"
"\nError: Invalid value for \"CHALLENGE\": two is not a valid integer\n")
runner = CliRunner()
result = runner.invoke(challenge, ['two', 'participate', '3'])
response = result.output
assert response == output