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
|
2026-02-18 19:07:02 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
from apps.dashboard.models import Item, Note
|
2026-02-08 22:33:15 -05:00
|
|
|
from apps.lyric.models import User
|
2026-01-14 13:56:21 -05:00
|
|
|
|
2026-02-18 19:07:02 -05:00
|
|
|
|
2026-01-23 21:51:56 -05:00
|
|
|
class ItemModelTest(TestCase):
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_item_is_related_to_note(self):
|
|
|
|
|
mynote = Note.objects.create()
|
2026-01-23 21:50:49 -05:00
|
|
|
item = Item()
|
2026-03-11 13:59:43 -04:00
|
|
|
item.note = mynote
|
2026-01-23 21:50:49 -05:00
|
|
|
item.save()
|
2026-03-11 13:59:43 -04:00
|
|
|
self.assertIn(item, mynote.item_set.all())
|
2026-01-19 16:35:00 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_cannot_save_null_note_items(self):
|
|
|
|
|
mynote = Note.objects.create()
|
|
|
|
|
item = Item(note=mynote, text=None)
|
2026-01-19 16:35:00 -05:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
|
item.save()
|
|
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_cannot_save_empty_note_items(self):
|
|
|
|
|
mynote = Note.objects.create()
|
|
|
|
|
item = Item(note=mynote, text="")
|
2026-01-19 16:35:00 -05:00
|
|
|
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):
|
2026-03-11 13:59:43 -04:00
|
|
|
mynote = Note.objects.create()
|
|
|
|
|
Item.objects.create(note=mynote, text="jklol")
|
2026-01-21 15:29:47 -05:00
|
|
|
with self.assertRaises(ValidationError):
|
2026-03-11 13:59:43 -04:00
|
|
|
item = Item(note=mynote, text="jklol")
|
2026-01-21 15:29:47 -05:00
|
|
|
item.full_clean()
|
|
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_still_can_save_same_item_to_different_notes(self):
|
|
|
|
|
note1 = Note.objects.create()
|
|
|
|
|
note2 = Note.objects.create()
|
|
|
|
|
Item.objects.create(note=note1, text="nojk")
|
|
|
|
|
item = Item(note=note2, text="nojk")
|
2026-01-21 15:29:47 -05:00
|
|
|
item.full_clean() # should not raise
|
2026-01-23 21:50:49 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
class NoteModelTest(TestCase):
|
2026-01-23 21:50:49 -05:00
|
|
|
def test_get_absolute_url(self):
|
2026-03-11 13:59:43 -04:00
|
|
|
mynote = Note.objects.create()
|
|
|
|
|
self.assertEqual(mynote.get_absolute_url(), f"/dashboard/note/{mynote.id}/")
|
2026-01-24 13:00:12 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_note_items_order(self):
|
|
|
|
|
note1 = Note.objects.create()
|
|
|
|
|
item1 = Item.objects.create(note=note1, text="i1")
|
|
|
|
|
item2 = Item.objects.create(note=note1, text="item 2")
|
|
|
|
|
item3 = Item.objects.create(note=note1, text="3")
|
2026-01-24 13:00:12 -05:00
|
|
|
self.assertEqual(
|
2026-03-11 13:59:43 -04:00
|
|
|
list(note1.item_set.all()),
|
2026-01-24 13:00:12 -05:00
|
|
|
[item1, item2, item3],
|
|
|
|
|
)
|
2026-02-08 22:33:15 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_notes_can_have_owners(self):
|
2026-02-08 22:33:15 -05:00
|
|
|
user = User.objects.create(email="a@b.cde")
|
2026-03-11 13:59:43 -04:00
|
|
|
mynote = Note.objects.create(owner=user)
|
|
|
|
|
self.assertIn(mynote, user.notes.all())
|
2026-02-08 22:33:15 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_note_owner_is_optional(self):
|
|
|
|
|
Note.objects.create()
|
2026-02-08 22:50:03 -05:00
|
|
|
|
2026-03-11 13:59:43 -04:00
|
|
|
def test_note_name_is_first_item_text(self):
|
|
|
|
|
note = Note.objects.create()
|
|
|
|
|
Item.objects.create(note=note, text="first item")
|
|
|
|
|
Item.objects.create(note=note, text="second item")
|
|
|
|
|
self.assertEqual(note.name, "first item")
|