Skip to content

Commit

Permalink
Add tests for submission/subreddits endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Feb 28, 2023
1 parent 362308a commit a8abdcc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
89 changes: 89 additions & 0 deletions blossom/api/tests/submissions/test_submission_subreddits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Disable line length restrictions to allow long URLs
# flake8: noqa: E501
from collections import OrderedDict

from django.test import Client
from django.urls import reverse
from rest_framework import status

from blossom.utils.test_helpers import create_submission, setup_user_client


class TestSubreddits:
"""Tests to validate that the subreddit data is generated correctly."""

def test_subreddit_extraction(self, client: Client) -> None:
"""Test that the subreddit of a single submission is determined correctly."""
client, headers, user = setup_user_client(client, accepted_coc=True, id=123456)

create_submission(
url="https://reddit.com/r/ProgrammerHumor/comments/11e845g/think_smart_not_hard/"
)

result = client.get(
reverse("submission-subreddits"),
content_type="application/json",
**headers,
)

assert result.status_code == status.HTTP_200_OK

expected_subreddits = OrderedDict(ProgrammerHumor=1)
subreddits = result.json()
assert subreddits == expected_subreddits

def test_subreddit_aggregation(self, client: Client) -> None:
"""Test that multiple submissions from the same subreddit are aggregated."""
client, headers, user = setup_user_client(client, accepted_coc=True, id=123456)

create_submission(
url="https://reddit.com/r/ProgrammerHumor/comments/11e845g/think_smart_not_hard/"
)
create_submission(
url="https://reddit.com/r/ProgrammerHumor/comments/11e88ls/then_what_do_you_do/"
)
create_submission(
url="https://reddit.com/r/CuratedTumblr/comments/11e232j/life_is_nuanced_and_complex/"
)
create_submission(
url="https://reddit.com/r/ProgrammerHumor/comments/11e42w6/yes_i_know_about_transactions_and_backups/"
)
create_submission(
url="https://reddit.com/r/CuratedTumblr/comments/11ds7gc/big_boss_was_down_bad/"
)

result = client.get(
reverse("submission-subreddits"),
content_type="application/json",
**headers,
)

assert result.status_code == status.HTTP_200_OK

expected_subreddits = OrderedDict(ProgrammerHumor=3, CuratedTumblr=2)
subreddits = result.json()
assert subreddits == expected_subreddits

def test_submission_filters(self, client: Client) -> None:
"""Test that the normal submission filters work."""
client, headers, user = setup_user_client(client, accepted_coc=True, id=123456)

create_submission(
url="https://reddit.com/r/ProgrammerHumor/comments/11e845g/think_smart_not_hard/",
completed_by=user,
)
create_submission(
url="https://reddit.com/r/ProgrammerHumor/comments/11e88ls/then_what_do_you_do/"
)

result = client.get(
reverse("submission-subreddits") + "?completed_by=123456",
content_type="application/json",
**headers,
)

assert result.status_code == status.HTTP_200_OK

expected_subreddits = OrderedDict(ProgrammerHumor=1)
subreddits = result.json()
assert subreddits == expected_subreddits
4 changes: 2 additions & 2 deletions blossom/api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def heatmap(self, request: Request) -> Response:
operation_summary="Get the submission count by subreddit.",
)
@action(detail=False, methods=["get"])
def subreddits(self) -> Response:
def subreddits(self, request: Request) -> Response:
"""Count the submissions by subreddit."""
subreddit_query = (
self.filter_queryset(Submission.objects)
Expand All @@ -420,7 +420,7 @@ def subreddits(self) -> Response:

# Sort descending by submission count
sorted_subreddits = OrderedDict(
sorted(subreddit_counts.items(), key=lambda _, count: count, reverse=True)
sorted(subreddit_counts.items(), key=lambda x: x[1], reverse=True)
)

return Response(sorted_subreddits)
Expand Down

0 comments on commit a8abdcc

Please sign in to comment.