Skip to content

Commit

Permalink
Merge pull request #257 from hjwp/fix-13-tests
Browse files Browse the repository at this point in the history
Fixes for tests in 13
  • Loading branch information
Xronophobe authored Jun 13, 2024
2 parents bfeb907 + 30a0155 commit 6502ee1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
52 changes: 35 additions & 17 deletions chapter_13_database_layer_validation.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ class ListAndItemModelsTest(TestCase):
----
====

TIP: If you're new to Python, you may never have seen the `with` statement.
It's used with what are called "context managers", which wrap a block of code,
usually with some kind of setup, cleanup, or error-handling code.
There's a good write-up in the
https://docs.python.org/release/2.5/whatsnew/pep-343.html[Python 2.5 release notes].
((("with statements")))
((("Python 3", "with statements")))

This is a new unit testing technique: when we want to check that doing
something will raise an error, we can use the `self.assertRaises` context
manager. We could have used something like this instead:
Expand All @@ -145,6 +137,15 @@ AssertionError: ValidationError not raised
----


TIP: If you're new to Python, you may never have seen the `with` statement.
It's used with what are called "context managers", which wrap a block of code,
usually with some kind of setup, cleanup, or error-handling code.
There's a good write-up in the
https://docs.python.org/release/2.5/whatsnew/pep-343.html[Python 2.5 release notes].
((("with statements")))
((("Python 3", "with statements")))


A Django Quirk: Model Save Doesn't Run Validation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -598,7 +599,7 @@ Let's put an early return in the FT to separate
what we got working from those that still need to be dealt with:

[role="sourcecode"]
.src/functional_tests/test_list_item_validation.py (ch13l???)
.src/functional_tests/test_list_item_validation.py (ch13l030-3)
====
[source,python]
----
Expand All @@ -608,8 +609,7 @@ class ItemValidationTest(FunctionalTest):
self.browser.find_element(By.ID, "id_new_item").send_keys(Keys.ENTER)
self.wait_for_row_in_list_table("1: Buy milk")
return
# TODO fix the remaining test scenarios
return # TODO re-enable the rest of this test.
# Perversely, she now decides to submit a second blank list item
self.browser.find_element(By.ID, "id_new_item").send_keys(Keys.ENTER)
Expand Down Expand Up @@ -695,13 +695,13 @@ This will immediately break our original functional test, because the
----
$ pass:quotes[*python src/manage.py test functional_tests*]
[...]
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
element: .invalid-feedback; [...]
[...]
AssertionError: '2: Use peacock feathers to make a fly' not found in ['1: Buy
peacock feathers']
----

The FTs are warning us that our attempted refactor has introduced a regression.
Let's try and finish the refactor as soon as we can, and get back to green.

NOTE: In this section we're performing a refactor at the application level.
We execute our application-level refactor by changing or adding unit tests,
and then adjusting our code.
Expand Down Expand Up @@ -843,7 +843,7 @@ OK
Let's try a full FT run: they're all passing!

----
Ran 4 tests in 20 s
Ran 4 tests in 9.951s
OK
----
Expand All @@ -857,8 +857,26 @@ $ *git commit -am "Refactor list view to handle new item POSTs"*
----


We can remove the early return
now.
We can remove the early return now.


[role="sourcecode"]
.src/functional_tests/test_list_item_validation.py (ch13l033-1)
====
[source,diff]
----
@@ -24,8 +24,6 @@ class ItemValidationTest(FunctionalTest):
self.browser.find_element(By.ID, "id_new_item").send_keys(Keys.ENTER)
self.wait_for_row_in_list_table("1: Buy milk")
- return # TODO re-enable the rest of this test.
-
# Perversely, she now decides to submit a second blank list item
----
====

And from our scratchpad:


[role="scratchpad"]
*****
Expand Down
2 changes: 1 addition & 1 deletion source/chapter_13_database_layer_validation/superlists

0 comments on commit 6502ee1

Please sign in to comment.