Source code for tests.posts.factories

import random

from django.core.files.base import ContentFile
from factory import LazyAttribute, SubFactory
from factory.django import DjangoModelFactory, ImageField
from faker import Faker

from apps.posts.constants import StatusPostChoice
from apps.posts.models import Comment, CommentImage, Like, Post, PostMedia, Tag
from tests.users.factories import UserFactory  # type: ignore

fake = Faker()


[docs] class PostFactory(DjangoModelFactory): class Meta: model = Post author = SubFactory(UserFactory) title = LazyAttribute(lambda x: fake.sentence()) content = LazyAttribute(lambda x: fake.text()) visibility = Post.VisibilityChoices.PUBLIC status = StatusPostChoice.PUBLISHED views_quantity = LazyAttribute(lambda x: random.randint(0, 1000)) likes_quantity = LazyAttribute(lambda x: random.randint(0, x.views_quantity))
[docs] class PostMediaFactory(DjangoModelFactory): post = SubFactory(PostFactory) original = LazyAttribute( lambda _: ContentFile(ImageField()._make_data({"width": 1024, "height": 768}), "example.jpg") ) class Meta: model = PostMedia
[docs] class LikeFactory(DjangoModelFactory): class Meta: model = Like author = SubFactory(UserFactory) post = SubFactory(PostFactory)
[docs] class CommentFactory(DjangoModelFactory): class Meta: model = Comment author = SubFactory(UserFactory) post = SubFactory(PostFactory) text = LazyAttribute(lambda x: fake.text())
[docs] class ImageCommentFactory(DjangoModelFactory): class Meta: model = CommentImage comment = SubFactory(CommentFactory) image = LazyAttribute(lambda _: ContentFile(ImageField()._make_data({"width": 1024, "height": 768}), "example.jpg"))
[docs] class TagFactory(DjangoModelFactory): class Meta: model = Tag name = LazyAttribute(lambda x: fake.word())