added Meta class to Item() model to test for duplicate item/list entries
This commit is contained in:
@@ -7,6 +7,8 @@ class List(models.Model):
|
|||||||
|
|
||||||
|
|
||||||
class Item(models.Model):
|
class Item(models.Model):
|
||||||
text = models.TextField(default='')
|
text = models.TextField(default="")
|
||||||
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
|
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = ("list", "text")
|
||||||
|
|||||||
@@ -46,3 +46,17 @@ class ListAndItemModelsTest(TestCase):
|
|||||||
def test_get_absolute_url(self):
|
def test_get_absolute_url(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
self.assertEqual(mylist.get_absolute_url(), f"/apps/dashboard/{mylist.id}/")
|
self.assertEqual(mylist.get_absolute_url(), f"/apps/dashboard/{mylist.id}/")
|
||||||
|
|
||||||
|
def test_duplicate_items_are_invalid(self):
|
||||||
|
mylist = List.objects.create()
|
||||||
|
Item.objects.create(list=mylist, text="jklol")
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
item = Item(list=mylist, text="jklol")
|
||||||
|
item.full_clean()
|
||||||
|
|
||||||
|
def test_still_can_save_same_item_to_different_lists(self):
|
||||||
|
list1 = List.objects.create()
|
||||||
|
list2 = List.objects.create()
|
||||||
|
Item.objects.create(list=list1, text="nojk")
|
||||||
|
item = Item(list=list2, text="nojk")
|
||||||
|
item.full_clean() # should not raise
|
||||||
|
|||||||
Reference in New Issue
Block a user