Skip to content

Commit

Permalink
test(persons, news): add endpoint tests for persons app, fix news fac…
Browse files Browse the repository at this point in the history
…tories
  • Loading branch information
tyronejosee committed Dec 3, 2024
1 parent 7e51d35 commit c4a4fd5
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 14 deletions.
30 changes: 20 additions & 10 deletions apps/news/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ class Meta:
image = factory.LazyAttribute(lambda _: generate_test_image(size=(600, 600)))
source = factory.Faker("url")
tag = factory.Iterator(TagChoices.values)
anime_relations = factory.RelatedFactoryList(
AnimeFactory,
"news",
size=2,
)
manga_relations = factory.RelatedFactoryList(
MangaFactory,
"news",
size=2,
)
author_id = factory.SubFactory(MemberFactory)

@factory.post_generation
def anime_relations(self, create, extracted, **kwargs):
if not create:
return
if extracted:
self.anime_relations.set(extracted)
else:
default_anime = AnimeFactory.create()
self.anime_relations.add(default_anime)

@factory.post_generation
def manga_relations(self, create, extracted, **kwargs):
if not create:
return
if extracted:
self.manga_relations.set(extracted)
else:
default_manga = MangaFactory.create()
self.manga_relations.add(default_manga)
5 changes: 2 additions & 3 deletions apps/news/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ def test_news_read_serializer(self, news):
"image": news.image.url,
"source": news.source,
"tag": news.get_tag_display(),
"anime_relations": [str(anime.id) for anime in news.anime_relations.all()],
"manga_relations": [str(manga.id) for manga in news.manga_relations.all()],
"anime_relations": serializer.data["anime_relations"],
"manga_relations": serializer.data["manga_relations"],
"created_at": news.created_at.isoformat(),
"updated_at": news.updated_at.isoformat(),
}

assert serializer.data == expected_data

def test_news_write_serializer_valid_data(self, news):
Expand Down
193 changes: 193 additions & 0 deletions apps/persons/tests/integration/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
"""Endpoint Tests for Persons App."""

import pytest
from django.contrib.contenttypes.models import ContentType
from rest_framework import status

from apps.characters.tests.factories import CharacterVoiceFactory
from apps.mangas.tests.factories import MangaFactory
from apps.utils.tests.factories import PictureFactory
from ...models import Person
from ...choices import CategoryChoices
from ..factories import PersonFactory


@pytest.mark.django_db
def test_list_persons(anonymous_user, person):
response = anonymous_user.get("/api/v1/persons/")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert len(response.data) > 0


@pytest.mark.django_db
def test_list_persons_errors(anonymous_user):
response = anonymous_user.get("/api/v1/persons/")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"


@pytest.mark.django_db
def test_retrieve_person(anonymous_user, person):
response = anonymous_user.get(f"/api/v1/persons/{person.id}/")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert str(response.data["id"]) == str(person.id)
assert response.data["name"] == person.name


@pytest.mark.django_db
def test_retrieve_person_errors(anonymous_user):
person_id = "989423d1-d6c0-431a-8f62-d805b8a5f321"
response = anonymous_user.get(f"/api/v1/persons/{person_id}/")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.reason_phrase == "Not Found"


@pytest.mark.django_db
def test_create_person(contributor_user, person):
data = {
"name": "New Person",
"given_name": person.given_name,
"family_name": person.family_name,
"image": person.image,
"birthday": person.birthday,
"about": person.about,
"website": person.website,
"language": person.language,
"category": person.category,
}
response = contributor_user.post("/api/v1/persons/", data, format="multipart")
assert response.status_code == status.HTTP_201_CREATED
assert response.reason_phrase == "Created"
assert Person.objects.filter(name="New Person").exists()
assert response.data["name"] == "New Person"


@pytest.mark.django_db
def test_create_person_errors(member_user):
data = {}
member_response = member_user.post("/api/v1/persons/", data, format="json")
assert member_response.status_code == status.HTTP_403_FORBIDDEN
assert member_response.reason_phrase == "Forbidden"
member_user.logout()
anonymus_response = member_user.post("/api/v1/persons/", data, format="json")
assert anonymus_response.status_code == status.HTTP_401_UNAUTHORIZED
assert anonymus_response.reason_phrase == "Unauthorized"


@pytest.mark.django_db
def test_update_person(contributor_user, person):
data = {
"name": "Updated Person",
"given_name": person.given_name,
"family_name": person.family_name,
"image": person.image,
"birthday": person.birthday,
"about": person.about,
"website": person.website,
"language": person.language,
"category": person.category,
}
response = contributor_user.put(
f"/api/v1/persons/{person.id}/", data, format="multipart"
)
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
person.refresh_from_db()
assert person.name == "Updated Person"


@pytest.mark.django_db
def test_partial_update_person(contributor_user, person):
data = {"name": "Partially Updated Person"}
response = contributor_user.patch(
f"/api/v1/persons/{person.id}/", data, format="json"
)
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
person.refresh_from_db()
assert person.name == "Partially Updated Person"


@pytest.mark.django_db
def test_delete_person(contributor_user, person):
assert person.is_available
response = contributor_user.delete(f"/api/v1/persons/{person.id}/")
person.refresh_from_db()
assert response.status_code == status.HTTP_204_NO_CONTENT
assert response.reason_phrase == "No Content"
assert Person.objects.filter(id=person.id).exists()
assert not person.is_available


@pytest.mark.django_db
def test_list_mangas_by_person(anonymous_user):
person = PersonFactory(category=CategoryChoices.WRITER)
MangaFactory.create_batch(5, author_id=person)
response = anonymous_user.get(f"/api/v1/persons/{person.id}/mangas/")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert response.data["count"] == 5
assert len(response.data["results"]) == 5


@pytest.mark.django_db
def test_list_mangas_by_person_errors(anonymous_user):
person = PersonFactory(category=CategoryChoices.WRITER)
response = anonymous_user.get(f"/api/v1/persons/{person.id}/mangas/")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.reason_phrase == "Not Found"
assert response.data["detail"] == "No mangas found for this person."


@pytest.mark.django_db
def test_list_pictures_by_manga(anonymous_user, person):
PictureFactory.create_batch(
3,
content_type=ContentType.objects.get_for_model(Person),
object_id=person.id,
)
response = anonymous_user.get(f"/api/v1/persons/{person.id}/pictures/")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert len(response.data) == 3


@pytest.mark.django_db
def test_list_pictures_by_person_errors(anonymous_user, person):
response = anonymous_user.get(f"/api/v1/persons/{person.id}/pictures/")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.reason_phrase == "Not Found"
assert response.data["detail"] == "No pictures found for this person."


@pytest.mark.django_db
def test_create_image_by_person(contributor_user, person, picture):
data = {
"name": "New person image",
"image": picture.image,
}
response = contributor_user.post(
f"/api/v1/persons/{person.id}/create-picture/", data, format="multipart"
)
assert response.status_code == status.HTTP_201_CREATED
assert response.reason_phrase == "Created"
assert response.data["detail"] == "Picture uploaded successfully."


@pytest.mark.django_db
def test_list_voices_by_person(anonymous_user, person):
CharacterVoiceFactory.create_batch(3, voice_id=person)
response = anonymous_user.get(f"/api/v1/persons/{person.id}/voices/")
assert response.status_code == status.HTTP_200_OK
assert response.reason_phrase == "OK"
assert len(response.data) == 3


@pytest.mark.django_db
def test_list_voices_by_person_errors(anonymous_user, person):
response = anonymous_user.get(f"/api/v1/persons/{person.id}/voices/")
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.reason_phrase == "Not Found"
assert response.data["detail"] == "No relations found for this person."
2 changes: 1 addition & 1 deletion apps/persons/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def create_picture(self, request, pk=None, *args, **kwargs):
Action create a picture for the person.
Endpoints:
- POST api/v1/persons/{id}/pictures/
- POST api/v1/persons/{id}/create-picture/
"""
serializer = PictureWriteSerializer(data=request.data)
if serializer.is_valid():
Expand Down

0 comments on commit c4a4fd5

Please sign in to comment.