Skip to content

Commit 11854a4

Browse files
committed
wip on blackifying post and db chapter listings
1 parent 7557e9e commit 11854a4

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

chapter_post_and_database.asciidoc

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Now, running our FTs gives us a slightly cryptic, unexpected error:
7070
$ pass:quotes[*python functional_tests.py*]
7171
[...]
7272
Traceback (most recent call last):
73-
File "...python-tdd-book/functional_tests.py", line 41, in
73+
File "...python-tdd-book/functional_tests.py", line 38, in
7474
test_can_start_a_list_and_retrieve_it_later
75-
table = self.browser.find_element(By.ID, 'id_list_table')
75+
table = self.browser.find_element(By.ID, "id_list_table")
7676
[...]
7777
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
7878
element: [id="id_list_table"]
@@ -362,10 +362,11 @@ ERROR: test_uses_home_template
362362
(lists.tests.HomePageTest.test_uses_home_template)
363363
364364
[...]
365-
File "...python-tdd-book/lists/views.py", line 5, in home_page
366365
{"new_item_text": request.POST["item_text"]},
366+
~~~~~~~~~~~~^^^^^^^^^^^^^
367367
[...]
368368
django.utils.datastructures.MultiValueDictKeyError: 'item_text'
369+
369370
----
370371

371372

@@ -391,7 +392,7 @@ site manually, and we can get on with fixing it straight away. Here's how:
391392

392393

393394
[role="sourcecode"]
394-
.lists/views.py
395+
.lists/views.py (ch05l010)
395396
====
396397
[source,python]
397398
----
@@ -436,7 +437,7 @@ improved error messages stay around to help debug any future errors:
436437
----
437438
self.assertTrue(
438439
any(row.text == "1: Buy peacock feathers" for row in rows),
439-
f"New to-do item did not appear in table. Contents were:\n{table.text}" #<1>
440+
f"New to-do item did not appear in table. Contents were:\n{table.text}", #<1>
440441
)
441442
----
442443
====
@@ -541,13 +542,12 @@ check for adding a second item to the table (copy and paste is our friend), we
541542
begin to see that our first cut solution really isn't going to, um, cut it:
542543

543544
[role="sourcecode"]
544-
.functional_tests.py
545+
.functional_tests.py (ch05l014)
545546
====
546547
[source,python]
547548
----
548-
def thing():
549-
# There is still a text box inviting her to add another item. She
550-
# enters "Use peacock feathers to make a fly"
549+
# There is still a text box inviting her to add another item.
550+
# She enters "Use peacock feathers to make a fly"
551551
# (Edith is very methodical)
552552
inputbox = self.browser.find_element(By.ID, "id_new_item")
553553
inputbox.send_keys("Use peacock feathers to make a fly")
@@ -623,20 +623,18 @@ only methods that begin with `test_` will get run as tests, so you can use
623623
other methods for your own purposes:
624624

625625
[role="sourcecode"]
626-
.functional_tests.py
626+
.functional_tests.py (ch05l015)
627627
====
628628
[source,python]
629629
----
630630
def tearDown(self):
631631
self.browser.quit()
632632
633-
634633
def check_for_row_in_list_table(self, row_text):
635-
table = self.browser.find_element(By.ID, 'id_list_table')
636-
rows = table.find_elements(By.TAG_NAME, 'tr')
634+
table = self.browser.find_element(By.ID, "id_list_table")
635+
rows = table.find_elements(By.TAG_NAME, "tr")
637636
self.assertIn(row_text, [row.text for row in rows])
638637
639-
640638
def test_can_start_a_list_and_retrieve_it_later(self):
641639
[...]
642640
----
@@ -646,27 +644,27 @@ I like to put helper methods near the top of the class, between the `tearDown`
646644
and the first test. Let's use it in the FT:
647645

648646
[role="sourcecode"]
649-
.functional_tests.py
647+
.functional_tests.py (ch05l016)
650648
====
651649
[source,python]
652650
----
653651
# When she hits enter, the page updates, and now the page lists
654652
# "1: Buy peacock feathers" as an item in a to-do list table
655653
inputbox.send_keys(Keys.ENTER)
656654
time.sleep(1)
657-
self.check_for_row_in_list_table('1: Buy peacock feathers')
655+
self.check_for_row_in_list_table("1: Buy peacock feathers")
658656
659-
# There is still a text box inviting her to add another item. She
660-
# enters "Use peacock feathers to make a fly" (Edith is very
661-
# methodical)
662-
inputbox = self.browser.find_element(By.ID, 'id_new_item')
663-
inputbox.send_keys('Use peacock feathers to make a fly')
657+
# There is still a text box inviting her to add another item.
658+
# She enters "Use peacock feathers to make a fly"
659+
# (Edith is very methodical)
660+
inputbox = self.browser.find_element(By.ID, "id_new_item")
661+
inputbox.send_keys("Use peacock feathers to make a fly")
664662
inputbox.send_keys(Keys.ENTER)
665663
time.sleep(1)
666664
667665
# The page updates again, and now shows both items on her list
668-
self.check_for_row_in_list_table('1: Buy peacock feathers')
669-
self.check_for_row_in_list_table('2: Use peacock feathers to make a fly')
666+
self.check_for_row_in_list_table("1: Buy peacock feathers")
667+
self.check_for_row_in_list_table("2: Use peacock feathers to make a fly")
670668
671669
# Edith wonders whether the site will remember her list. Then she sees
672670
[...]
@@ -713,31 +711,30 @@ how we want it to work.
713711
Let's create a new class in 'lists/tests.py':
714712

715713
[role="sourcecode"]
716-
.lists/tests.py
714+
.lists/tests.py (ch05l017)
717715
====
718716
[source,python]
719717
----
720718
from lists.models import Item
721719
[...]
722720
723721
class ItemModelTest(TestCase):
724-
725722
def test_saving_and_retrieving_items(self):
726723
first_item = Item()
727-
first_item.text = 'The first (ever) list item'
724+
first_item.text = "The first (ever) list item"
728725
first_item.save()
729726
730727
second_item = Item()
731-
second_item.text = 'Item the second'
728+
second_item.text = "Item the second"
732729
second_item.save()
733730
734731
saved_items = Item.objects.all()
735732
self.assertEqual(saved_items.count(), 2)
736733
737734
first_saved_item = saved_items[0]
738735
second_saved_item = saved_items[1]
739-
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
740-
self.assertEqual(second_saved_item.text, 'Item the second')
736+
self.assertEqual(first_saved_item.text, "The first (ever) list item")
737+
self.assertEqual(second_saved_item.text, "Item the second")
741738
----
742739
====
743740

@@ -803,6 +800,7 @@ creating a class:
803800
----
804801
from django.db import models
805802
803+
806804
class Item(object):
807805
pass
808806
----
@@ -829,6 +827,7 @@ model, we make it inherit from the `Model` class:
829827
----
830828
from django.db import models
831829
830+
832831
class Item(models.Model):
833832
pass
834833
----
@@ -967,7 +966,7 @@ self-explanatory:
967966
[source,python]
968967
----
969968
class Item(models.Model):
970-
text = models.TextField(default='')
969+
text = models.TextField(default="")
971970
----
972971
====
973972

@@ -1033,14 +1032,14 @@ response. We can do that by adding three new lines to the existing test called
10331032
[source,python]
10341033
----
10351034
def test_can_save_a_POST_request(self):
1036-
response = self.client.post('/', data={'item_text': 'A new list item'})
1035+
response = self.client.post("/", data={"item_text": "A new list item"})
10371036
1038-
self.assertEqual(Item.objects.count(), 1) #<1>
1039-
new_item = Item.objects.first() #<2>
1040-
self.assertEqual(new_item.text, 'A new list item') #<3>
1037+
self.assertEqual(Item.objects.count(), 1) # <1>
1038+
new_item = Item.objects.first() # <2>
1039+
self.assertEqual(new_item.text, "A new list item") # <3>
10411040
1042-
self.assertIn('A new list item', response.content.decode())
1043-
self.assertTemplateUsed(response, 'home.html')
1041+
self.assertIn("A new list item", response.content.decode())
1042+
self.assertTemplateUsed(response, "home.html")
10441043
----
10451044
====
10461045

@@ -1083,14 +1082,17 @@ Let's adjust our view:
10831082
from django.shortcuts import render
10841083
from lists.models import Item
10851084
1085+
10861086
def home_page(request):
10871087
item = Item()
1088-
item.text = request.POST.get('item_text', '')
1088+
item.text = request.POST.get("item_text", "")
10891089
item.save()
10901090
1091-
return render(request, 'home.html', {
1092-
'new_item_text': request.POST.get('item_text', ''),
1093-
})
1091+
return render(
1092+
request,
1093+
"home.html",
1094+
{"new_item_text": request.POST.get("item_text", "")},
1095+
)
10941096
----
10951097
====
10961098

@@ -1115,9 +1117,14 @@ refactoring:
11151117
====
11161118
[source,python]
11171119
----
1118-
return render(request, 'home.html', {
1119-
'new_item_text': item.text
1120-
})
1120+
[...]
1121+
item.save()
1122+
1123+
return render(
1124+
request,
1125+
"home.html",
1126+
{"new_item_text": item.text},
1127+
)
11211128
----
11221129
====
11231130

@@ -1137,7 +1144,7 @@ test, but it's best to keep unit tests to testing one thing at a time, so let's
11371144
add a new one:
11381145

11391146
[role="sourcecode"]
1140-
.lists/tests.py
1147+
.lists/tests.py (ch05l022)
11411148
====
11421149
[source,python]
11431150
----
@@ -1155,20 +1162,22 @@ quite a small change to the logic of the view, there are quite a few little
11551162
tweaks to the implementation in code:
11561163

11571164
[role="sourcecode"]
1158-
.lists/views.py
1165+
.lists/views.py (ch05l023)
11591166
====
11601167
[source,python]
11611168
----
11621169
def home_page(request):
1163-
if request.method == 'POST':
1164-
new_item_text = request.POST['item_text'] #<1>
1165-
Item.objects.create(text=new_item_text) #<2>
1170+
if request.method == "POST":
1171+
new_item_text = request.POST["item_text"] # <1>
1172+
Item.objects.create(text=new_item_text) # <2>
11661173
else:
1167-
new_item_text = '' #<1>
1174+
new_item_text = "" # <1>
11681175
1169-
return render(request, 'home.html', {
1170-
'new_item_text': new_item_text, #<1>
1171-
})
1176+
return render(
1177+
request,
1178+
"home.html",
1179+
{"new_item_text": new_item_text}, # <1>
1180+
)
11721181
----
11731182
====
11741183

@@ -1239,12 +1248,13 @@ substantially:
12391248
from django.shortcuts import redirect, render
12401249
from lists.models import Item
12411250
1251+
12421252
def home_page(request):
1243-
if request.method == 'POST':
1244-
Item.objects.create(text=request.POST['item_text'])
1245-
return redirect('/')
1253+
if request.method == "POST":
1254+
Item.objects.create(text=request.POST["item_text"])
1255+
return redirect("/")
12461256
1247-
return render(request, 'home.html')
1257+
return render(request, "home.html")
12481258
----
12491259
====
12501260

tests/sourcetree.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ def check_listing_matches_commit(listing, commit, future_contents):
224224
if new_line.strip() not in stripped_listing_lines:
225225
# print('stripped_listing_lines', stripped_listing_lines)
226226
raise ApplyCommitException(
227-
'could not find commit new line {0} in listing:\n{1}'.format(
228-
new_line, listing.contents
229-
)
227+
f"could not find commit new line {new_line!r} in listing:\n{listing.contents}"
230228
)
231229

232230
future_lines = future_contents.split('\n')

0 commit comments

Comments
 (0)