2. It’s showing a server error, code 500. Gotta get with the jargon!
[...]
django.db.utils.IntegrityError: UNIQUE constraint failed: lists_item.list_id,
lists_item.text
You can see that the error bubbles up from SQLite, and it’s a different error to the one
we want, an
IntegrityError
instead of a
ValidationError
.
Let’s revert our changes to the test, and see them all passing again:
$
python3 manage.py test lists
[...]
Ran 29 tests in 0.092s
OK
And now it’s time to commit our model-layer changes:
$
git status
# should show changes to tests + models and new migration
# let's give our new migration a better name
$
mv lists/migrations/0005_auto* lists/migrations/0005_list_item_unique_together.py
$
git add lists
$
git diff --staged
$
git commit -am "Implement duplicate item validation at model layer"
Experimenting with Duplicate Item Validation at the
Views Layer
Let’s try running our FT, just to see where we are:
selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate
element: {"method":"id","selector":"id_list_table"}' ; Stacktrace:
In case you didn’t see it as it flew past, the site is 500ing.
2
A quick unit test at the view
level ought to clear this up:
lists/tests/test_views.py (ch12l014).
class
ListViewTest
(
TestCase
):
[
...
]
def
test_for_invalid_input_shows_error_on_page
(
self
):
[
...
]
def
test_duplicate_item_validation_errors_end_up_on_lists_page
(
self
):
list1
=
List
.
objects
.
create
()
item1
=
Item
.
objects
.
create
(
list
=
list1
,
text
=
'textey'
)
response
=
self
.
client
.
post
(
'/lists/
%d
/'
%
(
list1
.
id
,),
data
=
{
'text'
:
'textey'
}
)
218
|
Chapter 12: More Advanced Forms