Skip to content

Commit 3ba2df8

Browse files
committed
Add Post & Comment Factory
1 parent 76ecba5 commit 3ba2df8

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

blog/tests.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

blog/tests/__init__.py

Whitespace-only changes.

blog/tests/blog_factories.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import factory
2+
import factory.fuzzy
3+
from django.contrib.auth import get_user_model
4+
5+
from blog.models import Post, Comment
6+
7+
8+
User = get_user_model()
9+
10+
11+
class UserFactory(factory.DjangoModelFactory):
12+
"""
13+
Factory for user
14+
"""
15+
class Meta:
16+
model = User
17+
18+
username = factory.Faker('user_name')
19+
email = factory.Faker('email')
20+
first_name = factory.Faker('first_name')
21+
last_name = factory.Faker('last_name')
22+
password = factory.PostGenerationMethodCall(
23+
'set_password', factory.Faker('password')
24+
)
25+
26+
27+
class PostFactory(factory.DjangoModelFactory):
28+
"""
29+
Factory For Post
30+
"""
31+
class Meta:
32+
model = Post
33+
34+
title = factory.Faker('sentence')
35+
slug = factory.Faker('slug')
36+
author = factory.SubFactory(UserFactory)
37+
body = factory.Faker('sentences')
38+
image = factory.django.ImageField(
39+
color=factory.fuzzy.FuzzyChoice(['blue', 'yellow', 'green', 'orange']),
40+
height=720,
41+
width=1280,
42+
)
43+
status = "published"
44+
45+
@factory.post_generation
46+
def post_tags(self, create, extracted, **kwargs):
47+
for _ in range(3):
48+
self.tags.add(factory.Faker('word').generate())
49+
50+
51+
class CommentFactory(factory.DjangoModelFactory):
52+
"""
53+
Factory For Comment
54+
"""
55+
class Meta:
56+
model = Comment
57+
58+
post = factory.SubFactory(PostFactory)
59+
name = factory.Faker('name')
60+
email = factory.Faker('email')
61+
body = factory.Faker('paragraph')

0 commit comments

Comments
 (0)