@@ -110,7 +110,7 @@ it a little:
110
110
inputbox.send_keys(Keys.ENTER)
111
111
time.sleep(10)
112
112
113
- table = self.browser.find_element(By.ID, ' id_list_table' )
113
+ table = self.browser.find_element(By.ID, " id_list_table" )
114
114
----
115
115
====
116
116
@@ -199,7 +199,7 @@ can put our normal short `time.sleep` back now though:
199
199
inputbox.send_keys(Keys.ENTER)
200
200
time.sleep(1)
201
201
202
- table = self.browser.find_element(By.ID, ' id_list_table' )
202
+ table = self.browser.find_element(By.ID, " id_list_table" )
203
203
----
204
204
====
205
205
@@ -223,13 +223,13 @@ and add a new method to `HomePageTest`:
223
223
[source,python]
224
224
----
225
225
def test_uses_home_template(self):
226
- response = self.client.get('/' )
227
- self.assertTemplateUsed(response, ' home.html' )
226
+ response = self.client.get("/" )
227
+ self.assertTemplateUsed(response, " home.html" )
228
228
229
229
230
230
def test_can_save_a_POST_request(self):
231
- response = self.client.post('/' , data={' item_text': ' A new list item' })
232
- self.assertIn(' A new list item' , response.content.decode())
231
+ response = self.client.post("/" , data={" item_text": " A new list item" })
232
+ self.assertIn(" A new list item" , response.content.decode())
233
233
----
234
234
====
235
235
@@ -261,10 +261,11 @@ silly return value:
261
261
from django.http import HttpResponse
262
262
from django.shortcuts import render
263
263
264
+
264
265
def home_page(request):
265
- if request.method == ' POST' :
266
- return HttpResponse(request.POST[' item_text' ])
267
- return render(request, ' home.html' )
266
+ if request.method == " POST" :
267
+ return HttpResponse(request.POST[" item_text" ])
268
+ return render(request, " home.html" )
268
269
----
269
270
====
270
271
@@ -321,9 +322,9 @@ template:
321
322
[source,python]
322
323
----
323
324
def test_can_save_a_POST_request(self):
324
- response = self.client.post('/' , data={' item_text': ' A new list item' })
325
- self.assertIn(' A new list item' , response.content.decode())
326
- self.assertTemplateUsed(response, ' home.html' )
325
+ response = self.client.post("/" , data={" item_text": " A new list item" })
326
+ self.assertIn(" A new list item" , response.content.decode())
327
+ self.assertTemplateUsed(response, " home.html" )
327
328
----
328
329
====
329
330
@@ -346,9 +347,11 @@ right down to:
346
347
[source,python]
347
348
----
348
349
def home_page(request):
349
- return render(request, 'home.html', {
350
- 'new_item_text': request.POST['item_text'],
351
- })
350
+ return render(
351
+ request,
352
+ "home.html",
353
+ {"new_item_text": request.POST["item_text"]},
354
+ )
352
355
----
353
356
====
354
357
@@ -360,7 +363,7 @@ ERROR: test_uses_home_template
360
363
361
364
[...]
362
365
File "...python-tdd-book/lists/views.py", line 5, in home_page
363
- ' new_item_text' : request.POST[' item_text'] ,
366
+ {" new_item_text" : request.POST[" item_text"]} ,
364
367
[...]
365
368
django.utils.datastructures.MultiValueDictKeyError: 'item_text'
366
369
----
@@ -393,9 +396,11 @@ site manually, and we can get on with fixing it straight away. Here's how:
393
396
[source,python]
394
397
----
395
398
def home_page(request):
396
- return render(request, 'home.html', {
397
- 'new_item_text': request.POST.get('item_text', ''),
398
- })
399
+ return render(
400
+ request,
401
+ "home.html",
402
+ {"new_item_text": request.POST.get("item_text", "")},
403
+ )
399
404
----
400
405
====
401
406
@@ -430,7 +435,7 @@ improved error messages stay around to help debug any future errors:
430
435
[source,python]
431
436
----
432
437
self.assertTrue(
433
- any(row.text == ' 1: Buy peacock feathers' for row in rows),
438
+ any(row.text == " 1: Buy peacock feathers" for row in rows),
434
439
f"New to-do item did not appear in table. Contents were:\n{table.text}" #<1>
435
440
)
436
441
----
@@ -462,7 +467,7 @@ a much simpler implementation. We can replace all four lines of the
462
467
====
463
468
[source,python]
464
469
----
465
- self.assertIn(' 1: Buy peacock feathers' , [row.text for row in rows])
470
+ self.assertIn(" 1: Buy peacock feathers" , [row.text for row in rows])
466
471
----
467
472
====
468
473
@@ -471,7 +476,7 @@ clever, because what you're probably being is 'overcomplicated'. And we get
471
476
the error message for free:
472
477
473
478
----
474
- self.assertIn(' 1: Buy peacock feathers' , [row.text for row in rows])
479
+ self.assertIn(" 1: Buy peacock feathers" , [row.text for row in rows])
475
480
AssertionError: '1: Buy peacock feathers' not found in ['Buy peacock feathers']
476
481
----
477
482
@@ -490,7 +495,7 @@ quick "cheating" change to the template:
490
495
491
496
492
497
[role="sourcecode"]
493
- .lists/templates/home.html
498
+ .lists/templates/home.html (ch05l013)
494
499
====
495
500
[source,html]
496
501
----
@@ -540,27 +545,31 @@ begin to see that our first cut solution really isn't going to, um, cut it:
540
545
====
541
546
[source,python]
542
547
----
548
+ def thing():
543
549
# There is still a text box inviting her to add another item. She
544
- # enters "Use peacock feathers to make a fly" (Edith is very
545
- # methodical)
546
- inputbox = self.browser.find_element(By.ID, ' id_new_item' )
547
- inputbox.send_keys(' Use peacock feathers to make a fly' )
550
+ # enters "Use peacock feathers to make a fly"
551
+ # (Edith is very methodical)
552
+ inputbox = self.browser.find_element(By.ID, " id_new_item" )
553
+ inputbox.send_keys(" Use peacock feathers to make a fly" )
548
554
inputbox.send_keys(Keys.ENTER)
549
555
time.sleep(1)
550
556
551
557
# The page updates again, and now shows both items on her list
552
- table = self.browser.find_element(By.ID, 'id_list_table')
553
- rows = table.find_elements(By.TAG_NAME, 'tr')
554
- self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
558
+ table = self.browser.find_element(By.ID, "id_list_table")
559
+ rows = table.find_elements(By.TAG_NAME, "tr")
555
560
self.assertIn(
556
- '2: Use peacock feathers to make a fly',
557
- [row.text for row in rows]
561
+ "1: Buy peacock feathers",
562
+ [row.text for row in rows],
563
+ )
564
+ self.assertIn(
565
+ "2: Use peacock feathers to make a fly",
566
+ [row.text for row in rows],
558
567
)
559
568
560
569
# Edith wonders whether the site will remember her list. Then she sees
561
570
# that the site has generated a unique URL for her -- there is some
562
571
# explanatory text to that effect.
563
- self.fail(' Finish the test!' )
572
+ self.fail(" Finish the test!" )
564
573
565
574
# She visits that URL - her to-do list is still there.
566
575
----
0 commit comments