Skip to content

Commit

Permalink
Fix #49: Add remaining test cases for the challenge features (#51)
Browse files Browse the repository at this point in the history
* Increase coverage by testing exceptions.

* Add coverage for RequestException handling

* Add meaningful function names

* Add meaningful function names
  • Loading branch information
guyandtheworld authored and RishabhJain2018 committed Jun 22, 2018
1 parent 22e22b5 commit 9719a9f
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 13 deletions.
3 changes: 2 additions & 1 deletion evalai/utils/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
import sys

from click import echo
from evalai.utils.config import AUTH_TOKEN_PATH
Expand All @@ -21,7 +22,7 @@ def get_user_auth_token():
else:
echo("\nYour token file doesn't exists.")
echo("\nIt should be present at ~/.evalai/token.json\n")
return None
sys.exit(1)


def get_request_header():
Expand Down
6 changes: 2 additions & 4 deletions evalai/utils/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,10 @@ def display_participated_or_hosted_challenges(is_host=False, is_participant=Fals
URLS.host_teams.value)
challenge_url = "{}{}".format(API_HOST_URL,
URLS.host_challenges.value)
echo(style("\nHosted Challenges\n", bold=True))

teams = get_participant_or_host_teams(team_url)

challenges = get_participant_or_host_team_challenges(challenge_url, teams)
echo(style("\nHosted Challenges\n", bold=True))

if len(challenges) != 0:
for challenge in challenges:
Expand All @@ -164,11 +163,10 @@ def display_participated_or_hosted_challenges(is_host=False, is_participant=Fals
URLS.participant_teams.value)
challenge_url = "{}{}".format(API_HOST_URL,
URLS.participant_challenges.value)
echo(style("\nParticipated Challenges\n", bold=True))

teams = get_participant_or_host_teams(team_url)

challenges = get_participant_or_host_team_challenges(challenge_url, teams)
echo(style("\nParticipated Challenges\n", bold=True))

if len(challenges) != 0:
for challenge in challenges:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import shutil
from click.testing import CliRunner

from evalai.challenges import challenges
from evalai.utils.config import AUTH_TOKEN_DIR

from tests.base import BaseTestClass


class TestGetUserAuthToken(BaseTestClass):

def setup(self):
if os.path.exists(AUTH_TOKEN_DIR):
shutil.rmtree(AUTH_TOKEN_DIR)

def test_get_user_auth_token_when_file_does_not_exist(self):
expected = ("\nYour token file doesn't exists.\n"
"\nIt should be present at ~/.evalai/token.json\n\n")
runner = CliRunner()
result = runner.invoke(challenges)
response = result.output
assert response == expected
80 changes: 72 additions & 8 deletions tests/test_challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .base import BaseTestClass


class TestChallenges(BaseTestClass):
class TestDisplayChallenges(BaseTestClass):

def setup(self):

Expand Down Expand Up @@ -52,34 +52,98 @@ def setup(self):
self.output = "{}{}".format(self.output, challenge)

@responses.activate
def test_challenge_lists(self):
def test_diaplay_all_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges)
response_table = result.output
assert response_table == self.output

@responses.activate
def test_past_challenge_lists(self):
def test_display_past_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges, ['past'])
response_table = result.output
assert response_table == self.output

@responses.activate
def test_ongoing_challenge_lists(self):
def test_display_ongoing_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges, ['ongoing'])
response_table = result.output
assert response_table == self.output

@responses.activate
def test_future_challenge_lists(self):
def test_display_future_challenge_lists(self):
runner = CliRunner()
result = runner.invoke(challenges, ['future'])
response_table = result.output
assert response_table == self.output


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)

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)

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()
expected = "\nParticipated Challenges\n\n"
self.output = "{}{}".format(expected, self.output)
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()
participant_string = "\nParticipated Challenges\n\n"
host_string = "\nHosted Challenges\n\n"
self.output = "{}{}{}{}".format(host_string, self.output, participant_string, self.output)
result = runner.invoke(challenges, ['--participant', '--host'])
response = result.output
assert response == self.output


class TestParticipantOrHostTeamChallenges(BaseTestClass):

def setup(self):
Expand Down Expand Up @@ -123,7 +187,7 @@ def setup(self):
self.output = "{}{}".format(self.output, challenge)

@responses.activate
def test_host_challenge_list(self):
def test_display_host_challenge_list(self):
runner = CliRunner()
expected = "\nHosted Challenges\n\n"
self.output = "{}{}".format(expected, self.output)
Expand All @@ -132,7 +196,7 @@ def test_host_challenge_list(self):
assert response == self.output

@responses.activate
def test_participant_challenge_lists(self):
def test_display_participant_challenge_lists(self):
runner = CliRunner()
expected = "\nParticipated Challenges\n\n"
self.output = "{}{}".format(expected, self.output)
Expand All @@ -141,7 +205,7 @@ def test_participant_challenge_lists(self):
assert response == self.output

@responses.activate
def test_participant_and_host_challenge_lists(self):
def test_display_participant_and_host_challenge_lists(self):
runner = CliRunner()
participant_string = "\nParticipated Challenges\n\n"
host_string = "\nHosted Challenges\n\n"
Expand Down
Loading

0 comments on commit 9719a9f

Please sign in to comment.