Skip to content

Commit 928eefa

Browse files
committed
chapter working incrementally done probably
1 parent 2d7ca3e commit 928eefa

File tree

3 files changed

+40
-69
lines changed

3 files changed

+40
-69
lines changed

chapter_working_incrementally.asciidoc

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,11 +1571,11 @@ https://docs.python.org/3/reference/lexical_analysis.html#f-strings[docs]
15711571
Running the unit tests gives an expected 404, and another related error:
15721572

15731573
----
1574-
FAIL: test_displays_only_items_for_that_list (lists.tests.ListViewTest)
1574+
FAIL: test_displays_only_items_for_that_list
15751575
AssertionError: 404 != 200 : Couldn't retrieve content: Response code was 404
15761576
(expected 200)
15771577
[...]
1578-
FAIL: test_uses_list_template (lists.tests.ListViewTest)
1578+
FAIL: test_uses_list_template
15791579
AssertionError: No templates used to render the response
15801580
----
15811581

@@ -1595,7 +1595,7 @@ It's time to learn how we can pass parameters from URLs to views:
15951595
urlpatterns = [
15961596
path('', views.home_page, name='home'),
15971597
path('lists/new', views.new_list, name='new_list'),
1598-
path('lists/<list_id>/', views.view_list, name='view_list'),
1598+
path('lists/<int:list_id>/', views.view_list, name='view_list'),
15991599
]
16001600
----
16011601
====
@@ -1614,18 +1614,20 @@ If we go to '/lists/foo/', we get `view_list(request, "foo")`.
16141614
But our view doesn't expect an argument yet! Sure enough, this causes problems:
16151615

16161616
----
1617-
ERROR: test_displays_only_items_for_that_list (lists.tests.ListViewTest)
1617+
ERROR: test_displays_only_items_for_that_list
16181618
[...]
1619-
TypeError: view_list() takes 1 positional argument but 2 were given
1619+
TypeError: view_list() got an unexpected keyword argument 'list_id'
16201620
[...]
1621-
ERROR: test_uses_list_template (lists.tests.ListViewTest)
1621+
ERROR: test_uses_list_template
16221622
[...]
1623-
TypeError: view_list() takes 1 positional argument but 2 were given
1623+
TypeError: view_list() got an unexpected keyword argument 'list_id'
16241624
[...]
1625-
ERROR: test_redirects_after_POST (lists.tests.NewListTest)
1625+
FAIL: test_redirects_after_POST
16261626
[...]
1627-
TypeError: view_list() takes 1 positional argument but 2 were given
1628-
FAILED (errors=3)
1627+
AssertionError: 404 != 200 : Couldn't retrieve redirection page
1628+
'/lists/the-only-list-in-the-world/': response code was 404 (expected 200)
1629+
[...]
1630+
FAILED (failures=1, errors=2)
16291631
----
16301632

16311633
We can fix that easily with a dummy parameter in 'views.py':
@@ -1643,7 +1645,7 @@ def view_list(request, list_id):
16431645
Now we're down to our expected failure:
16441646

16451647
----
1646-
FAIL: test_displays_only_items_for_that_list (lists.tests.ListViewTest)
1648+
FAIL: test_displays_only_items_for_that_list
16471649
[...]
16481650
AssertionError: 1 != 0 : Response should not contain 'other list item 1'
16491651
----
@@ -1669,12 +1671,13 @@ def view_list(request, list_id):
16691671
Adjusting new_list to the New World
16701672
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16711673

1672-
Oops, now we get errors in another test:
1674+
Oops, now we get a failure in another test:
16731675

16741676
----
1675-
ERROR: test_redirects_after_POST (lists.tests.NewListTest)
1676-
ValueError: invalid literal for int() with base 10:
1677-
'the-only-list-in-the-world'
1677+
FAIL: test_redirects_after_POST
1678+
1679+
AssertionError: 404 != 200 : Couldn't retrieve redirection page
1680+
'/lists/the-only-list-in-the-world/': response code was 404 (expected 200)
16781681
----
16791682

16801683
Let's take a look at this test then, since it's moaning:
@@ -1756,7 +1759,7 @@ Well, almost:
17561759
F.
17571760
======================================================================
17581761
FAIL: test_can_start_a_list_for_one_user
1759-
(functional_tests.tests.NewVisitorTest)
1762+
(functional_tests.tests.NewVisitorTest.test_can_start_a_list_for_one_user)
17601763
---------------------------------------------------------------------
17611764
Traceback (most recent call last):
17621765
File "...python-tdd-book/functional_tests/tests.py", line 68, in
@@ -1842,44 +1845,20 @@ NOTE: Are you wondering about `other_list`? A bit like in the tests for
18421845
of those, after all). It's a judgement call, but this one feels worth it.
18431846
There's some more discussion of this in <<testing-for-stupidity>>.
18441847

1845-
We get:
1848+
1849+
And that fails as expected, the list item is not saved,
1850+
and the new URL currently returns a 404:
18461851

18471852
----
18481853
AssertionError: 0 != 1
18491854
[...]
1850-
AssertionError: 301 != 302 : Response didn't redirect as expected: Response
1851-
code was 301 (expected 302)
1855+
AssertionError: 404 != 302 : Response didn't redirect as expected: Response
1856+
code was 404 (expected 302)
18521857
----
18531858

1859+
////
1860+
(old code
18541861

1855-
Beware of Greedy Regular Expressions!
1856-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1857-
1858-
1859-
((("regular expressions")))((("greedy regular expressions")))That's
1860-
a little strange. We haven't actually specified a URL for
1861-
'/lists/1/add_item' yet, so our expected failure is `404 != 302`. Why are we
1862-
getting a 301?
1863-
1864-
This was a bit of a puzzler! It's because we've used a very "greedy"
1865-
capture group in our URL:
1866-
1867-
1868-
[role="sourcecode currentcontents"]
1869-
.superlists/urls.py
1870-
====
1871-
[source,python]
1872-
----
1873-
path('lists/<list_id>/', views.view_list, name='view_list'),
1874-
----
1875-
====
1876-
1877-
((("HTML", "POST requests", "redirect following")))((("POST requests", "redirect following")))((("permanent redirect (301)")))Django
1878-
has some built-in code to issue a permanent redirect (301) whenever
1879-
someone asks for a URL which is 'almost' right, except for a missing slash.
1880-
In this case, [keep-together]#'/lists/1/add_item/'# would be a match for
1881-
`lists/(.+)/`, with the `(.+)` capturing `1/add_item`. So Django "helpfully"
1882-
guesses that we actually wanted the URL with a trailing slash.
18831862

18841863
We can fix that by making our URL pattern explicitly capture only numerical
18851864
digits, by using the prefix `int:`:
@@ -1894,20 +1873,12 @@ digits, by using the prefix `int:`:
18941873
====
18951874
//38
18961875

1897-
That gives us the failure we expected:
1876+
////
18981877

1899-
----
1900-
AssertionError: 0 != 1
1901-
[...]
1902-
AssertionError: 404 != 302 : Response didn't redirect as expected: Response
1903-
code was 404 (expected 302)
1904-
----
19051878

19061879
The Last New URL
19071880
^^^^^^^^^^^^^^^^
19081881

1909-
1910-
19111882
Now we've got our expected 404, let's add a new URL for adding new items to
19121883
existing lists:
19131884

@@ -1917,10 +1888,10 @@ existing lists:
19171888
[source,python]
19181889
----
19191890
urlpatterns = [
1920-
url(r'^$', views.home_page, name='home'),
1921-
url(r'^lists/new$', views.new_list, name='new_list'),
1922-
url(r'^lists/(\d+)/$', views.view_list, name='view_list'),
1923-
url(r'^lists/(\d+)/add_item$', views.add_item, name='add_item'),
1891+
path('', views.home_page, name='home'),
1892+
path('lists/new', views.new_list, name='new_list'),
1893+
path('lists/<int:list_id>/', views.view_list, name='view_list'),
1894+
path('lists/<int:list_id>/add_item', views.add_item, name='add_item'),
19241895
]
19251896
----
19261897
====
@@ -1964,7 +1935,7 @@ def add_item(request):
19641935
Aha:
19651936

19661937
----
1967-
TypeError: add_item() takes 1 positional argument but 2 were given
1938+
TypeError: add_item() got an unexpected keyword argument 'list_id'
19681939
----
19691940

19701941

@@ -2113,7 +2084,7 @@ That, of course, will break one of our old tests, because the template
21132084
needed `items`:
21142085

21152086
----
2116-
FAIL: test_displays_only_items_for_that_list (lists.tests.ListViewTest)
2087+
FAIL: test_displays_only_items_for_that_list
21172088
[...]
21182089
AssertionError: False is not true : Couldn't find 'itemey 1' in response
21192090
----
@@ -2244,13 +2215,13 @@ of our three URLs, and none of the other stuff from the parent 'urls.py':
22442215
====
22452216
[source,python]
22462217
----
2247-
from django.conf.urls import url
2218+
from django.urls import path
22482219
from lists import views
22492220
22502221
urlpatterns = [
2251-
url(r'^new$', views.new_list, name='new_list'),
2252-
url(r'^(\d+)/$', views.view_list, name='view_list'),
2253-
url(r'^(\d+)/add_item$', views.add_item, name='add_item'),
2222+
path('lists/new', views.new_list, name='new_list'),
2223+
path('lists/<int:list_id>/', views.view_list, name='view_list'),
2224+
path('lists/<int:list_id>/add_item', views.add_item, name='add_item'),
22542225
]
22552226
----
22562227
====

tests/test_chapter_working_incrementally.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def test_listings_and_commands_and_output(self):
3636
# hack fast-forward
3737
skip = False
3838
if skip:
39-
self.pos = 106
39+
self.pos = 99
4040
self.sourcetree.run_command(
41-
"git checkout {}".format(self.sourcetree.get_commit_spec("ch07l036-1"))
41+
"git checkout {}".format(self.sourcetree.get_commit_spec("ch07l034"))
4242
)
4343

4444
while self.pos < touch_pos:

0 commit comments

Comments
 (0)