Skip to content

Commit

Permalink
test(playlists): add animelist and animelistiem integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyronejosee committed Dec 5, 2024
1 parent b0f3881 commit ab78bf2
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apps/playlists/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Meta:
score = factory.Iterator(ScoreChoices.values)
start_date = factory.Faker("date_between", start_date="-2y", end_date="today")
finish_date = factory.Faker("date_between", start_date="today", end_date="+1y")
tags = factory.Faker("words", nb=3)
tags = factory.LazyFunction(lambda: ["tag1", "tag2", "tag3"])
priority = factory.Iterator(PriorityChoices.values)
storage = factory.Iterator(StorageChoices.values)
times_rewatched = factory.Faker("random_int", min=0, max=5)
Expand Down
138 changes: 138 additions & 0 deletions apps/playlists/tests/integration/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -1 +1,139 @@
"""Endpoint Tests for Playlists App."""

import pytest
from rest_framework import status

from ...models import AnimeListItem
from ...choices import AnimeStatusChoices
from ..factories import AnimeListFactory, AnimeListItemFactory


@pytest.mark.django_db
def test_retrieve_animelist(member_user):
endpoint = "/api/v1/playlists/animelist/"
response = member_user.get(endpoint)
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert "id" in response.data
assert "banner" in response.data
assert "is_public" in response.data
assert "created_at" in response.data
assert "updated_at" in response.data


@pytest.mark.django_db
def test_retrieve_animelist_errors(anonymous_user):
endpoint = "/api/v1/playlists/animelist/"
response = anonymous_user.get(endpoint)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
assert response.reason_phrase == "Unauthorized"
assert response.data["detail"] == "Authentication credentials were not provided."


@pytest.mark.django_db
def test_partial_update_animelist(member_user):
endpoint = "/api/v1/playlists/animelist/"
data = {"is_public": False}
response = member_user.patch(endpoint, data, format="json")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert not response.data["is_public"]


@pytest.mark.django_db
def test_partial_update_animelist_errors(member_user):
endpoint = "/api/v1/playlists/animelist/"
data = {"is_public": "String field"}
response = member_user.patch(endpoint, data, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.reason_phrase == "Bad Request"
assert response.data["is_public"][0] == "Must be a valid boolean."


@pytest.mark.django_db
def test_list_animelist_item(member_user):
endpoint = "/api/v1/playlists/animelist/animes/"
response = member_user.get(endpoint)
assert response.status_code == status.HTTP_204_NO_CONTENT
assert response.reason_phrase == "No Content"
assert response.data["detail"] == "Your animelist is empty."


@pytest.mark.django_db
def test_create_animelist_item(member_user, anime_list_item, anime):
endpoint = "/api/v1/playlists/animelist/animes/"
data = {
"anime_id": anime.id,
"status": anime_list_item.status,
"episodes_watched": anime_list_item.episodes_watched,
"score": anime_list_item.score,
"start_date": anime_list_item.start_date,
"finish_date": anime_list_item.finish_date,
"priority": anime_list_item.priority,
"storage": anime_list_item.storage,
"times_rewatched": anime_list_item.times_rewatched,
"notes": anime_list_item.notes,
"order": anime_list_item.order,
"is_watched": anime_list_item.is_watched,
"is_favorite": anime_list_item.is_favorite,
}
response = member_user.post(endpoint, data, format="multipart")
assert response.status_code == status.HTTP_201_CREATED
assert response.reason_phrase == "Created"
assert str(response.data["anime_id"]) == str(anime.id)
response = member_user.get(endpoint)
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"


@pytest.mark.django_db
def test_retrieve_animelist_item(api_client_with_member_user):
api_client, user = api_client_with_member_user
animelist = AnimeListFactory.create(user=user)
item = AnimeListItemFactory.create(animelist_id=animelist)
endpoint = f"/api/v1/playlists/animelist/animes/{item.id}/"
response = api_client.get(endpoint)
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert "id" in response.data
assert "anime_id" in response.data
assert "status" in response.data
assert "episodes_watched" in response.data
assert "score" in response.data
assert "start_date" in response.data
assert "finish_date" in response.data
assert "tags" in response.data
assert "priority" in response.data
assert "storage" in response.data
assert "times_rewatched" in response.data
assert "notes" in response.data
assert "order" in response.data
assert "is_watched" in response.data
assert "is_favorite" in response.data
assert "created_at" in response.data
assert "updated_at" in response.data


@pytest.mark.django_db
def test_partial_update_animelist_item(api_client_with_member_user):
api_client, user = api_client_with_member_user
animelist = AnimeListFactory.create(user=user)
item = AnimeListItemFactory.create(animelist_id=animelist)
endpoint = f"/api/v1/playlists/animelist/animes/{item.id}/"
data = {"status": "watching"}
response = api_client.patch(endpoint, data, format="json")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert response.data["status"] == AnimeStatusChoices.WATCHING


@pytest.mark.django_db
def test_delete_animelist_item(api_client_with_member_user):
api_client, user = api_client_with_member_user
animelist = AnimeListFactory.create(user=user)
item = AnimeListItemFactory.create(animelist_id=animelist)
endpoint = f"/api/v1/playlists/animelist/animes/{item.id}/"
response = api_client.delete(endpoint)
assert response.status_code == status.HTTP_204_NO_CONTENT
assert response.reason_phrase == "No Content"
assert AnimeListItem.objects.filter(id=item.id).exists()
8 changes: 7 additions & 1 deletion apps/playlists/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ class AnimeListItemView(APIView):
permission_classes = [IsMember]

def get_queryset(self):
return AnimeList.objects.get(user=self.request.user)
# return AnimeList.objects.get(user=self.request.user)
try:
return AnimeList.objects.get(user=self.request.user)
except AnimeList.DoesNotExist:
return AnimeList.objects.create(user=self.request.user)
except Exception as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)

def get(self, request, *args, **kwargs):
# Retrieve all animes from the animelist
Expand Down
7 changes: 7 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def user():
return UserBaseFactory.create()


@pytest.fixture
def api_client_with_member_user(api_client):
user = MemberFactory()
api_client.force_authenticate(user=user)
return api_client, user


@pytest.fixture
def member_user(api_client):
user = MemberFactory()
Expand Down

0 comments on commit ab78bf2

Please sign in to comment.