-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed unit test for post comment model
- Loading branch information
1 parent
0d0b9d6
commit 13a8378
Showing
6 changed files
with
197 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 5.0.1 on 2024-03-28 07:53 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("posts", "0006_alter_post_categories"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="category", | ||
name="name", | ||
field=models.CharField( | ||
max_length=100, null=True, unique=True, verbose_name="Category name" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 5.0.1 on 2024-03-28 18:01 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("posts", "0007_alter_category_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="post", | ||
name="title", | ||
field=models.CharField( | ||
max_length=250, null=True, unique=True, verbose_name="Post title" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
from django.contrib.auth import get_user_model | ||
|
||
from apps.posts.models import Comment | ||
|
||
# * If you don't declare pytestmark our test class model don't accsess to database table | ||
pytestmark = pytest.mark.django_db | ||
|
||
# User | ||
User = get_user_model() | ||
|
||
|
||
# !TestPostComment | ||
class TestPostComment: | ||
def test_comment_factory_creation( | ||
self, comment_factory, post_factory, user_factory | ||
): | ||
""" | ||
Test the creation of a Comment object using the CommentFactory. | ||
Args: | ||
comment_factory: A fixture that provides a CommentFactory instance. | ||
post_factory: A fixture that provides a PostFactory instance. | ||
user_factory: A fixture that provides a UserFactory instance. | ||
Returns: | ||
None | ||
Raises: | ||
AssertionError: If any of the assertions fail. | ||
""" | ||
obj = comment_factory(post=post_factory(), author=user_factory()) | ||
assert isinstance(obj, Comment) | ||
assert obj.post is not None | ||
assert obj.author is not None | ||
assert obj.body is not None | ||
|
||
def test_str_method(self, comment_factory): | ||
""" | ||
Test the __str__ method of the Comment model. | ||
Args: | ||
comment_factory: A fixture that provides a CommentFactory instance. | ||
Returns: | ||
None | ||
Raises: | ||
AssertionError: If the string representation does not match the expected format. | ||
""" | ||
obj = comment_factory() | ||
assert obj.__str__() == f"{obj.body[:20]} by {obj.author.username}" | ||
|
||
def test_comment_body(self, comment_factory): | ||
""" | ||
Test the body attribute of the Comment model. | ||
Args: | ||
comment_factory: A fixture that provides a CommentFactory instance. | ||
Returns: | ||
None | ||
Raises: | ||
AssertionError: If the body attribute does not contain at least one paragraph. | ||
""" | ||
body = comment_factory().body.split("\n\n") | ||
assert len(body) >= 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters