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

0004_item_list.py:

- Add field list to item

Deleting migrations is dangerous. If you delete a migration that’s

already been applied to a database somewhere, Django will be con‐

fused about what state it’s in, and how to apply future migrations.

You should only do it when you’re sure the migration hasn’t been

used. A good rule of thumb is that you should never delete a migra‐

tion that’s been committed to your VCS.

Adjusting the Rest of the World to Our New Models

Back in our tests, now what happens?

$

python3 manage.py test lists

[...]

ERROR: test_displays_all_items (lists.tests.ListViewTest)

django.db.utils.IntegrityError: NOT NULL constraint failed: lists_item.list_id

[...]

ERROR: test_redirects_after_POST (lists.tests.NewListTest)

django.db.utils.IntegrityError: NOT NULL constraint failed: lists_item.list_id

[...]

ERROR: test_saving_a_POST_request (lists.tests.NewListTest)

django.db.utils.IntegrityError: NOT NULL constraint failed: lists_item.list_id

Ran 7 tests in 0.021s

FAILED (errors=3)

Oh dear!

There is some good news. Although it’s hard to see, our model tests are passing. But

three of our view tests are failing nastily.

The reason is because of the new relationship we’ve introduced between Items and Lists,

which requires each list to have a parent item, which our old tests weren’t prepared for.

Still, this is exactly why we have tests. Let’s get them working again. The easiest is the

ListViewTest

; we just create a parent list for our two test items:

lists/tests.py (ch06l031).

class

ListViewTest

(

TestCase

):

def

test_displays_all_items

(

self

):

list_

=

List

.

objects

.

create

()

Item

.

objects

.

create

(

text

=

'itemey 1'

,

list

=

list_

)

Item

.

objects

.

create

(

text

=

'itemey 2'

,

list

=

list_

)

That gets us down to two failing tests, both on tests that try to POST to our

new_list

view. Decoding the tracebacks using our usual technique, working back from error, to

line of test code, to the line of our own code that caused the failure, we identify:

100

|

Chapter 6: Getting to the Minimum Viable Site