Skip to content

Commit 907e2f7

Browse files
committed
test(animes): update endpoint tests for animes app
1 parent c4a4fd5 commit 907e2f7

File tree

3 files changed

+129
-28
lines changed

3 files changed

+129
-28
lines changed

apps/animes/tests/integration/test_endpoints.py

Lines changed: 120 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,54 @@
66
from rest_framework import status
77
from rest_framework.test import APIClient
88

9-
from apps.utils.functions import generate_test_image
109
from apps.characters.tests.factories import CharacterAnimeFactory
11-
from apps.characters.serializers import CharacterMinimalSerializer
10+
from apps.news.tests.factories import NewsFactory
1211
from apps.persons.tests.factories import PersonFactory, StaffAnimeFactory
1312
from apps.persons.models import Person, StaffAnime
1413
from apps.persons.serializers import StaffMinimalSerializer
1514
from apps.producers.tests.factories import ProducerFactory
1615
from apps.producers.choices import TypeChoices
1716
from apps.reviews.tests.factories import ReviewFactory
1817
from apps.reviews.models import Review
19-
from apps.reviews.serializers import ReviewReadSerializer
2018
from apps.users.tests.factories import MemberFactory
19+
from apps.utils.functions import generate_test_image
20+
from apps.utils.tests.factories import VideoFactory, PictureFactory
2121
from ...models import Anime
2222
from ...choices import SeasonChoices
23+
from ..factories import AnimeFactory
2324

2425

2526
@pytest.mark.django_db
2627
def test_list_animes(anonymous_user, anime):
2728
response = anonymous_user.get("/api/v1/animes/")
2829
assert response.status_code == status.HTTP_200_OK
29-
assert len(response.json()) > 0
30+
assert response.reason_phrase == "OK"
31+
assert len(response.data) > 0
32+
33+
34+
@pytest.mark.django_db
35+
def test_list_animes_errors(anonymous_user):
36+
response = anonymous_user.get("/api/v1/animes/")
37+
assert response.status_code == status.HTTP_200_OK
38+
assert response.reason_phrase == "OK"
39+
# TODO: Update status code to 404
3040

3141

3242
@pytest.mark.django_db
3343
def test_retrieve_anime(anonymous_user, anime):
3444
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/")
3545
assert response.status_code == status.HTTP_200_OK
46+
assert response.reason_phrase == "OK"
3647
assert str(response.data["id"]) == str(anime.id)
3748
assert response.data["name"] == anime.name
3849

3950

4051
@pytest.mark.django_db
41-
def test_retrieve_anime_not_found(anonymous_user):
42-
response = anonymous_user.get(
43-
"/api/v1/genres/989423d1-d6c0-431a-8f62-d805b8a5f321/"
44-
)
52+
def test_retrieve_anime_errors(anonymous_user):
53+
anime_id = "989423d1-d6c0-431a-8f62-d805b8a5f321"
54+
response = anonymous_user.get(f"/api/v1/animes/{anime_id}/")
4555
assert response.status_code == status.HTTP_404_NOT_FOUND
56+
assert response.reason_phrase == "Not Found"
4657

4758

4859
@pytest.mark.django_db
@@ -61,24 +72,23 @@ def test_create_anime(contributor_user, genre, theme):
6172
"themes": [str(theme.id)],
6273
"duration": timedelta(hours=1, minutes=45, seconds=30),
6374
}
64-
response = contributor_user.post(
65-
"/api/v1/animes/",
66-
data,
67-
format="multipart",
68-
)
75+
response = contributor_user.post("/api/v1/animes/", data, format="multipart")
6976
assert response.status_code == status.HTTP_201_CREATED
77+
assert response.reason_phrase == "Created"
7078
assert Anime.objects.filter(name="New Anime").exists()
7179
assert response.data["name"] == "New Anime"
7280

7381

7482
@pytest.mark.django_db
75-
def test_create_anime_unauthorized(member_user):
83+
def test_create_anime_errors(member_user):
7684
data = {}
7785
member_response = member_user.post("/api/v1/animes/", data, format="json")
7886
assert member_response.status_code == status.HTTP_403_FORBIDDEN
87+
assert member_response.reason_phrase == "Forbidden"
7988
member_user.logout()
8089
anonymus_response = member_user.post("/api/v1/animes/", data, format="json")
8190
assert anonymus_response.status_code == status.HTTP_401_UNAUTHORIZED
91+
assert anonymus_response.reason_phrase == "Unauthorized"
8292

8393

8494
@pytest.mark.django_db
@@ -101,6 +111,7 @@ def test_update_anime(contributor_user, anime, genre, theme):
101111
f"/api/v1/animes/{anime.id}/", data, format="multipart"
102112
)
103113
assert response.status_code == status.HTTP_200_OK
114+
assert response.reason_phrase == "OK"
104115
anime.refresh_from_db()
105116
assert anime.name == "Updated Anime"
106117

@@ -114,6 +125,7 @@ def test_partial_update_anime(contributor_user, anime):
114125
format="json",
115126
)
116127
assert response.status_code == status.HTTP_200_OK
128+
assert response.reason_phrase == "OK"
117129
anime.refresh_from_db()
118130
assert anime.name == "Partially Updated Anime"
119131

@@ -124,6 +136,7 @@ def test_delete_anime(contributor_user, anime):
124136
response = contributor_user.delete(f"/api/v1/animes/{anime.id}/")
125137
anime.refresh_from_db()
126138
assert response.status_code == status.HTTP_204_NO_CONTENT
139+
assert response.reason_phrase == "No Content"
127140
assert Anime.objects.filter(id=anime.id).exists()
128141
assert not anime.is_available
129142

@@ -133,14 +146,15 @@ def test_list_characters_by_anime(anonymous_user, anime, character):
133146
CharacterAnimeFactory(character_id=character, anime_id=anime)
134147
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/characters/")
135148
assert response.status_code == status.HTTP_200_OK
136-
expected_data = CharacterMinimalSerializer([character], many=True).data
137-
assert response.json() == expected_data
149+
assert response.reason_phrase == "OK"
150+
assert len(response.data) == 1
138151

139152

140153
@pytest.mark.django_db
141154
def test_list_characters_by_anime_errors(anonymous_user, anime):
142155
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/characters/")
143156
assert response.status_code == status.HTTP_404_NOT_FOUND
157+
assert response.reason_phrase == "Not Found"
144158
assert response.data["detail"] == "No characters found for this anime."
145159

146160

@@ -152,6 +166,7 @@ def test_list_staff_by_anime(anonymous_user, anime):
152166
StaffAnimeFactory(person_id=staff_two, anime_id=anime)
153167
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/staff/")
154168
assert response.status_code == status.HTTP_200_OK
169+
assert response.reason_phrase == "OK"
155170
staff_ids = StaffAnime.objects.filter(anime_id=anime.id).values_list(
156171
"person_id", flat=True
157172
)
@@ -164,13 +179,15 @@ def test_list_staff_by_anime(anonymous_user, anime):
164179
def test_list_staff_by_anime_errors(anonymous_user, anime):
165180
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/staff/")
166181
assert response.status_code == status.HTTP_404_NOT_FOUND
182+
assert response.reason_phrase == "Not Found"
167183
assert response.data["detail"] == "No staff found for this anime."
168184

169185

170186
@pytest.mark.django_db
171187
def test_retrieve_stats_by_anime(anonymous_user, anime):
172188
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/stats/")
173189
assert response.status_code == status.HTTP_200_OK
190+
assert response.reason_phrase == "OK"
174191
assert "id" in response.data
175192
assert "watching" in response.data
176193
assert "completed" in response.data
@@ -181,11 +198,11 @@ def test_retrieve_stats_by_anime(anonymous_user, anime):
181198

182199

183200
@pytest.mark.django_db
184-
def test_retrieve_stats_by_anime_errors(anonymous_user, anime):
185-
response = anonymous_user.get(
186-
"/api/v1/animes/1ec14918-dbf8-41cd-b1fd-1ad4b4493835/stats/"
187-
)
201+
def test_retrieve_stats_by_anime_errors(anonymous_user):
202+
anime_id = "88bf5d4f-115b-4dea-a7c5-4fc45b794c9a"
203+
response = anonymous_user.get(f"/api/v1/animes/{anime_id}/stats/")
188204
assert response.status_code == status.HTTP_404_NOT_FOUND
205+
assert response.reason_phrase == "Not Found"
189206
assert response.data["detail"] == "Not found."
190207

191208

@@ -196,9 +213,10 @@ def test_list_reviews_by_anime(anonymous_user, anime):
196213
object_id=anime.id,
197214
)
198215
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/reviews/")
199-
expected_data = ReviewReadSerializer([review], many=True).data
200216
assert response.status_code == status.HTTP_200_OK
201-
assert response.data == expected_data
217+
assert response.reason_phrase == "OK"
218+
assert len(response.data) > 0
219+
assert str(response.data[0]["id"]) == str(review.id)
202220

203221

204222
@pytest.mark.django_db
@@ -209,11 +227,10 @@ def test_create_review_by_anime(member_user, anime, review):
209227
"rating": review.rating,
210228
}
211229
response = member_user.post(
212-
f"/api/v1/animes/{anime.id}/reviews/create/",
213-
data,
214-
format="json",
230+
f"/api/v1/animes/{anime.id}/reviews/create/", data, format="json"
215231
)
216232
assert response.status_code == status.HTTP_201_CREATED
233+
assert response.reason_phrase == "Created"
217234
assert Review.objects.filter(comment="Review created").exists()
218235
assert response.data["comment"] == "Review created"
219236

@@ -237,5 +254,83 @@ def test_update_review_by_anime(anime):
237254
f"/api/v1/animes/{anime.id}/reviews/{review.id}/", data, format="json"
238255
)
239256
assert response.status_code == status.HTTP_200_OK
257+
assert response.reason_phrase == "OK"
240258
anime.refresh_from_db()
241259
assert response.data["comment"] == "Review updated"
260+
261+
262+
@pytest.mark.django_db
263+
def test_list_recommendations_by_anime(anonymous_user, theme, genre):
264+
anime = AnimeFactory(genres=[genre], themes=[theme])
265+
AnimeFactory.create_batch(3, genres=[genre], themes=[theme])
266+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/recommendations/")
267+
assert response.status_code == status.HTTP_200_OK
268+
assert response.reason_phrase == "OK"
269+
assert len(response.data) == 3
270+
271+
272+
@pytest.mark.django_db
273+
def test_list_recommendations_by_anime_errors(anonymous_user, anime):
274+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/recommendations/")
275+
assert response.status_code == status.HTTP_404_NOT_FOUND
276+
assert response.reason_phrase == "Not Found"
277+
assert response.data["detail"] == "No recommendations found for this anime."
278+
279+
280+
@pytest.mark.django_db
281+
def test_list_news_by_anime(anonymous_user, anime):
282+
NewsFactory.create_batch(3, anime_relations=[anime])
283+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/news/")
284+
assert response.status_code == status.HTTP_200_OK
285+
assert response.reason_phrase == "OK"
286+
assert len(response.data) == 3
287+
288+
289+
@pytest.mark.django_db
290+
def test_list_news_by_anime_errors(anonymous_user, anime):
291+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/news/")
292+
assert response.status_code == status.HTTP_404_NOT_FOUND
293+
assert response.reason_phrase == "Not Found"
294+
assert response.data["detail"] == "No news found for this anime."
295+
296+
297+
@pytest.mark.django_db
298+
def test_list_videos_by_anime(anonymous_user, anime):
299+
VideoFactory.create_batch(
300+
3,
301+
content_type=ContentType.objects.get_for_model(Anime),
302+
object_id=anime.id,
303+
)
304+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/videos/")
305+
assert response.status_code == status.HTTP_200_OK
306+
assert response.reason_phrase == "OK"
307+
assert len(response.data) == 3
308+
309+
310+
@pytest.mark.django_db
311+
def test_list_videos_by_anime_errors(anonymous_user, anime):
312+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/videos/")
313+
assert response.status_code == status.HTTP_404_NOT_FOUND
314+
assert response.reason_phrase == "Not Found"
315+
assert response.data["detail"] == "No videos found for this anime."
316+
317+
318+
@pytest.mark.django_db
319+
def test_list_pictures_by_anime(anonymous_user, anime):
320+
PictureFactory.create_batch(
321+
3,
322+
content_type=ContentType.objects.get_for_model(Anime),
323+
object_id=anime.id,
324+
)
325+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/pictures/")
326+
assert response.status_code == status.HTTP_200_OK
327+
assert response.reason_phrase == "OK"
328+
assert len(response.data) == 3
329+
330+
331+
@pytest.mark.django_db
332+
def test_list_pictures_by_anime_errors(anonymous_user, anime):
333+
response = anonymous_user.get(f"/api/v1/animes/{anime.id}/pictures/")
334+
assert response.status_code == status.HTTP_404_NOT_FOUND
335+
assert response.reason_phrase == "Not Found"
336+
assert response.data["detail"] == "No pictures found for this anime."

apps/animes/viewsets.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ def get_stats(self, request, *args, **kwargs):
173173
permission_classes=[AllowAny],
174174
url_path="reviews",
175175
)
176+
@method_decorator(cache_page(60 * 60 * 2))
177+
@method_decorator(vary_on_headers("User-Agent", "Accept-Language"))
176178
def get_reviews(self, request, *args, **kwargs):
177179
"""
178180
Action get all reviews for an anime.
@@ -317,6 +319,8 @@ def get_recommendations(self, request, *args, **kwargs):
317319
permission_classes=[AllowAny],
318320
url_path="news",
319321
)
322+
@method_decorator(cache_page(60 * 60 * 2))
323+
@method_decorator(vary_on_headers("User-Agent", "Accept-Language"))
320324
def get_news(self, request, *args, **kwargs):
321325
"""
322326
Action retrieve news associated with a anime.
@@ -329,7 +333,10 @@ def get_news(self, request, *args, **kwargs):
329333
if news.exists():
330334
serializer = NewsMinimalSerializer(news, many=True)
331335
return Response(serializer.data)
332-
return Response({"detail": _("No news found for this anime.")})
336+
return Response(
337+
{"detail": _("No news found for this anime.")},
338+
status=status.HTTP_404_NOT_FOUND,
339+
)
333340

334341
# @action(
335342
# detail=True,

apps/characters/tests/integration/test_endpoints.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Endpoint Tests for Animes App."""
22

33
import pytest
4-
from django.contrib.contenttypes.models import ContentType
54
from rest_framework import status
65

76
from apps.animes.tests.factories import AnimeFactory
@@ -113,7 +112,7 @@ def test_delete_character(contributor_user, character):
113112
@pytest.mark.django_db
114113
def test_list_pictures_by_character(anonymous_user, character):
115114
picture = PictureFactory(
116-
content_type=ContentType.objects.get_for_model(Character),
115+
content_object=character,
117116
object_id=character.id,
118117
)
119118
response = anonymous_user.get(f"/api/v1/characters/{character.id}/pictures/")

0 commit comments

Comments
 (0)