Skip to content

Commit

Permalink
Merge pull request #101 from chrishiestand/patch-6
Browse files Browse the repository at this point in the history
fix invalid_url, add misdated_url tests
  • Loading branch information
macro1 committed Nov 5, 2014
2 parents 4477f80 + 6e19c45 commit 17a2a45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
19 changes: 13 additions & 6 deletions myblog/blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,20 @@ def test_url(self):
self.assertTemplateUsed(response,
template_name='blog/entry_detail.html')

def test_invalid_url(self):
entry = Entry.objects.create(title="title", body="body",
author=self.user)
response = self.client.get("/0000/00/00/{0}-invalid/".format(entry.id))
def test_misdated_url(self):
entry = Entry.objects.create(
title="title", body="body", author=self.user)
url = "/0000/00/00/{0}-misdated/".format(entry.id)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response,
template_name='blog/entry_detail.html')
self.assertTemplateUsed(
response, template_name='blog/entry_detail.html')

def test_invalid_url(self):
entry = Entry.objects.create(
title="title", body="body", author=self.user)
response = self.client.get("/0000/00/00/0-invalid/")
self.assertEqual(response.status_code, 404)


class EntryHistoryTagTest(TestCase):
Expand Down
24 changes: 16 additions & 8 deletions tutorials/09-readable-urls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,26 @@ What would happen if we changed the slug or an invalid date was given
in the URL? This shouldn't matter because we only check for the model
``pk``.

Let's write a test for this case to make sure the correct page is
displayed in this case. Our test should look like this:
Let's write a couple more tests for this case to make sure the correct
page is displayed in this case, and for when the id does not exist. Our
tests should look like this:

.. code-block:: python
def test_invalid_url(self):
entry = Entry.objects.create(title="title", body="body",
author=self.user)
response = self.client.get("/0000/00/00/{0}-invalid/".format(entry.id))
def test_misdated_url(self):
entry = Entry.objects.create(
title="title", body="body", author=self.user)
url = "/0000/00/00/{0}-misdated/".format(entry.id)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response,
template_name='blog/entry_detail.html')
self.assertTemplateUsed(
response, template_name='blog/entry_detail.html')
def test_invalid_url(self):
entry = Entry.objects.create(
title="title", body="body", author=self.user)
response = self.client.get("/0000/00/00/0-invalid/")
self.assertEqual(response.status_code, 404)
Now let's run our tests and make sure they still pass.

Expand Down

0 comments on commit 17a2a45

Please sign in to comment.