Skip to content

Commit

Permalink
pytest, linting, forrmating passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanotech committed Sep 16, 2024
1 parent a739653 commit 0334478
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
4 changes: 1 addition & 3 deletions core/management/commands/update_karma_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def handle(self: "Command", *_args: str, **_options: str) -> None:
def karma_subquery(*, parent_isnull: bool) -> Subquery:
return (
Post.objects.filter(
author_id=OuterRef("user_id"),
created_at__gte=date_limit,
parent__isnull=parent_isnull
author_id=OuterRef("user_id"), created_at__gte=date_limit, parent__isnull=parent_isnull
)
.values("author_id")
.annotate(karma_score=Sum(F("up_votes") - F("down_votes")))
Expand Down
20 changes: 6 additions & 14 deletions core/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
(5, 1, {"comment_karma": 4}), # for comment
],
)

@freeze_time(FIXED_DATETIME)
def test_calculate_karma_score_post_and_comment(
user: User, community: Community, up_votes: int, down_votes: int,
expected_karma: dict
user: User, community: Community, up_votes: int, down_votes: int, expected_karma: dict
) -> None:

post = Post.objects.create(
author=user,
up_votes=up_votes,
Expand All @@ -39,7 +36,6 @@ def test_calculate_karma_score_post_and_comment(
created_at=timezone.now() - timedelta(days=100),
)


Post.objects.create(
author=user,
up_votes=up_votes,
Expand All @@ -49,28 +45,24 @@ def test_calculate_karma_score_post_and_comment(
created_at=timezone.now() - timedelta(days=50),
)


call_command("update_karma_scores")

profile = Profile.objects.filter(user_id=user.id).get()


if "post_karma" in expected_karma:
assert profile.post_karma == expected_karma["post_karma"], (
f"Expected post_karma: {expected_karma['post_karma']}, "
f"but got {profile.post_karma}"
)
f"Expected post_karma: {expected_karma['post_karma']}, " f"but got {profile.post_karma}"
)

if "comment_karma" in expected_karma:
assert profile.comment_karma == expected_karma["comment_karma"], (
f"Expected comment_karma: {expected_karma['comment_karma']}, "
f"but got {profile.comment_karma}"
)
f"Expected comment_karma: {expected_karma['comment_karma']}, " f"but got {profile.comment_karma}"
)


@pytest.mark.django_db()
@freeze_time(FIXED_DATETIME)
def test_calculate_karma_score_two_posts_within_year(user: User, community: Community) -> None:

Post.objects.create(author=user, up_votes=10, down_votes=2, community=community, parent=None)
Post.objects.create(author=user, up_votes=5, down_votes=0, community=community, parent=None)

Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[pytest]
DJANGO_SETTINGS_MODULE = reddit.settings
python_files = tests.py test_*.py *_tests.py
python_files = test_commands.py, update_karma_scores.py
pythonpath = .
import_mode=importlib
2 changes: 1 addition & 1 deletion reddit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@

LAST_ACTIVITY_ONLINE_LIMIT_MINUTES = 15

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
EMAIL_HOST = config("EMAIL_HOST", default="smtp.gmail.com")
EMAIL_PORT = config("EMAIL_PORT", default=587)
EMAIL_USE_TLS = config("EMAIL_USE_TLS", default=True)
Expand Down

0 comments on commit 0334478

Please sign in to comment.