Skip to content

Commit 31ed75d

Browse files
committed
lots more in 19
1 parent ead9eaf commit 31ed75d

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

chapter_19_mocking.asciidoc

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ So let's call `send_mail`, naively:
232232

233233

234234
[role="sourcecode"]
235-
.src/accounts/views.py
235+
.src/accounts/views.py (ch19l006-1)
236236
====
237237
[source,python]
238238
----
@@ -259,7 +259,7 @@ Let's try this:
259259

260260

261261
[role="sourcecode"]
262-
.src/accounts/views.py
262+
.src/accounts/views.py (ch19l006-2)
263263
====
264264
[source,python]
265265
----
@@ -281,7 +281,7 @@ something like this:
281281

282282

283283
[role="sourcecode"]
284-
.src/accounts/views.py
284+
.src/accounts/views.py (ch19l006)
285285
====
286286
[source,python]
287287
----
@@ -512,8 +512,8 @@ First let's get back to our FT and see where it's failing:
512512
----
513513
$ pass:quotes[*python src/manage.py test functional_tests.test_login*]
514514
[...]
515-
AssertionError: 'Check your email' not found in 'Superlists\nEnter email to log
516-
in:\nStart a new To-Do list'
515+
AssertionError: 'Check your email' not found in 'Superlists\nEnter your email
516+
to log in\nStart a new To-Do list'
517517
----
518518

519519
Submitting the email address currently has no effect,
@@ -586,7 +586,7 @@ And we can get it passing with:
586586

587587

588588
[role="sourcecode"]
589-
.src/accounts/views.py (ch17l014)
589+
.src/accounts/views.py (ch19l014)
590590
====
591591
[source,python]
592592
----
@@ -597,7 +597,7 @@ def send_login_email(request):
597597
[...]
598598
messages.success(
599599
request,
600-
"Check your email, we've sent you a link you can use to log in."
600+
"Check your email, we've sent you a link you can use to log in.",
601601
)
602602
return redirect("/")
603603
----
@@ -653,7 +653,7 @@ achieve the same result. I could write the code like this:
653653
messages.add_message(
654654
request,
655655
messages.SUCCESS,
656-
"Check your email, we've sent you a link you can use to log in."
656+
"Check your email, we've sent you a link you can use to log in.",
657657
)
658658
----
659659
====
@@ -730,15 +730,15 @@ Let's just cheat for now though, by changing the value in the view:
730730

731731

732732
[role="sourcecode"]
733-
.src/accounts/views.py
733+
.src/accounts/views.py (ch19l016)
734734
====
735735
[source,python]
736736
----
737737
send_mail(
738-
'Your login link for Superlists',
739-
'Use this link to log in',
740-
'noreply@superlists',
741-
[email]
738+
"Your login link for Superlists",
739+
"Use this link to log in",
740+
"noreply@superlists",
741+
[email],
742742
)
743743
----
744744
====
@@ -763,15 +763,14 @@ just cheats:
763763

764764

765765
[role="sourcecode"]
766-
.src/accounts/tests/test_views.py (ch17l017)
766+
.src/accounts/tests/test_views.py (ch19l017)
767767
====
768768
[source,python]
769769
----
770770
class LoginViewTest(TestCase):
771-
772771
def test_redirects_to_home_page(self):
773-
response = self.client.get('/accounts/login?token=abcd123')
774-
self.assertRedirects(response, '/')
772+
response = self.client.get("/accounts/login?token=abcd123")
773+
self.assertRedirects(response, "/")
775774
----
776775
====
777776

@@ -793,7 +792,7 @@ code was 404 (expected 302)
793792

794793
* No view:
795794
+
796-
[role="dofirst-ch17l018 small-code"]
795+
[role="dofirst-ch19l018 small-code"]
797796
----
798797
AttributeError: module 'accounts.views' has no attribute 'login'
799798
----
@@ -894,7 +893,7 @@ from accounts.models import Token
894893
[...]
895894
896895
def send_login_email(request):
897-
email = request.POST['email']
896+
email = request.POST["email"]
898897
token = Token.objects.create(email=email)
899898
send_mail(
900899
[...]
@@ -904,7 +903,7 @@ def send_login_email(request):
904903
And now the second test prompts us to actually use the token in the body
905904
of our email:
906905

907-
[subs="specialcharacters,macros"]
906+
[subs=""]
908907
----
909908
[...]
910909
AssertionError:
@@ -918,7 +917,7 @@ So we can insert the token into our email like this:
918917

919918

920919
[role="sourcecode"]
921-
.src/accounts/views.py (ch19l023)
920+
.src/accounts/views.py (ch17l023)
922921
====
923922
[source,python]
924923
----
@@ -1416,7 +1415,7 @@ duplication in our tests. Let's see how it looks:
14161415
[source,python]
14171416
----
14181417
@mock.patch("accounts.views.auth") # <1>
1419-
def test_calls_authenticate_with_uid_from_get_request(self, mock_auth): # <1>
1418+
def test_calls_authenticate_with_uid_from_get_request(self, mock_auth): # <2>
14201419
self.client.get("/accounts/login?token=abcd123")
14211420
self.assertEqual(
14221421
mock_auth.authenticate.call_args, # <3>

tests/test_chapter_19_mocking.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
import os
23
import unittest
34

45
from book_tester import ChapterTest
@@ -24,11 +25,10 @@ def test_listings_and_commands_and_output(self):
2425
# self.sourcetree.run_command("rm src/accounts/tests.py")
2526

2627
# hack fast-forward
27-
skip = False
28-
if skip:
29-
self.pos = 100
28+
if os.environ.get("SKIP"):
29+
self.pos = 40
3030
self.sourcetree.run_command(
31-
"git checkout {0}".format(self.sourcetree.get_commit_spec("ch16l047"))
31+
"git checkout {}".format(self.sourcetree.get_commit_spec("ch19l017"))
3232
)
3333

3434
while self.pos < len(self.listings):

0 commit comments

Comments
 (0)