Skip to content

Commit 17a2a45

Browse files
committed
Merge pull request #101 from chrishiestand/patch-6
fix invalid_url, add misdated_url tests
2 parents 4477f80 + 6e19c45 commit 17a2a45

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

myblog/blog/tests.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,20 @@ def test_url(self):
176176
self.assertTemplateUsed(response,
177177
template_name='blog/entry_detail.html')
178178

179-
def test_invalid_url(self):
180-
entry = Entry.objects.create(title="title", body="body",
181-
author=self.user)
182-
response = self.client.get("/0000/00/00/{0}-invalid/".format(entry.id))
179+
def test_misdated_url(self):
180+
entry = Entry.objects.create(
181+
title="title", body="body", author=self.user)
182+
url = "/0000/00/00/{0}-misdated/".format(entry.id)
183+
response = self.client.get(url)
183184
self.assertEqual(response.status_code, 200)
184-
self.assertTemplateUsed(response,
185-
template_name='blog/entry_detail.html')
185+
self.assertTemplateUsed(
186+
response, template_name='blog/entry_detail.html')
187+
188+
def test_invalid_url(self):
189+
entry = Entry.objects.create(
190+
title="title", body="body", author=self.user)
191+
response = self.client.get("/0000/00/00/0-invalid/")
192+
self.assertEqual(response.status_code, 404)
186193

187194

188195
class EntryHistoryTagTest(TestCase):

tutorials/09-readable-urls.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,26 @@ What would happen if we changed the slug or an invalid date was given
178178
in the URL? This shouldn't matter because we only check for the model
179179
``pk``.
180180

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

184185
.. code-block:: python
185186
186-
def test_invalid_url(self):
187-
entry = Entry.objects.create(title="title", body="body",
188-
author=self.user)
189-
response = self.client.get("/0000/00/00/{0}-invalid/".format(entry.id))
187+
def test_misdated_url(self):
188+
entry = Entry.objects.create(
189+
title="title", body="body", author=self.user)
190+
url = "/0000/00/00/{0}-misdated/".format(entry.id)
191+
response = self.client.get(url)
190192
self.assertEqual(response.status_code, 200)
191-
self.assertTemplateUsed(response,
192-
template_name='blog/entry_detail.html')
193+
self.assertTemplateUsed(
194+
response, template_name='blog/entry_detail.html')
195+
196+
def test_invalid_url(self):
197+
entry = Entry.objects.create(
198+
title="title", body="body", author=self.user)
199+
response = self.client.get("/0000/00/00/0-invalid/")
200+
self.assertEqual(response.status_code, 404)
193201
194202
Now let's run our tests and make sure they still pass.
195203

0 commit comments

Comments
 (0)