2026-01-19 16:35:00 -05:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
from django.db.utils import IntegrityError
|
2026-01-14 13:56:21 -05:00
|
|
|
from django.test import TestCase
|
|
|
|
|
from ..models import Item, List
|
|
|
|
|
|
2026-01-23 21:51:56 -05:00
|
|
|
class ItemModelTest(TestCase):
|
2026-01-23 21:50:49 -05:00
|
|
|
def test_default_text(self):
|
|
|
|
|
item = Item()
|
|
|
|
|
self.assertEqual(item.text, "")
|
|
|
|
|
|
|
|
|
|
def test_item_is_related_to_list(self):
|
|
|
|
|
mylist = List.objects.create()
|
|
|
|
|
item = Item()
|
|
|
|
|
item.list = mylist
|
|
|
|
|
item.save()
|
|
|
|
|
self.assertIn(item, mylist.item_set.all())
|
2026-01-19 16:35:00 -05:00
|
|
|
|
|
|
|
|
def test_cannot_save_null_list_items(self):
|
|
|
|
|
mylist = List.objects.create()
|
|
|
|
|
item = Item(list=mylist, text=None)
|
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
|
item.save()
|
|
|
|
|
|
|
|
|
|
def test_cannot_save_empty_list_items(self):
|
|
|
|
|
mylist = List.objects.create()
|
|
|
|
|
item = Item(list=mylist, text="")
|
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
|
item.full_clean()
|
2026-01-19 19:25:04 -05:00
|
|
|
|
2026-01-21 15:29:47 -05:00
|
|
|
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
|
2026-01-23 21:50:49 -05:00
|
|
|
|
2026-01-24 13:00:12 -05:00
|
|
|
def test_string_representation(self):
|
|
|
|
|
item = Item(text="sample text")
|
|
|
|
|
self.assertEqual(str(item), "sample text")
|
|
|
|
|
|
2026-01-23 21:50:49 -05:00
|
|
|
class ListModelTest(TestCase):
|
|
|
|
|
def test_get_absolute_url(self):
|
|
|
|
|
mylist = List.objects.create()
|
|
|
|
|
self.assertEqual(mylist.get_absolute_url(), f"/apps/dashboard/{mylist.id}/")
|
2026-01-24 13:00:12 -05:00
|
|
|
|
|
|
|
|
def test_list_items_order(self):
|
|
|
|
|
list1 = List.objects.create()
|
|
|
|
|
item1 = Item.objects.create(list=list1, text="i1")
|
|
|
|
|
item2 = Item.objects.create(list=list1, text="item 2")
|
|
|
|
|
item3 = Item.objects.create(list=list1, text="3")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
list(list1.item_set.all()),
|
|
|
|
|
[item1, item2, item3],
|
|
|
|
|
)
|