Background Image
Table of Contents Table of Contents
Previous Page  210 / 478 Next Page
Information
Show Menu
Previous Page 210 / 478 Next Page
Page Background

lists/tests/test_views.py (ch10l020-1).

class

NewListTest

(

TestCase

):

[

...

]

def

test_validation_errors_are_sent_back_to_home_page_template

(

self

):

[

...

]

def

test_invalid_list_items_arent_saved

(

self

):

self

.

client

.

post

(

'/lists/new'

,

data

=

{

'item_text'

:

''

})

self

.

assertEqual

(

List

.

objects

.

count

(),

0

)

self

.

assertEqual

(

Item

.

objects

.

count

(),

0

)

That gives:

[...]

Traceback (most recent call last):

File "/workspace/superlists/lists/tests/test_views.py", line 57, in

test_invalid_list_items_arent_saved

self.assertEqual(List.objects.count(), 0)

AssertionError: 1 != 0

We fix it like this:

lists/views.py (ch10l020-2).

def

new_list

(

request

):

list_

=

List

.

objects

.

create

()

item

=

Item

(

text

=

request

.

POST

[

'item_text'

],

list

=

list_

)

try

:

item

.

full_clean

()

item

.

save

()

except

ValidationError

:

list_

.

delete

()

error

=

"You can't have an empty list item"

return

render

(

request

,

'home.html'

, {

"error"

:

error

})

return

redirect

(

'/lists/

%d

/'

%

(

list_

.

id

,))

Do the FTs pass?

$

python3 manage.py test functional_tests.test_list_item_validation

[...]

File "/workspace/superlists/functional_tests/test_list_item_validation.py",

line 26, in test_cannot_add_empty_list_items

[...]

selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate

element: {"method":"css selector","selector":".has-error"}' ; Stacktrace:

Not quite, but they did get a little further. Checking the

line 26

, we can see that we’ve

got past the first part of the test, and are now onto the second check—that submitting

a second empty item also shows an error.

We’ve got some working code though, so let’s have a commit:

$

git commit -am"Adjust new list view to do model validation"

182

|

Chapter 10: Input Validation and Test Organisation