Skip to content

Commit ebb6b2f

Browse files
committed
start on post and database chap
1 parent b79cd1f commit ebb6b2f

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

chapter_post_and_database.asciidoc

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ it a little:
110110
inputbox.send_keys(Keys.ENTER)
111111
time.sleep(10)
112112
113-
table = self.browser.find_element(By.ID, 'id_list_table')
113+
table = self.browser.find_element(By.ID, "id_list_table")
114114
----
115115
====
116116

@@ -199,7 +199,7 @@ can put our normal short `time.sleep` back now though:
199199
inputbox.send_keys(Keys.ENTER)
200200
time.sleep(1)
201201
202-
table = self.browser.find_element(By.ID, 'id_list_table')
202+
table = self.browser.find_element(By.ID, "id_list_table")
203203
----
204204
====
205205

@@ -223,13 +223,13 @@ and add a new method to `HomePageTest`:
223223
[source,python]
224224
----
225225
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")
228228
229229
230230
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())
233233
----
234234
====
235235

@@ -261,10 +261,11 @@ silly return value:
261261
from django.http import HttpResponse
262262
from django.shortcuts import render
263263
264+
264265
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")
268269
----
269270
====
270271

@@ -321,9 +322,9 @@ template:
321322
[source,python]
322323
----
323324
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")
327328
----
328329
====
329330

@@ -346,9 +347,11 @@ right down to:
346347
[source,python]
347348
----
348349
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+
)
352355
----
353356
====
354357

@@ -360,7 +363,7 @@ ERROR: test_uses_home_template
360363
361364
[...]
362365
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"]},
364367
[...]
365368
django.utils.datastructures.MultiValueDictKeyError: 'item_text'
366369
----
@@ -393,9 +396,11 @@ site manually, and we can get on with fixing it straight away. Here's how:
393396
[source,python]
394397
----
395398
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+
)
399404
----
400405
====
401406

@@ -430,7 +435,7 @@ improved error messages stay around to help debug any future errors:
430435
[source,python]
431436
----
432437
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),
434439
f"New to-do item did not appear in table. Contents were:\n{table.text}" #<1>
435440
)
436441
----
@@ -462,7 +467,7 @@ a much simpler implementation. We can replace all four lines of the
462467
====
463468
[source,python]
464469
----
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])
466471
----
467472
====
468473

@@ -471,7 +476,7 @@ clever, because what you're probably being is 'overcomplicated'. And we get
471476
the error message for free:
472477

473478
----
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])
475480
AssertionError: '1: Buy peacock feathers' not found in ['Buy peacock feathers']
476481
----
477482

@@ -490,7 +495,7 @@ quick "cheating" change to the template:
490495

491496

492497
[role="sourcecode"]
493-
.lists/templates/home.html
498+
.lists/templates/home.html (ch05l013)
494499
====
495500
[source,html]
496501
----
@@ -540,27 +545,31 @@ begin to see that our first cut solution really isn't going to, um, cut it:
540545
====
541546
[source,python]
542547
----
548+
def thing():
543549
# 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")
548554
inputbox.send_keys(Keys.ENTER)
549555
time.sleep(1)
550556
551557
# 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")
555560
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],
558567
)
559568
560569
# Edith wonders whether the site will remember her list. Then she sees
561570
# that the site has generated a unique URL for her -- there is some
562571
# explanatory text to that effect.
563-
self.fail('Finish the test!')
572+
self.fail("Finish the test!")
564573
565574
# She visits that URL - her to-do list is still there.
566575
----

0 commit comments

Comments
 (0)