@@ -135,8 +135,8 @@ If we want to get it deliberately wrong, we can do this:
135
135
[source,python]
136
136
----
137
137
class Item(models.Model):
138
- text = models.TextField(default='' , unique=True)
139
- list = models.ForeignKey(List, default=None)
138
+ text = models.TextField(default="" , unique=True)
139
+ list = models.ForeignKey(List, default=None, on_delete=models.CASCADE )
140
140
----
141
141
====
142
142
@@ -412,6 +412,8 @@ works; we get a fully passing test suite:
412
412
OK
413
413
----
414
414
415
+ // TODO: in theory we should do a migration now.
416
+
415
417
[[rewrite-model-test]]
416
418
Rewriting the Old Model Test
417
419
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -429,18 +431,17 @@ Delete `test_saving_and_retrieving_items` and replace with this:
429
431
[source,python]
430
432
----
431
433
class ListAndItemModelsTest(TestCase):
432
-
433
434
def test_default_text(self):
434
435
item = Item()
435
- self.assertEqual(item.text, '' )
436
+ self.assertEqual(item.text, "" )
436
437
437
-
438
438
def test_item_is_related_to_list(self):
439
- list_ = List.objects.create()
439
+ mylist = List.objects.create()
440
440
item = Item()
441
- item.list = list_
441
+ item.list = mylist
442
442
item.save()
443
- self.assertIn(item, list_.item_set.all())
443
+ self.assertIn(item, mylist.item_set.all())
444
+
444
445
445
446
[...]
446
447
----
@@ -461,14 +462,11 @@ for `List` (there's only one of the latter, `test_get_absolute_url`):
461
462
[source,python]
462
463
----
463
464
class ItemModelTest(TestCase):
464
-
465
465
def test_default_text(self):
466
466
[...]
467
467
468
468
469
-
470
469
class ListModelTest(TestCase):
471
-
472
470
def test_get_absolute_url(self):
473
471
[...]
474
472
----
@@ -505,7 +503,7 @@ it as an application-layer constraint:
505
503
----
506
504
$ pass:quotes[*python manage.py makemigrations*]
507
505
Migrations for 'lists':
508
- lists/migrations/0005_auto_20140414_2038 .py
506
+ src/ lists/migrations/0005_alter_item_options_alter_item_unique_together .py
509
507
- Change Meta options on item
510
508
- Alter unique_together for item (1 constraint(s))
511
509
----
@@ -563,7 +561,6 @@ now it's time to commit our model-layer changes:
563
561
----
564
562
$ pass:[<strong>git status</strong>] # should show changes to tests + models and new migration
565
563
# let's give our new migration a better name
566
- $ pass:[<strong>mv lists/migrations/0005_auto* lists/migrations/0005_list_item_unique_together.py</strong>]
567
564
$ pass:[<strong>git add lists</strong>]
568
565
$ pass:[<strong>git diff --staged</strong>]
569
566
$ pass:[<strong>git commit -am "Implement duplicate item validation at model layer"</strong>]
@@ -659,32 +656,31 @@ We duplicate our tests for the previous form, tweaking them slightly:
659
656
[source,python]
660
657
----
661
658
from lists.forms import (
662
- DUPLICATE_ITEM_ERROR, EMPTY_ITEM_ERROR,
663
- ExistingListItemForm, ItemForm
659
+ DUPLICATE_ITEM_ERROR,
660
+ EMPTY_ITEM_ERROR,
661
+ ExistingListItemForm,
662
+ ItemForm,
664
663
)
665
664
[...]
666
665
667
666
class ExistingListItemFormTest(TestCase):
668
-
669
667
def test_form_renders_item_text_input(self):
670
668
list_ = List.objects.create()
671
669
form = ExistingListItemForm(for_list=list_)
672
670
self.assertIn('placeholder="Enter a to-do item"', form.as_p())
673
671
674
-
675
672
def test_form_validation_for_blank_items(self):
676
673
list_ = List.objects.create()
677
- form = ExistingListItemForm(for_list=list_, data={' text': '' })
674
+ form = ExistingListItemForm(for_list=list_, data={" text": "" })
678
675
self.assertFalse(form.is_valid())
679
- self.assertEqual(form.errors['text'], [EMPTY_ITEM_ERROR])
680
-
676
+ self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])
681
677
682
678
def test_form_validation_for_duplicate_items(self):
683
679
list_ = List.objects.create()
684
- Item.objects.create(list=list_, text=' no twins!' )
685
- form = ExistingListItemForm(for_list=list_, data={' text': ' no twins!' })
680
+ Item.objects.create(list=list_, text=" no twins!" )
681
+ form = ExistingListItemForm(for_list=list_, data={" text": " no twins!" })
686
682
self.assertFalse(form.is_valid())
687
- self.assertEqual(form.errors[' text' ], [DUPLICATE_ITEM_ERROR])
683
+ self.assertEqual(form.errors[" text" ], [DUPLICATE_ITEM_ERROR])
688
684
----
689
685
====
690
686
@@ -826,16 +822,16 @@ class ListViewTest(TestCase):
826
822
[...]
827
823
828
824
def test_displays_item_form(self):
829
- list_ = List.objects.create()
830
- response = self.client.get(f' /lists/{list_ .id}/' )
831
- self.assertIsInstance(response.context[' form' ], ExistingListItemForm)
825
+ mylist = List.objects.create()
826
+ response = self.client.get(f" /lists/{mylist .id}/" )
827
+ self.assertIsInstance(response.context[" form" ], ExistingListItemForm)
832
828
self.assertContains(response, 'name="text"')
833
829
834
830
[...]
835
831
836
832
def test_for_invalid_input_passes_form_to_template(self):
837
833
response = self.post_invalid_input()
838
- self.assertIsInstance(response.context[' form' ], ExistingListItemForm)
834
+ self.assertIsInstance(response.context[" form" ], ExistingListItemForm)
839
835
----
840
836
====
841
837
@@ -856,10 +852,10 @@ So we can adjust the view:
856
852
from lists.forms import ExistingListItemForm, ItemForm
857
853
[...]
858
854
def view_list(request, list_id):
859
- list_ = List.objects.get(id=list_id)
860
- form = ExistingListItemForm(for_list=list_ )
861
- if request.method == ' POST' :
862
- form = ExistingListItemForm(for_list=list_ , data=request.POST)
855
+ our_list = List.objects.get(id=list_id)
856
+ form = ExistingListItemForm(for_list=our_list )
857
+ if request.method == " POST" :
858
+ form = ExistingListItemForm(for_list=our_list , data=request.POST)
863
859
if form.is_valid():
864
860
form.save()
865
861
[...]
0 commit comments