Skip to content

Commit 6df22d6

Browse files
committed
tests passing maybe
1 parent d738ce6 commit 6df22d6

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

chapter_20_fixtures_and_wait_decorator.asciidoc

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,36 @@ so this isn't an unrealistic cheat at all.
4343
Here's how you can set it up:
4444

4545
[role="sourcecode"]
46-
.src/functional_tests/test_my_lists.py
46+
.src/functional_tests/test_my_lists.py (ch20l001)
4747
====
4848
[source,python]
4949
----
5050
from django.conf import settings
5151
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
5252
from django.contrib.sessions.backends.db import SessionStore
53+
5354
from .base import FunctionalTest
55+
5456
User = get_user_model()
5557
5658
5759
class MyListsTest(FunctionalTest):
58-
5960
def create_pre_authenticated_session(self, email):
6061
user = User.objects.create(email=email)
6162
session = SessionStore()
62-
session[SESSION_KEY] = user.pk #<1>
63+
session[SESSION_KEY] = user.pk # <1>
6364
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
6465
session.save()
6566
## to set a cookie we need to first visit the domain.
6667
## 404 pages load the quickest!
6768
self.browser.get(self.live_server_url + "/404_no_such_url/")
68-
self.browser.add_cookie(dict(
69-
name=settings.SESSION_COOKIE_NAME,
70-
value=session.session_key, #<2>
71-
path='/',
72-
))
69+
self.browser.add_cookie(
70+
dict(
71+
name=settings.SESSION_COOKIE_NAME,
72+
value=session.session_key, # <2>
73+
path="/",
74+
)
75+
)
7376
----
7477
====
7578

@@ -260,7 +263,7 @@ a good place for a commit:
260263

261264
[subs="specialcharacters,quotes"]
262265
----
263-
$ *git add functional_tests*
266+
$ *git add src/functional_tests*
264267
$ *git commit -m "test_my_lists: precreate sessions, move login checks into base"*
265268
----
266269

@@ -305,8 +308,14 @@ TIP: Once you have more than a handful of fields on a model, and/or several
305308
Our Final Explicit Wait Helper: A Wait Decorator
306309
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307310

308-
((("decorators", "wait decorator", id="Dwait20")))((("explicit and implicit waits", id="exp20")))((("implicit and explicit waits", id="imp20")))((("helper methods", id="help20")))((("wait_for_row_in_list_table helper method")))((("self.wait_for helper method")))((("wait_to_be_logged_in/out")))We've
309-
used decorators a few times in our code so far, but it's time to learn
311+
((("decorators", "wait decorator", id="Dwait20")))
312+
((("explicit and implicit waits", id="exp20")))
313+
((("implicit and explicit waits", id="imp20")))
314+
((("helper methods", id="help20")))
315+
((("wait_for_row_in_list_table helper method")))
316+
((("self.wait_for helper method")))
317+
((("wait_to_be_logged_in/out")))
318+
We've used decorators a few times in our code so far, but it's time to learn
310319
how they actually work by making one of our own.
311320

312321
First, let's imagine how we might want our decorator to work. It would be
@@ -398,7 +407,8 @@ That's 'almost' right, but not quite; try running it?
398407
$ pass:quotes[*python src/manage.py test functional_tests.test_my_lists*]
399408
[...]
400409
self.wait_to_be_logged_out(email)
401-
TypeError: modified_fn() takes 0 positional arguments but 2 were given
410+
TypeError: wait.<locals>.modified_fn() takes 0 positional arguments but 2 were
411+
given
402412
----
403413

404414

@@ -414,7 +424,7 @@ that have [keep-together]#arguments#:
414424
----
415425
@wait
416426
def wait_to_be_logged_in(self, email):
417-
self.browser.find_element(By.LINK_TEXT, "Log out").click()
427+
self.browser.find_element(By.LINK_TEXT, "Log out")
418428
----
419429
====
420430

tests/book_tester.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def prep_database(self):
353353

354354
def assertLineIn(self, line, lines):
355355
if "\t" in line or "\t" in "\n".join(lines):
356-
print('tabz')
356+
print("tabz")
357357
if line not in lines:
358358
raise AssertionError(
359359
"%s not found in:\n%s" % (repr(line), "\n".join(repr(l) for l in lines))
@@ -537,7 +537,8 @@ def run_test_and_check_result(self, bdd=False):
537537
def run_js_tests(self, tests_path: Path):
538538
p = subprocess.run(
539539
["python", str(JASMINE_RUNNER), str(tests_path)],
540-
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
540+
stdout=subprocess.PIPE,
541+
stderr=subprocess.STDOUT,
541542
# env={**os.environ, "OPENSSL_CONF": "/dev/null"},
542543
check=False,
543544
)
@@ -556,7 +557,12 @@ def check_current_contents(self, listing, actual_contents):
556557
for block in split_blocks(listing_contents):
557558
stripped_block = [line.strip() for line in block.strip().split("\n")]
558559
for line in stripped_block:
559-
self.assertIn(line, stripped_actual_lines)
560+
self.assertIn(
561+
line,
562+
stripped_actual_lines,
563+
f"{line!r} not found in\n"
564+
+ "\n".join(repr(l) for l in stripped_actual_lines),
565+
)
560566
self.assertTrue(
561567
contains(stripped_actual_lines, stripped_block),
562568
"\n{}\n\nnot found in\n\n{}".format(

tests/test_chapter_20_fixtures_and_wait_decorator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_listings_and_commands_and_output(self):
1313
self.parse_listings()
1414

1515
# sanity checks
16-
self.assertEqual(self.listings[0].type, "code listing")
16+
self.assertEqual(self.listings[0].type, "code listing with git ref")
1717
self.assertEqual(self.listings[1].type, "other command")
1818
self.assertEqual(self.listings[2].type, "output")
1919

0 commit comments

Comments
 (0)