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

+ url(r'^new$', 'lists.views.new_list', name='new_list'),

url(r'^users/(.+)/$', 'lists.views.my_lists', name='my_lists'),

)

lists/views.py (ch19l047).

def

new_list

(

request

):

form

=

NewListForm

(

data

=

request

.

POST

)

if

form

.

is_valid

():

list_

=

form

.

save

(

owner

=

request

.

user

)

[

...

]

And a quick check that all the tests still pass:

OK

Removing Redundant Code at the Forms Layer

Finally, we have to decide what (if anything) to keep from our integrated test suite.

One option is to throw themall away, and decide that the FTs will pick up any integration

problems. That’s perfectly valid.

On the other hand, we saw how integrated tests can warn you when you’ve made small

mistakes in integrated your layers.We could keep just a couple of tests around as “sanity-

checks”, to give us a quicker feedback cycle.

How about these three:

lists/tests/test_views.py (ch19l048).

class

NewListViewIntegratedTest

(

TestCase

):

def

test_saving_a_POST_request

(

self

):

self

.

client

.

post

(

'/lists/new'

,

data

=

{

'text'

:

'A new list item'

}

)

self

.

assertEqual

(

Item

.

objects

.

count

(),

1

)

new_item

=

Item

.

objects

.

first

()

self

.

assertEqual

(

new_item

.

text

,

'A new list item'

)

def

test_for_invalid_input_doesnt_save_but_shows_errors

(

self

):

response

=

self

.

client

.

post

(

'/lists/new'

,

data

=

{

'text'

:

''

})

self

.

assertEqual

(

List

.

objects

.

count

(),

0

)

self

.

assertContains

(

response

,

escape

(

EMPTY_LIST_ERROR

))

def

test_saves_list_owner_if_user_logged_in

(

self

):

request

=

HttpRequest

()

request

.

user

=

User

.

objects

.

create

(

email

=

'a@b.com

'

)

request

.

POST

[

'text'

]

=

'new list item'

new_list

(

request

)

list_

=

List

.

objects

.

first

()

self

.

assertEqual

(

list_

.

owner

,

request

.

user

)

Tidy Up: What to Keep from Our Integrated Test Suite

|

361