Skip to content

Commit

Permalink
pep8 and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
therealmarv committed Jan 18, 2017
1 parent ae3632c commit c7149e9
Showing 1 changed file with 6 additions and 42 deletions.
48 changes: 6 additions & 42 deletions test/test_cache/test_cache_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,24 @@
from pybossa.model.user import User

from factories import ProjectFactory, TaskFactory, TaskRunFactory, UserFactory
from factories import reset_all_pk_sequences


class TestUsersCache(Test):


def test_get_user_summary_nousers(self):
"""Test CACHE USERS get_user_summary returns None if no user exists with
the name requested"""
user = cached_users.get_user_summary('nouser')

assert user is None, user


def test_public_get_user_summary_nousers(self):
"""Test public CACHE USERS get_user_summary returns None if no user exists with
the name requested"""
user = cached_users.public_get_user_summary('nouser')

assert user is None, user


def test_get_user_summary_user_exists(self):
"""Test CACHE USERS get_user_summary returns a dict with the user data
if the user exists"""
Expand All @@ -65,7 +61,6 @@ def test_public_get_user_summary_user_exists(self):
assert type(zizou) is dict, type(zizou)
assert zizou != None, zizou


def test_get_user_summary_returns_fields(self):
"""Test CACHE USERS get_user_summary all the fields in the dict"""
UserFactory.create(name='user')
Expand Down Expand Up @@ -99,7 +94,7 @@ def test_rank_and_score(self):
users = UserFactory.create_batch(4)
for user in users:
i += 1
taskruns = TaskRunFactory.create_batch(i, user=user, task=tasks[i-1])
taskruns = TaskRunFactory.create_batch(i, user=user, task=tasks[i - 1])

first_in_rank = cached_users.rank_and_score(users[3].id)
last_in_rank = cached_users.rank_and_score(users[0].id)
Expand All @@ -109,7 +104,6 @@ def test_rank_and_score(self):
assert last_in_rank['rank'] == 4, last_in_rank['rank']
assert last_in_rank['score'] == 1, last_in_rank['score']


def test_projects_contributed_no_contributions(self):
"""Test CACHE USERS projects_contributed returns empty list if the user has
not contributed to any project"""
Expand All @@ -119,7 +113,6 @@ def test_projects_contributed_no_contributions(self):

assert projects_contributed == [], projects_contributed


def test_projects_contributed_no_contributions_cached(self):
"""Test CACHE USERS projects_contributed_cached returns empty list if the user has
not contributed to any project"""
Expand All @@ -129,7 +122,6 @@ def test_projects_contributed_no_contributions_cached(self):

assert projects_contributed == [], projects_contributed


def test_public_projects_contributed_no_contributions(self):
"""Test public CACHE USERS projects_contributed returns empty list if the user has
not contributed to any project"""
Expand All @@ -139,7 +131,6 @@ def test_public_projects_contributed_no_contributions(self):

assert projects_contributed == [], projects_contributed


def test_public_projects_contributed_no_contributions_cached(self):
"""Test public CACHE USERS projects_contributed_cached returns empty list if the user has
not contributed to any project"""
Expand All @@ -149,7 +140,6 @@ def test_public_projects_contributed_no_contributions_cached(self):

assert projects_contributed == [], projects_contributed


def test_projects_contributed_contributions(self):
"""Test CACHE USERS projects_contributed returns a list of projects that has
contributed to"""
Expand All @@ -164,7 +154,6 @@ def test_projects_contributed_contributions(self):
assert len(projects_contributed) == 1
assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed


def test_projects_contributed_contributions_cached(self):
"""Test CACHE USERS projects_contributed_cached returns a list of projects that has
contributed to"""
Expand Down Expand Up @@ -199,7 +188,6 @@ def test_public_projects_contributed_contributions(self):
assert 'owner_id' not in projects_contributed[0], err_msg
assert 'task_presenter' not in projects_contributed[0]['info']


def test_public_projects_contributed_contributions_cached(self):
"""Test CACHE USERS public cached projects_contributed returns a list of projects that has
contributed to"""
Expand All @@ -220,7 +208,6 @@ def test_public_projects_contributed_contributions_cached(self):
assert 'owner_id' not in projects_contributed[0], err_msg
assert 'task_presenter' not in projects_contributed[0]['info']


def test_projects_contributed_returns_fields(self):
"""Test CACHE USERS projects_contributed returns the info of the projects with
the required fields"""
Expand All @@ -229,14 +216,13 @@ def test_projects_contributed_returns_fields(self):
task = TaskFactory.create(project=project_contributed)
TaskRunFactory.create(task=task, user=user)
fields = ('id', 'name', 'short_name', 'owner_id', 'description',
'overall_progress', 'n_tasks', 'n_volunteers', 'info')
'overall_progress', 'n_tasks', 'n_volunteers', 'info')

projects_contributed = cached_users.projects_contributed(user.id)

for field in fields:
assert field in projects_contributed[0].keys(), field


def test_published_projects_no_projects(self):
"""Test CACHE USERS published_projects returns empty list if the user has
not created any project"""
Expand All @@ -246,7 +232,6 @@ def test_published_projects_no_projects(self):

assert projects_published == [], projects_published


def test_published_projects_no_projects_cached(self):
"""Test CACHE USERS published_projects_cached returns empty list if the user has
not created any project"""
Expand All @@ -256,7 +241,6 @@ def test_published_projects_no_projects_cached(self):

assert projects_published == [], projects_published


def test_public_published_projects_no_projects(self):
"""Test public CACHE USERS published_projects returns empty list if the user has
not created any project"""
Expand All @@ -266,7 +250,6 @@ def test_public_published_projects_no_projects(self):

assert projects_published == [], projects_published


def test_public_published_projects_no_projects_cached(self):
"""Test public CACHE USERS published_projects_cached returns empty list if the user has
not created any project"""
Expand All @@ -276,7 +259,6 @@ def test_public_published_projects_no_projects_cached(self):

assert projects_published == [], projects_published


def test_published_projects_returns_published(self):
"""Test CACHE USERS published_projects returns a list with the projects that
are published by the user"""
Expand All @@ -288,7 +270,6 @@ def test_published_projects_returns_published(self):
assert len(projects_published) == 1, projects_published
assert projects_published[0]['short_name'] == published_project.short_name, projects_published


def test_public_published_projects_returns_published(self):
"""Test public CACHE USERS published_projects returns a list with the projects that
are published by the user"""
Expand All @@ -300,7 +281,6 @@ def test_public_published_projects_returns_published(self):
assert len(projects_published) == 1, projects_published
assert projects_published[0]['short_name'] == published_project.short_name, projects_published


def test_published_projects_only_returns_published(self):
"""Test CACHE USERS published_projects does not return draft
or another user's projects"""
Expand All @@ -312,21 +292,19 @@ def test_published_projects_only_returns_published(self):

assert len(projects_published) == 0, projects_published


def test_published_projects_returns_fields(self):
"""Test CACHE USERS published_projects returns the info of the projects with
the required fields"""
user = UserFactory.create()
published_project = ProjectFactory.create(owner=user, published=True)
fields = ('id', 'name', 'short_name', 'owner_id', 'description',
'overall_progress', 'n_tasks', 'n_volunteers', 'info')
'overall_progress', 'n_tasks', 'n_volunteers', 'info')

projects_published = cached_users.published_projects(user.id)

for field in fields:
assert field in projects_published[0].keys(), field


def test_public_published_projects_returns_fields(self):
"""Test CACHE USERS published_projects returns the info of the projects with
the required fields"""
Expand All @@ -344,7 +322,6 @@ def test_public_published_projects_returns_fields(self):
for field in private_fields:
assert field not in projects_published[0].keys(), field


def test_public_published_projects_cached_returns_fields(self):
"""Test CACHE USERS published_projects_cached returns the info of the projects with
the required fields"""
Expand All @@ -362,7 +339,6 @@ def test_public_published_projects_cached_returns_fields(self):
for field in private_fields:
assert field not in projects_published[0].keys(), field


def test_draft_projects_no_projects(self):
"""Test CACHE USERS draft_projects returns an empty list if the user has no
draft projects"""
Expand All @@ -373,7 +349,6 @@ def test_draft_projects_no_projects(self):

assert len(draft_projects) == 0, draft_projects


def test_draft_projects_return_drafts(self):
"""Test CACHE USERS draft_projects returns draft belonging to the user"""
user = UserFactory.create()
Expand All @@ -384,7 +359,6 @@ def test_draft_projects_return_drafts(self):
assert len(draft_projects) == 1, draft_projects
assert draft_projects[0]['short_name'] == draft_project.short_name, draft_projects


def test_draft_projects_only_returns_drafts(self):
"""Test CACHE USERS draft_projects does not return any pubished projects
or drafts that belong to another user"""
Expand All @@ -396,21 +370,19 @@ def test_draft_projects_only_returns_drafts(self):

assert len(draft_projects) == 0, draft_projects


def test_draft_projects_returns_fields(self):
"""Test CACHE USERS draft_projects returns the info of the projects with
the required fields"""
user = UserFactory.create()
draft_project = ProjectFactory.create(owner=user, published=False)
fields = ('id', 'name', 'short_name', 'owner_id', 'description',
'overall_progress', 'n_tasks', 'n_volunteers', 'info')
'overall_progress', 'n_tasks', 'n_volunteers', 'info')

draft_project = cached_users.draft_projects(user.id)

for field in fields:
assert field in draft_project[0].keys(), field


def test_get_leaderboard_no_users_returns_empty_list(self):
"""Test CACHE USERS get_leaderboard returns an empty list if there are no
users"""
Expand All @@ -419,7 +391,6 @@ def test_get_leaderboard_no_users_returns_empty_list(self):

assert users == [], users


def test_get_leaderboard_returns_users_ordered_by_rank(self):
leader = UserFactory.create()
second = UserFactory.create()
Expand All @@ -428,7 +399,7 @@ def test_get_leaderboard_returns_users_ordered_by_rank(self):
tasks = TaskFactory.create_batch(3, project=project)
i = 3
for user in [leader, second, third]:
TaskRunFactory.create_batch(i, user=user, task=tasks[i-1])
TaskRunFactory.create_batch(i, user=user, task=tasks[i - 1])
i -= 1

leaderboard = cached_users.get_leaderboard(3)
Expand All @@ -437,7 +408,6 @@ def test_get_leaderboard_returns_users_ordered_by_rank(self):
assert leaderboard[1]['name'] == second.name
assert leaderboard[2]['name'] == third.name


def test_get_leaderboard_includes_specific_user_even_is_not_in_top(self):
leader = UserFactory.create()
second = UserFactory.create()
Expand All @@ -446,7 +416,7 @@ def test_get_leaderboard_includes_specific_user_even_is_not_in_top(self):
tasks = TaskFactory.create_batch(3, project=project)
i = 3
for user in [leader, second, third]:
TaskRunFactory.create_batch(i, user=user, task=tasks[i-1])
TaskRunFactory.create_batch(i, user=user, task=tasks[i - 1])
i -= 1
user_out_of_top = UserFactory.create()

Expand All @@ -455,7 +425,6 @@ def test_get_leaderboard_includes_specific_user_even_is_not_in_top(self):
assert len(leaderboard) is 4
assert leaderboard[-1]['name'] == user_out_of_top.name


def test_get_leaderboard_returns_fields(self):
"""Test CACHE USERS get_leaderboard returns user fields"""
user = UserFactory.create()
Expand All @@ -468,13 +437,11 @@ def test_get_leaderboard_returns_fields(self):
assert field in leaderboard[0].keys(), field
assert len(leaderboard[0].keys()) == len(fields)


def test_get_total_users_returns_0_if_no_users(self):
total_users = cached_users.get_total_users()

assert total_users == 0, total_users


def test_get_total_users_returns_number_of_users(self):
expected_number_of_users = 3
UserFactory.create_batch(expected_number_of_users)
Expand All @@ -483,7 +450,6 @@ def test_get_total_users_returns_number_of_users(self):

assert total_users == expected_number_of_users, total_users


def test_get_users_page_only_returns_users_with_contributions(self):
users = UserFactory.create_batch(2)
TaskRunFactory.create(user=users[0])
Expand All @@ -492,7 +458,6 @@ def test_get_users_page_only_returns_users_with_contributions(self):

assert len(users_with_contrib) == 1, users_with_contrib


def test_get_users_page_supports_pagination(self):
users = UserFactory.create_batch(3)
for user in users:
Expand All @@ -503,7 +468,6 @@ def test_get_users_page_supports_pagination(self):
assert len(paginated_users) == 1, paginated_users
assert paginated_users[0]['name'] == users[1].name


def test_get_users_page_returns_fields(self):
user = UserFactory.create()
TaskRunFactory.create(user=user)
Expand Down

0 comments on commit c7149e9

Please sign in to comment.