Skip to content

Commit

Permalink
[#104] Add a simple test case for search
Browse files Browse the repository at this point in the history
  • Loading branch information
punchagan committed Feb 19, 2017
1 parent 2a06486 commit 268147e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
29 changes: 27 additions & 2 deletions home/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def tearDown(self):
def clear_db(self):
User.objects.all().delete()

def create_posts(self, n):
create_posts(n)
def create_posts(self, n, **kwargs):
create_posts(n, **kwargs)
for blog in Blog.objects.filter():
blog.user = self.user
blog.save()
Expand Down Expand Up @@ -517,3 +517,28 @@ def test_should_paginate_new_posts(self):
else:
self.assertContains(response_2, post.title)
self.assertContains(response_2, post.slug)


class SearchViewTestCase(BaseViewTestCase):

def test_should_show_matching_posts(self):
# Given
self.login()
query = 'python'
n = 5
matching_title = 'Python is awesome'
non_matching_title = 'Django rocks'
self.create_posts(n, title=matching_title)
self.create_posts(n, title=non_matching_title)

# When
response = self.client.get('/search/?q={}'.format(query), follow=True)

# Then
for post in Post.objects.all():
if post.title == matching_title:
self.assertContains(response, post.title)
self.assertContains(response, post.slug)

else:
self.assertNotContains(response, post.slug)
4 changes: 2 additions & 2 deletions home/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def _generate_item(atom=False):
return item


def create_posts(n):
def create_posts(n, **kwargs):
"""Create the specified number of posts."""
return PostFactory.create_batch(n)
return PostFactory.create_batch(n, **kwargs)


def generate_feed():
Expand Down

0 comments on commit 268147e

Please sign in to comment.