Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/create unit tests #42

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions blog/tests.py

This file was deleted.

1 change: 1 addition & 0 deletions blog/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test collection for blog app."""
46 changes: 46 additions & 0 deletions blog/tests/models_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Tests for models."""

from datetime import datetime
from unittest.mock import patch

from django.contrib.auth.models import User
from django.test import TestCase
from django.utils import timezone

from ..models import Post


class PostModelTest(TestCase):
"""Tests for post model."""

def setUp(self):
"""Set up non-modifiable objects used by all test methods."""
self.post = Post.objects.create(
author=User.objects.create(username="bob"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's an error. And I actually prefer single quotes.

title="Title",
text="A few lines of text",
)

def tearDown(self):
"""Clean-up test data."""
del self.post

def test_post_str(self):
"""Test post stringification."""
assert str(self.post) == "Title"

@patch(
"django.utils.timezone.now",
lambda: datetime(
day=11, month=9, year=2018, tzinfo=timezone.get_current_timezone()
),
)
def test_post_publish(self):
"""Test post has a published date after publish method called."""
assert self.post.published_date is None

self.post.publish()
serhii73 marked this conversation as resolved.
Show resolved Hide resolved

assert self.post.published_date == datetime(
day=11, month=9, year=2018, tzinfo=timezone.get_current_timezone()
)
129 changes: 129 additions & 0 deletions blog/tests/views_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Test for views."""

from datetime import datetime

from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse

from ..models import Post


class PostListTest(TestCase):
"""Tests for Post views."""

def setUp(self):
"""Set up non-modifiable objects used by all test methods."""
bob = User.objects.create(username="bob")
self.post_first = Post.objects.create(
author=bob,
title="Title Bob",
text="Text bob",
published_date=datetime(day=8, month=11, year=2018),
)

john = User.objects.create(username="john")

self.post_second = Post.objects.create(
author=john, title="Title John", text="Text John"
)
self.post_third = Post.objects.create(
author=john, title="Title John 2", text="Text John 2"
)
self.post_third.publish()

def tearDown(self):
"""Clean-up test data."""
del self.post_first
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
del self.post_second
del self.post_third

def test_post_list_correct_order(self):
"""Test show post list on the page and test correct order."""
response = self.client.get(reverse("post_list"))
assert response.status_code == 200

self.assertTemplateUsed(response, "blog/post_list.html", "blog/base.html")

assert len(response.context["posts"]) == 2

first_title_on_the_page = response.context["posts"][0].title
first_text_on_the_page = response.context["posts"][0].text

second_title_on_the_page = response.context["posts"][1].title
second_text_on_the_page = response.context["posts"][1].text

assert first_title_on_the_page == "Title John 2"
assert first_text_on_the_page == "Text John 2"

assert second_title_on_the_page == "Title Bob"
assert second_text_on_the_page == "Text bob"

def test_post_detail(self):
"""Test post contents displayed correctly."""
response = self.client.get(reverse("post_detail", kwargs={"pk": 1}))
assert response.status_code == 200

self.assertTemplateUsed(response, "blog/post_detail.html", "blog/base.html")

title_detail_db = Post.objects.filter(pk=1)[0].title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is saved in the attribute, no need to query db.

title_detail_page = response.context["post"].title

assert title_detail_db == title_detail_page
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare post objects instead of some of their properties.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipdb> response.context["post"]                                         
<Post: Title post first>
ipdb> response.context["post"].author                                  
<User: bob>
ipdb> response.context["post"].text                                    
'Text post first'

I do not understand how to do it. I read all the internet, but not find an answer.
What do you mean post objects ?


text_detail_db = Post.objects.filter(pk=1)[0].text
text_detail_page = response.context["post"].text

assert text_detail_db == text_detail_page

def test_post_new_and_edit(self):
"""Test create and edit post."""
response = self.client.get(reverse("post_new"))
assert response.status_code == 200

paul_password = "test"
paul = User.objects.create_superuser(
username="paul", password=paul_password, email="[email protected]"
)
self.client.login(username=paul.username, password=paul_password)

response = self.client.post(
reverse("post_new"),
{
"author": paul,
"title": "Post with post new",
"text": "Text with post new",
},
follow=True,
)
assert response.status_code == 200

self.assertTemplateUsed(response, "blog/post_detail.html", "blog/base.html")

title_new_in_db = Post.objects.filter(pk=4)[0].title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare models instead.

title_new_on_the_page = response.context["post"].title

assert title_new_in_db == title_new_on_the_page

text_new_in_db = Post.objects.filter(pk=4)[0].text
text_new_on_the_page = response.context["post"].text

assert text_new_in_db == text_new_on_the_page

response = self.client.post(
reverse("post_edit", kwargs={"pk": 4}),
{
"author": paul,
"title": "Post with post edit",
"text": "Text with post edit",
},
follow=True,
)

title_edit_in_db = Post.objects.filter(pk=4)[0].title
title_edit_on_the_page = response.context["post"].title

assert title_edit_in_db == title_edit_on_the_page

response = self.client.get(reverse("post_edit", kwargs={"pk": 1}))
assert response.status_code == 200