-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Conversation
blog/tests/models_test.py
Outdated
@@ -0,0 +1,64 @@ | |||
"""Tests for Post model's Blog app.""" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
blog/tests/models_test.py
Outdated
|
||
|
||
class PostModelTest(TestCase): | ||
"""Tests for Post model.""" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still ignored.
There was a problem hiding this comment.
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."""
There was a problem hiding this comment.
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.
Any progress on this? |
0f6fcb0
to
b9613f1
Compare
Ref #12 |
6580057
to
b9613f1
Compare
@webknjaz I think now all good. Can we to merge this branch to master? |
Now, I'll review it again. |
blog/tests/models_test.py
Outdated
@@ -0,0 +1,69 @@ | |||
"""Tests for models' Blog app.""" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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/models_test.py
Outdated
"""Clean-up test data.""" | ||
del self.post | ||
|
||
def test_title_label(self): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood
blog/tests/models_test.py
Outdated
self.assertEqual(field_label, 'published date') | ||
|
||
def test_str_method(self): | ||
"""Test for __str__ method.""" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
blog/tests/models_test.py
Outdated
|
||
def test_str_method(self): | ||
"""Test for __str__ method.""" | ||
self.assertEqual('Title', self.post.__str__()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed assertEqual
blog/tests/views_test.py
Outdated
del self.post_third | ||
|
||
def test_post_list(self): | ||
"""Test show post list on the page.""" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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')
]
blog/tests/views_test.py
Outdated
def test_post_detail(self): | ||
"""Test post detail.""" | ||
response = self.client.get(reverse('post_detail', kwargs={'pk': 1})) | ||
self.assertEqual(response.status_code, 200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/assert/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed assertEqual
blog/tests/views_test.py
Outdated
self.assertEqual(len(response.context['posts']), 2) | ||
|
||
def test_post_detail(self): | ||
"""Test post detail.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
post contents displayed correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
blog/tests/views_test.py
Outdated
response = self.client.post( | ||
reverse('post_new'), | ||
{ | ||
'author': paul, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignored.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paul.username
blog/tests/views_test.py
Outdated
self.assertIn(b'Title Bob', response.content) | ||
self.assertIn(b'Text bob', response.content) | ||
|
||
self.assertEqual(str(response.context['post']), 'Title Bob') |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
blog/tests/__init__.py
Outdated
@@ -0,0 +1 @@ | |||
"""This package contains tests for the models and views of Blog app.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""This package contains tests for the models and views of Blog app.""" | |
"""Tests for the models and views of the Blog app.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
blog/tests/models_test.py
Outdated
|
||
|
||
class PostModelTest(TestCase): | ||
"""Tests for Post model.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still ignored.
blog/tests/models_test.py
Outdated
self.assertEqual(field_label, 'published date') | ||
|
||
def test_str_method(self): | ||
"""Test for __str__ method.""" |
There was a problem hiding this comment.
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
Outdated
), | ||
) | ||
def test_post_publish(self): | ||
"""Test for publish method.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
blog/tests/views_test.py
Outdated
str.encode(Post.objects.filter(pk=1)[0].text), response.content | ||
) | ||
assert ( | ||
str(response.context['post']) == Post.objects.filter(pk=1)[0].title |
There was a problem hiding this comment.
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.
blog/tests/views_test.py
Outdated
str.encode(Post.objects.filter(pk=1)[0].title), response.content | ||
) | ||
self.assertIn( | ||
str.encode(Post.objects.filter(pk=1)[0].text), response.content |
There was a problem hiding this comment.
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.
blog/tests/views_test.py
Outdated
response, 'blog/post_detail.html', 'blog/base.html' | ||
) | ||
|
||
self.assertIn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert .. in ..
blog/tests/views_test.py
Outdated
response = self.client.post( | ||
reverse('post_edit', kwargs={'pk': 1}), | ||
{ | ||
'author': paul, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still there.
blog/tests/views_test.py
Outdated
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})) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So?
There was a problem hiding this comment.
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.
blog/tests/models_test.py
Outdated
|
||
def test_str_method(self): | ||
"""Test for __str__ method.""" | ||
assert self.post.__str__() == 'Title' |
There was a problem hiding this comment.
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.
@webknjaz I think now all good. |
There was a problem hiding this 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.
blog/tests/models_test.py
Outdated
del self.post | ||
|
||
def test_right_title(self): | ||
"""Test right title output.""" |
There was a problem hiding this comment.
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
blog/tests/models_test.py
Outdated
), | ||
) | ||
def test_post_publish(self): | ||
"""Test for publish method.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still there
blog/tests/views_test.py
Outdated
|
||
assert len(response.context['posts']) == 2 | ||
|
||
first_title_on_the_page = response.context['posts'][0].title |
There was a problem hiding this comment.
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
blog/tests/views_test.py
Outdated
title_detail_db = Post.objects.filter(pk=1)[0].title | ||
title_detail_page = response.context['post'].title | ||
|
||
assert title_detail_db == title_detail_page |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
blog/tests/views_test.py
Outdated
response, 'blog/post_detail.html', 'blog/base.html' | ||
) | ||
|
||
title_detail_db = Post.objects.filter(pk=1)[0].title |
There was a problem hiding this comment.
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.
blog/tests/views_test.py
Outdated
response, 'blog/post_detail.html', 'blog/base.html' | ||
) | ||
|
||
title_new_in_db = Post.objects.filter(pk=4)[0].title |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compare models instead.
blog/tests/views_test.py
Outdated
response = self.client.post( | ||
reverse('post_new'), | ||
{ | ||
'author': paul, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paul.username
blog/tests/views_test.py
Outdated
response = self.client.post( | ||
reverse('post_edit', kwargs={'pk': 1}), | ||
{ | ||
'author': paul, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still there.
blog/tests/views_test.py
Outdated
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})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So?
Co-Authored-By: Sviatoslav Sydorenko <[email protected]>
blog/tests/models_test.py
Outdated
author=User.objects.create(username='bob'), | ||
title='Title', | ||
text='A few lines of text', | ||
author=User.objects.create(username="bob"), |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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})) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is odd
No description provided.