Skip to content

Commit 68ca83b

Browse files
committed
progress on getting tests in
1 parent 0d98835 commit 68ca83b

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

chapter_20_fixtures_and_wait_decorator.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ def wait(fn): #<1>
400400
----
401401
====
402402

403+
// TODO: introduce blank line before "return modified_fn"
404+
403405
<1> A decorator is a way of modifying a function;
404406
it takes a function as an [keep-together]#argument...#
405407

chapter_21_server_side_debugging.asciidoc

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ PLAYBOOK: ansible-provision.yaml ***********************************************
4141

4242
Here's what happens when we run the functional tests:
4343

44-
[role="small-code"]
44+
[role="against-server small-code"]
4545
[subs="specialcharacters,macros"]
4646
----
4747
$ pass:quotes[*TEST_SERVER=staging.ottg.co.uk python src/manage.py test functional_tests*]
4848
4949
======================================================================
50-
ERROR: test_logged_in_users_lists_are_saved_as_my_lists
51-
(functional_tests.test_my_lists.MyListsTest)
50+
ERROR: test_logged_in_users_lists_are_saved_as_my_lists (functional_tests.test_
51+
my_lists.MyListsTest.test_logged_in_users_lists_are_saved_as_my_lists)
5252
---------------------------------------------------------------------
5353
Traceback (most recent call last):
5454
File "...goat-book/functional_tests/test_my_lists.py", line 34, in
@@ -100,7 +100,7 @@ First, make sure your 'settings.py' still contains the `LOGGING`
100100
settings which will actually send stuff to the console:
101101

102102
[role="sourcecode currentcontents"]
103-
.superlists/settings.py
103+
.src/superlists/settings.py
104104
====
105105
[source,python]
106106
----
@@ -173,7 +173,7 @@ elspeth@server:$ *docker restart superlists*
173173

174174
Now if we rerun our FTs, we see a change:
175175

176-
[role="small-code"]
176+
[role="against-server small-code"]
177177
[subs="specialcharacters,macros"]
178178
----
179179
$ pass:quotes[*TEST_SERVER=staging.ottg.co.uk python src/manage.py test functional_tests*]
@@ -260,6 +260,7 @@ import time
260260
print("getting msg", i)
261261
_, lines, __ = inbox.retr(i)
262262
lines = [l.decode("utf8") for l in lines]
263+
print(lines)
263264
if f"Subject: {subject}" in lines:
264265
email_id = i
265266
body = "\n".join(lines)
@@ -384,7 +385,7 @@ And, believe it or not, that'll actually work, and give us an FT
384385
that can actually check for logins that work, involving real emails!
385386

386387

387-
[role="small-code"]
388+
[role="against-server small-code"]
388389
[subs="specialcharacters,macros"]
389390
----
390391
$ pass:quotes[*TEST_SERVER=staging.ottg.co.uk python src/manage.py test functional_tests.test_login*]
@@ -461,30 +462,31 @@ $ <strong>mkdir -p src/functional_tests/management/commands</strong>
461462
$ <strong>touch src/functional_tests/management/__init__.py</strong>
462463
$ <strong>touch src/functional_tests/management/commands/__init__.py</strong>
463464
----
465+
//ch21l012-1
464466

465467
The boilerplate in a management command is a class that inherits from
466468
`django.core.management.BaseCommand`, and that defines a method called
467469
`handle`:
468470

469471
[role="sourcecode"]
470-
.src/functional_tests/management/commands/create_session.py
472+
.src/functional_tests/management/commands/create_session.py (ch21l012)
471473
====
472474
[source,python]
473475
----
474476
from django.conf import settings
475477
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
476-
User = get_user_model()
477478
from django.contrib.sessions.backends.db import SessionStore
478479
from django.core.management.base import BaseCommand
479480
481+
User = get_user_model()
480482
481-
class Command(BaseCommand):
482483
484+
class Command(BaseCommand):
483485
def add_arguments(self, parser):
484-
parser.add_argument('email')
486+
parser.add_argument("email")
485487
486488
def handle(self, *args, **options):
487-
session_key = create_pre_authenticated_session(options['email'])
489+
session_key = create_pre_authenticated_session(options["email"])
488490
self.stdout.write(session_key)
489491
490492
@@ -497,35 +499,33 @@ def create_pre_authenticated_session(email):
497499
return session.session_key
498500
----
499501
====
500-
//12
501502

502503
We've taken the code for `create_pre_authenticated_session` from
503504
'test_my_lists.py'. `handle` will pick up an email address from the parser,
504505
and then return the session key that we'll want to add to our browser cookies,
505506
and the management command prints it out at the command line. Try it out:
506507

507-
//IDEA: test commands that have return code
508-
[role="skipme"]
508+
[role="ignore-errors"]
509509
[subs="specialcharacters,macros"]
510510
----
511511
$ pass:quotes[*python src/manage.py create_session [email protected]*]
512-
Unknown command: 'create_session'
512+
Unknown command: 'create_session'. Did you mean clearsessions?
513513
----
514514

515515
One more step: we need to add `functional_tests` to our 'settings.py'
516516
for it to recognise it as a real app that might have management commands as
517517
well as tests:
518518

519519
[role="sourcecode"]
520-
.superlists/settings.py
520+
.src/superlists/settings.py (ch21l014)
521521
====
522522
[source,python]
523523
----
524524
+++ b/superlists/settings.py
525525
@@ -42,6 +42,7 @@ INSTALLED_APPS = [
526-
'lists',
527-
'accounts',
528-
+ 'functional_tests',
526+
"lists",
527+
"accounts",
528+
+ "functional_tests",
529529
]
530530
----
531531
====
@@ -557,25 +557,29 @@ and make it run the management command on the staging server if we're on that:
557557
[source,python]
558558
----
559559
from django.conf import settings
560+
560561
from .base import FunctionalTest
561-
from .server_tools import create_session_on_server
562562
from .management.commands.create_session import create_pre_authenticated_session
563+
from .server_tools import create_session_on_server
563564
564-
class MyListsTest(FunctionalTest):
565565
566+
class MyListsTest(FunctionalTest):
566567
def create_pre_authenticated_session(self, email):
567568
if self.test_server:
568569
session_key = create_session_on_server(self.test_server, email)
569570
else:
570571
session_key = create_pre_authenticated_session(email)
572+
571573
## to set a cookie we need to first visit the domain.
572574
## 404 pages load the quickest!
573575
self.browser.get(self.live_server_url + "/404_no_such_url/")
574-
self.browser.add_cookie(dict(
575-
name=settings.SESSION_COOKIE_NAME,
576-
value=session_key,
577-
path='/',
578-
))
576+
self.browser.add_cookie(
577+
dict(
578+
name=settings.SESSION_COOKIE_NAME,
579+
value=session_key,
580+
path="/",
581+
)
582+
)
579583
580584
[...]
581585
----
@@ -595,7 +599,6 @@ from .server_tools import reset_database #<1>
595599
[...]
596600
597601
class FunctionalTest(StaticLiveServerTestCase):
598-
599602
def setUp(self):
600603
self.browser = webdriver.Firefox()
601604
self.test_server = os.environ.get("TEST_SERVER")
@@ -632,15 +635,13 @@ from fabric.context_managers import settings, shell_env
632635
633636
634637
def _get_manage_dot_py(host):
635-
return f'~/sites/{host}/.venv/bin/python ~/sites/{host}/manage.py'
638+
return f"~/sites/{host}/.venv/bin/python ~/sites/{host}/manage.py"
636639
637640
638641
def reset_database(host):
639642
manage_dot_py = _get_manage_dot_py(host)
640-
with settings(host_string=f'elspeth@{host}'): #<1>
641-
run(f'{manage_dot_py} flush --noinput') #<2>
642-
643-
643+
with settings(host_string=f"elspeth@{host}"): # <1>
644+
run(f"{manage_dot_py} flush --noinput") # <2>
644645
----
645646
====
646647

@@ -832,7 +833,7 @@ Before we finish, let's update our deployment fabfile so that it can
832833
automatically add the `EMAIL_PASSWORD` to the _.env_ file on the server:
833834

834835

835-
[role="sourcecode"]
836+
[role="sourcecode dofirst-ch21l022"]
836837
.deploy_tools/fabfile.py (ch18l021)
837838
====
838839
[source,python]

tests/test_chapter_21_server_side_debugging.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3.7
22
import os
3-
import subprocess
43
import unittest
54

65
from book_tester import DO_SERVER_COMMANDS, ChapterTest

0 commit comments

Comments
 (0)