Chapter 15. More Advanced Forms
Now letâs look at some more advanced forms usage. Weâve helped our users to avoid blank list items, so now letâs help them avoid duplicate items.
This chapter goes into more intricate details of Djangoâs form validation, and you have my official permission to skip it if you already know all about customising Django forms, or if youâre reading this book for the TDD rather than for the Django.
If youâre still learning Django, thereâs good stuff in here. If you want to skip ahead, thatâs OK too. Make sure you take a quick look at the aside on developer stupidity, and the recap on testing views at the end.
Another FT for Duplicate Items
We
add a second test method to ItemValidationTest
:
functional_tests/test_list_item_validation.py (ch13l001)
def
test_cannot_add_duplicate_items
(
self
):
# Edith goes to the home page and starts a new list
self
.
browser
.
get
(
self
.
live_server_url
)
self
.
get_item_input_box
()
.
send_keys
(
'Buy wellies'
)
self
.
get_item_input_box
()
.
send_keys
(
Keys
.
ENTER
)
self
.
wait_for_row_in_list_table
(
'1: Buy wellies'
)
# She accidentally tries to enter a duplicate item
self
.
get_item_input_box
()
.
send_keys
(
'Buy wellies'
)
self
.
get_item_input_box
()
.
send_keys
(
Keys
.
ENTER
)
# She sees a helpful error message
self
.
wait_for
(
lambda
:
self
.
assertEqual
(
self
.
browser
.
find_element_by_css_selector
(
'.has-error'
)
.
text
,
"You've already got this in your list"
))
Why have two test methods rather than extending one, or having a new file and class? Itâs ...
Get Test-Driven Development with Python, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.