Skip to content

Commit

Permalink
Add command to display a challenge details(Cloud-CV#68)
Browse files Browse the repository at this point in the history
* Pretty printing the challenges details

* Fix conflicts

* Fix conflicts

* Table format

* Fix format of challenge details

* Fix conflicts and tests
  • Loading branch information
guyandtheworld authored and RishabhJain2018 committed Jul 12, 2018
1 parent 98a3de9 commit f4f5d4e
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 13 deletions.
5 changes: 4 additions & 1 deletion evalai/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
display_ongoing_challenge_list,
display_past_challenge_list,
display_participated_or_hosted_challenges,
display_challenge_details,
display_challenge_phase_list,
display_challenge_phase_detail,
display_challenge_phase_split_list,
Expand Down Expand Up @@ -59,14 +60,16 @@ def challenges(ctx, participant, host):
display_all_challenge_list()


@click.group()
@click.group(invoke_without_command=True)
@click.pass_context
@click.argument('CHALLENGE', type=int)
def challenge(ctx, challenge):
"""
Display challenge specific details.
"""
ctx.obj = Challenge(challenge=challenge)
if ctx.invoked_subcommand is None:
display_challenge_details(challenge)


@challenges.command()
Expand Down
41 changes: 41 additions & 0 deletions evalai/utils/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,47 @@ def display_participated_or_hosted_challenges(is_host=False, is_participant=Fals
echo("Sorry, no challenges found!")


def pretty_print_challenge_details(challenge):
table = BeautifulTable(max_width=200)
attributes = ["description", "submission_guidelines", "evaluation_details", "terms_and_conditions"]
table.column_headers = ["Start Date", "End Date", "Description", "Submission Guidelines",
"Evaluation Details", "Terms and Conditions"]
values = []
start_date = convert_UTC_date_to_local(challenge["start_date"]).split(" ")[0]
end_date = convert_UTC_date_to_local(challenge["end_date"]).split(" ")[0]
values.extend([start_date, end_date])
values.extend(list(map(lambda item: clean_data(challenge[item]), attributes)))
table.append_row(values)
echo(table)


def display_challenge_details(challenge):
"""
Function to display challenge details.
"""
url = URLS.challenge_details.value
url = "{}{}".format(get_host_url(), url)
url = url.format(challenge)

header = get_request_header()
try:
response = requests.get(url, headers=header)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if (response.status_code in EVALAI_ERROR_CODES):
validate_token(response.json())
echo(style("Error: {}".format(response.json()["error"]), fg="red", bold=True))
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException as err:
echo(err)
sys.exit(1)

response = response.json()
pretty_print_challenge_details(response)


def pretty_print_all_challenge_phases(phases):
"""
Function to print all the challenge phases of a challenge
Expand Down
1 change: 1 addition & 0 deletions evalai/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class URLS(Enum):
challenge_list = "/api/challenges/challenge/all"
past_challenge_list = "/api/challenges/challenge/past"
future_challenge_list = "/api/challenges/challenge/future"
challenge_details = "/api/challenges/challenge/{}"
participant_teams = "/api/participants/participant_team"
host_teams = "/api/hosts/challenge_host_team/"
host_challenges = "/api/challenges/challenge_host_team/{}/challenge"
Expand Down
38 changes: 38 additions & 0 deletions tests/data/challenge_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,44 @@
"""


challenge_details = """
{
"allowed_email_domains": [],
"anonymous_leaderboard": false,
"approved_by_admin": true,
"blocked_email_domains": [],
"creator": {
"created_by": "host",
"id": 1,
"team_name": "Lake Cynthiabury Host Team",
"team_url": ""
},
"description": "Ex voluptatum accusantium dignissimos voluptatem eveniet enim non \
aspernatur. Expedita consequatur velit vitae enim. Vel asperiores deserunt suscipit \
non eaque veritatis labore. A atque illo fuga suscipit mollitia dignissimos assumenda.",
"enable_forum": true,
"end_date": "2019-11-11T06:31:31.594239Z",
"evaluation_details": "Perspiciatis harum molestias iste corporis \
aspernatur sit doloribus. Occaecati aliquid ullam odit aperiam in. Cupiditate consectetur \
ab doloremque dolore.",
"id": 1,
"image": null,
"is_active": true,
"published": true,
"short_description": "Nisi vero sint ipsam recusandae. Eveniet provident expedita iusto \
atque delectus et recusandae. Odio blanditiis qui alias autem minima blanditiis. Iste et \
ipsa minima placeat cupiditate fuga.",
"start_date": "2018-03-21T06:31:31.594224Z",
"submission_guidelines": "Ratione vitae dolor eos officia rem exercitationem. \
Ipsam pariatur a alias mollitia perspiciatis. Ipsa sit esse officiis quam eaque.",
"terms_and_conditions": "Officia dolores non labore nihil exercitationem minima. \
Esse repellendus accusamus minus nisi. Commodi cum adipisci molestias ipsum beatae qui \
enim porro. Cumque saepe distinctio repellendus et sed labore ratione aspernatur.",
"title": "Sarah Challenge"
}
"""


challenge_participant_teams = """
{
"count": 1,
Expand Down
37 changes: 34 additions & 3 deletions tests/test_challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ def test_display_future_challenge_lists(self):
assert response_table == 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 TestDisplayChallengesWithNoChallengeData(BaseTestClass):

def setup(self):
Expand Down Expand Up @@ -519,10 +550,10 @@ def test_display_challenge_phase_split(self):

@responses.activate
def test_display_challenge_phase_split_list_with_a_single_argument(self):
output = ("Usage: challenge [OPTIONS] CHALLENGE COMMAND [ARGS]...\n"
"\nError: Missing command.\n")
output = ("Usage: challenge phase [OPTIONS] PHASE COMMAND [ARGS]...\n"
"\nError: Missing argument \"PHASE\".\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2'])
result = runner.invoke(challenge, ['2', 'phase'])
response = result.output
assert response == output

Expand Down
34 changes: 28 additions & 6 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def setup(self):
responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_challenges.value).format("3"),
status=404)

responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_details.value.format("1")), status=404)

responses.add(responses.GET, url.format(API_HOST_URL, URLS.host_challenges.value).format("2"), status=404)

# Teams URLS
Expand Down Expand Up @@ -194,6 +196,15 @@ def test_display_challenge_phase_split_list_for_http_error_404(self):
response = result.output.rstrip()
url = "{}{}".format(API_HOST_URL, URLS.challenge_phase_split_detail.value)
expected = self.expected.format(url).format("1")
assert response == expected.format(url)

@responses.activate
def test_display_challenge_details_for_http_error_404(self):
runner = CliRunner()
result = runner.invoke(challenge, ["1"])
response = result.output
url = "{}{}".format(API_HOST_URL, URLS.challenge_details.value).format("1")
expected = "{}{}".format(self.expected.format(url), "\n")
assert response == expected

@responses.activate
Expand Down Expand Up @@ -390,11 +401,13 @@ def setup(self):

responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value), body=RequestException('...'))

responses.add(responses.GET, url.format(API_HOST_URL, URLS.past_challenge_list.value), body=Exception('...'))
responses.add(responses.GET, url.format(API_HOST_URL, URLS.past_challenge_list.value),
body=RequestException('...'))

responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_list.value), body=Exception('...'))

responses.add(responses.GET, url.format(API_HOST_URL, URLS.future_challenge_list.value), body=Exception('...'))
responses.add(responses.GET, url.format(API_HOST_URL, URLS.future_challenge_list.value),
body=RequestException('...'))

responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_teams.value), body=Exception('...'))

Expand All @@ -406,6 +419,9 @@ def setup(self):
responses.add(responses.GET, url.format(API_HOST_URL, URLS.host_challenges.value).format("2"),
body=Exception('...'))

responses.add(responses.GET, url.format(API_HOST_URL, URLS.challenge_details.value.format("1")),
body=RequestException('...'))

# Teams URLS

responses.add(responses.GET, url.format(API_HOST_URL, URLS.participant_team_list.value), body=Exception('...'))
Expand Down Expand Up @@ -446,25 +462,25 @@ def setup(self):
def test_display_challenge_list_for_request_exception(self):
runner = CliRunner()
result = runner.invoke(challenges)
assert result.output.strip() == "..."
assert result.exit_code == 1

@responses.activate
def test_display_past_challenge_list_for_request_exception(self):
runner = CliRunner()
result = runner.invoke(challenges, ['past'])
assert result.exit_code == -1
assert result.exit_code == 1

@responses.activate
def test_display_ongoing_challenge_list_for_request_exception(self):
runner = CliRunner()
result = runner.invoke(challenges, ['ongoing'])
assert result.output.strip() == "..."
assert result.exit_code == 1

@responses.activate
def test_display_future_challenge_list_for_request_exception(self):
runner = CliRunner()
result = runner.invoke(challenges, ['future'])
assert result.exit_code == -1
assert result.exit_code == 1

@responses.activate
def test_display_host_challenge_list_for_request_exception(self):
Expand Down Expand Up @@ -554,3 +570,9 @@ def test_display_leaderboard_for_request_exception(self):
result = runner.invoke(challenge, ['2', 'leaderboard', '1'])
response = result.output.strip()
assert response == "..."

@responses.activate
def test_display_challenge_details_for_request_exception(self):
runner = CliRunner()
result = runner.invoke(challenge, ["1"])
assert result.output.strip() == "..."
6 changes: 3 additions & 3 deletions tests/test_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def test_participate_in_a_challenge(self):

@responses.activate
def test_participate_in_a_challenge_with_single_argument(self):
output = ("Usage: challenge [OPTIONS] CHALLENGE COMMAND [ARGS]...\n"
"\nError: Missing command.\n")
output = ("Usage: challenge participate [OPTIONS] TEAM\n"
"\nError: Missing argument \"TEAM\".\n")
runner = CliRunner()
result = runner.invoke(challenge, ['2'])
result = runner.invoke(challenge, ['2', 'participate'])
response = result.output
assert response == output

Expand Down

0 comments on commit f4f5d4e

Please sign in to comment.