Skip to content

Commit

Permalink
test(utils): add picture factory and global fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
tyronejosee committed Dec 2, 2024
1 parent 9c75121 commit 0cadd70
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions apps/utils/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Factories for Utils App."""

import factory
from uuid import uuid4
from django.contrib.contenttypes.models import ContentType

from apps.animes.models import Anime
from apps.mangas.models import Manga
from apps.characters.models import Character
from apps.persons.models import Person
from ..models import Picture
from ..functions import generate_test_image, generate_random_code


class PictureFactory(factory.django.DjangoModelFactory):
"""Factory for Picture model."""

class Meta:
model = Picture
skip_postgeneration_save = True

content_type = factory.LazyAttribute(
lambda o: ContentType.objects.get_for_model(o.content_object.__class__)
)
object_id = factory.LazyAttribute(lambda o: uuid4())
name = factory.LazyFunction(lambda: generate_random_code())
image = factory.LazyAttribute(lambda _: generate_test_image(size=(600, 600)))

@factory.lazy_attribute
def content_object(self):
model_mapping = {
"anime": Anime.objects.first(),
"manga": Manga.objects.first(),
"character": Character.objects.first(),
"person": Person.objects.first(),
}
content_type_model = model_mapping.get(self.content_type.model)
return content_type_model
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ModeratorFactory,
AdministratorFactory,
)
from apps.utils.tests.factories import PictureFactory

User = get_user_model()

Expand Down Expand Up @@ -204,3 +205,8 @@ def anime_list_item():
@pytest.fixture
def manga_list_item():
return MangaListItemFactory.create()


@pytest.fixture
def picture():
return PictureFactory.create()

0 comments on commit 0cadd70

Please sign in to comment.