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

lists/tests/test_models.py (ch12l011).

class

ItemModelTest

(

TestCase

):

def

test_default_text

(

self

):

[

...

]

class

ListModelTest

(

TestCase

):

def

test_get_absolute_url

(

self

):

[

...

]

That’s neater and tidier:

$

python3 manage.py test lists

[...]

Ran 29 tests in 0.092s

OK

Some Integrity Errors Do Show Up on Save

A final aside before we move on. Do you remember I mentioned in

Chapter 10

that

some data integrity errors

are

picked up on save? It all depends on whether the integrity

constraint is actually being enforced by the database.

Try running

makemigrations

and you’ll see that Django wants to add the

unique_to

gether

constraint to the database itself, rather than just having it as an application-layer

constraint:

$

python3 manage.py makemigrations

Migrations for 'lists':

0005_auto_20140414_2038.py:

- Alter unique_together for item (1 constraints)

Now if we change our duplicates test to do a

.save

instead of a

.full_clean

lists/tests/test_models.py.

def

test_duplicate_items_are_invalid

(

self

):

list_

=

List

.

objects

.

create

()

Item

.

objects

.

create

(

list

=

list_

,

text

=

'bla'

)

with

self

.

assertRaises

(

ValidationError

):

item

=

Item

(

list

=

list_

,

text

=

'bla'

)

# item.full_clean()

item

.

save

()

It gives:

ERROR: test_duplicate_items_are_invalid (lists.tests.test_models.ItemModelTest)

[...]

return Database.Cursor.execute(self, query, params)

sqlite3.IntegrityError: UNIQUE constraint failed: lists_item.list_id,

lists_item.text

Another FT for Duplicate Items

|

217