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

Conversation

serhii73
Copy link
Member

No description provided.

.pre-commit-config.yaml Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
@@ -0,0 +1,64 @@
"""Tests for Post model's Blog app."""
Copy link
Member

Choose a reason for hiding this comment

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

why does blog belong to model?

Copy link
Member Author

Choose a reason for hiding this comment

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

"""Tests for models of Blog app."""

My decision.



class PostModelTest(TestCase):
"""Tests for Post model."""
Copy link
Member

Choose a reason for hiding this comment

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

Anything specific, except for copy-paste from module docstring?

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 still ignored.

Copy link
Member Author

Choose a reason for hiding this comment

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

In top

"""Tests for models of Blog app."""

Doc's class

"""Tests for Post model."""

Copy link
Member Author

Choose a reason for hiding this comment

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

Now I think it good. Maybe I still wrong.

blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
blog/tests/models_test.py Outdated Show resolved Hide resolved
@webknjaz
Copy link
Member

webknjaz commented Sep 1, 2018

Any progress on this?

blog/tests/views_test.py Outdated Show resolved Hide resolved
blog/tests/views_test.py Outdated Show resolved Hide resolved
blog/tests/views_test.py Outdated Show resolved Hide resolved
blog/tests/views_test.py Outdated Show resolved Hide resolved
blog/tests/views_test.py Outdated Show resolved Hide resolved
blog/tests/views_test.py Outdated Show resolved Hide resolved
@serhii73 serhii73 force-pushed the feature/create_unit_tests branch from 0f6fcb0 to b9613f1 Compare November 12, 2018 08:33
@webknjaz
Copy link
Member

Ref #12

@serhii73 serhii73 force-pushed the feature/create_unit_tests branch from 6580057 to b9613f1 Compare November 19, 2018 09:54
@serhii73
Copy link
Member Author

@webknjaz I think now all good. Can we to merge this branch to master?

@webknjaz
Copy link
Member

Now, I'll review it again.

@@ -0,0 +1,69 @@
"""Tests for models' Blog app."""
Copy link
Member

Choose a reason for hiding this comment

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

What is a blog app of the models?

Copy link
Member Author

Choose a reason for hiding this comment

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

"""Tests for models of Blog app."""

Fixed

blog/tests/__init__.py Outdated Show resolved Hide resolved
"""Clean-up test data."""
del self.post

def test_title_label(self):
Copy link
Member

Choose a reason for hiding this comment

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

This and further similar tests test that Django's internal ORM classes work (which is not needed), they don't check whether the code that you wrote works.

Copy link
Member Author

Choose a reason for hiding this comment

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

Understood

self.assertEqual(field_label, 'published date')

def test_str_method(self):
"""Test for __str__ method."""
Copy link
Member

Choose a reason for hiding this comment

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

__str__ is an essential part of stringification protocol, invoked internally when str() function is called on objects.
So you're supposed to test that the class supports that protocol well by using the protocol.

Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member Author

Choose a reason for hiding this comment

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

    def test_right_title(self):
        """Test right title output."""
        assert str(self.post) == "Title"

But I think it needs to remove, I think I do not need to test Django functional. Or not?

Copy link
Member

Choose a reason for hiding this comment

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

I thought you added __str__ method to your model


def test_str_method(self):
"""Test for __str__ method."""
self.assertEqual('Title', self.post.__str__())
Copy link
Member

Choose a reason for hiding this comment

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

Also, the requirement to use assert statement is global. Style should be the same across the project, otherwise, it's hard to read it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed assertEqual

del self.post_third

def test_post_list(self):
"""Test show post list on the page."""
Copy link
Member

Choose a reason for hiding this comment

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

No, test that appropriate posts listed in the correct order.

Copy link
Member Author

Choose a reason for hiding this comment

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

    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
        assert [
            ['Title Bob', 'Text bob'],
            ['Title John', 'Text John'],
            ['Title John 2', 'Text John 2'],
        ] == [[i.title, i.text] for i in Post.objects.all()]
        assert [
            ['Title John', 'Text John'],
            ['Title Bob', 'Text bob'],
            ['Title John 2', 'Text John 2'],
        ] == [
            [i.title, i.text] for i in Post.objects.order_by('published_date')
        ]

def test_post_detail(self):
"""Test post detail."""
response = self.client.get(reverse('post_detail', kwargs={'pk': 1}))
self.assertEqual(response.status_code, 200)
Copy link
Member

Choose a reason for hiding this comment

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

/assert/

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed assertEqual

self.assertEqual(len(response.context['posts']), 2)

def test_post_detail(self):
"""Test post detail."""
Copy link
Member

Choose a reason for hiding this comment

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

post contents displayed correctly

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

response = self.client.post(
reverse('post_new'),
{
'author': paul,
Copy link
Member

Choose a reason for hiding this comment

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

You should probably use a string value, not some object.

Copy link
Member

Choose a reason for hiding this comment

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

Ignored.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have not understood this moment :(
Please tell me more what I need to do, thank you.

Copy link
Member

Choose a reason for hiding this comment

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

paul.username

self.assertIn(b'Title Bob', response.content)
self.assertIn(b'Text bob', response.content)

self.assertEqual(str(response.context['post']), 'Title Bob')
Copy link
Member

Choose a reason for hiding this comment

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

Why not compare to the post object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@@ -0,0 +1 @@
"""This package contains tests for the models and views of Blog app."""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""This package contains tests for the models and views of Blog app."""
"""Tests for the models and views of the Blog app."""

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed



class PostModelTest(TestCase):
"""Tests for Post model."""
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 still ignored.

self.assertEqual(field_label, 'published date')

def test_str_method(self):
"""Test for __str__ method."""
Copy link
Member

Choose a reason for hiding this comment

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

same

),
)
def test_post_publish(self):
"""Test for publish method."""
Copy link
Member

Choose a reason for hiding this comment

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

same

blog/tests/models_test.py Show resolved Hide resolved
str.encode(Post.objects.filter(pk=1)[0].text), response.content
)
assert (
str(response.context['post']) == 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.

Use a var for showing what you try to do.

str.encode(Post.objects.filter(pk=1)[0].title), response.content
)
self.assertIn(
str.encode(Post.objects.filter(pk=1)[0].text), response.content
Copy link
Member

Choose a reason for hiding this comment

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

Don't use unbound version of str method, just call it on the str itself. Also, don't query the DB for random things. Tests shouldn't do magical stuff.

response, 'blog/post_detail.html', 'blog/base.html'
)

self.assertIn(
Copy link
Member

Choose a reason for hiding this comment

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

assert .. in ..

response = self.client.post(
reverse('post_edit', kwargs={'pk': 1}),
{
'author': paul,
Copy link
Member

Choose a reason for hiding this comment

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

Same.

Copy link
Member

Choose a reason for hiding this comment

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

Still there.

self.assertIn(b'Post with post edit', response.content)
self.assertIn(b'Text with post edit', response.content)

response = self.client.get(reverse('post_edit', kwargs={'pk': 1}))
Copy link
Member

Choose a reason for hiding this comment

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

What do you test here?

Copy link
Member

Choose a reason for hiding this comment

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

So?

Copy link
Member Author

Choose a reason for hiding this comment

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

@webknjaz Maybe now all good.


def test_str_method(self):
"""Test for __str__ method."""
assert self.post.__str__() == 'Title'
Copy link
Member

Choose a reason for hiding this comment

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

Still wrong: see the explanation above.

@serhii73
Copy link
Member Author

@webknjaz I think now all good.
I fixed all places with assert.
I checked templates in all places.
Also, I re-read your comments and my code.

Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

Still ignoring stale comments.

del self.post

def test_right_title(self):
"""Test right title output."""
Copy link
Member

Choose a reason for hiding this comment

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

It's not output, it's just stringification protocol

),
)
def test_post_publish(self):
"""Test for publish method."""
Copy link
Member

Choose a reason for hiding this comment

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

still there

blog/tests/models_test.py Show resolved Hide resolved

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

first_title_on_the_page = response.context['posts'][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 too verbose and manual comparison. You can just compare post lists, not individual attributes.

Here's a good example of how to check what's rendered in different points of time: https://github.com/kpi-web-guild/django-girls-blog-makkaronis4e/pull/27/files#diff-8e9b1eecb0153fa41f8d69ca4922dc61R35

title_detail_db = Post.objects.filter(pk=1)[0].title
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 ?

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.

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.

response = self.client.post(
reverse('post_new'),
{
'author': paul,
Copy link
Member

Choose a reason for hiding this comment

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

paul.username

response = self.client.post(
reverse('post_edit', kwargs={'pk': 1}),
{
'author': paul,
Copy link
Member

Choose a reason for hiding this comment

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

Still there.

self.assertIn(b'Post with post edit', response.content)
self.assertIn(b'Text with post edit', response.content)

response = self.client.get(reverse('post_edit', kwargs={'pk': 1}))
Copy link
Member

Choose a reason for hiding this comment

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

So?

author=User.objects.create(username='bob'),
title='Title',
text='A few lines of text',
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.

"""Test show post list on the page and test correct order."""
with patch(
'django.utils.timezone.now',
lambda: datetime(day=1, month=1, year=2018, tzinfo=self.time_z),
Copy link
Member

Choose a reason for hiding this comment

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

You need to test 3 cases:

  • the date is frozen before anything got posted
  • the date is frozen after everything got posted
  • the date is frozen in between of those


def tearDown(self):
"""Clean-up test data."""
del self.time_z
del self.client
Copy link
Member

Choose a reason for hiding this comment

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

How is this related to that all quotes replacement thing?

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
resp = self.client.get(reverse('post_detail', kwargs={'pk': 1}))
Copy link
Member

Choose a reason for hiding this comment

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

Actually, the full var name reads better.

@@ -39,6 +39,7 @@ def test_post_publish(self):
"""Test post has a published date after publish method called."""
assert self.post.published_date is None

assert self.post.published_date is None
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 odd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants