from django.test import TestCase from django.urls import reverse from django.contrib import auth from mixer.backend.django import mixer from notes.models import Note, Notebook User = auth.get_user_model() class NotebookModelTest(TestCase): def setUp(self): self.user = mixer.blend(User, username='tpynchon') self.notebook = Notebook(owner=self.user, name="San Miguel Notes") self.notebook.save() def test_string_representation(self): """Notebook title should be the name of the object""" self.assertEqual(str(self.notebook), "San Miguel Notes") def test_get_absolute_url(self): """Absolute URL should return /nb/slug """ self.assertEqual(str(self.notebook.get_absolute_url), reverse("notebooks:detail", kwargs={'slug': self.notebook.slug})) class NoteModelTest(TestCase): def setUp(self): self.user = mixer.blend(User, username='tpynchon') self.note = Note.objects.create( owner=self.user, title="test note", body_text="the body of the note", url="https://luxagraf.net/", tags="mine,cool site" ) self.note.save() self.note_no_title = Note.objects.create( owner=self.user, body_text="the body of the note", url="https://luxagraf.net/", tags="mine,cool site" ) self.note_no_title.save() def test_string_representation(self): self.assertEqual(str(self.note), "test note") self.assertEqual(str(self.note.body_text), "the body of the note") self.assertEqual(str(self.note.url), "https://luxagraf.net/") self.assertEqual(str(self.note.tags), "mine,cool site") # titleless note gets date self.assertEqual(str(self.note_no_title), str(self.note_no_title.body_text)[:50]) def test_get_absolute_url(self): """Absolute URL should return /n/slug/pk """ self.assertEqual(str(self.note.get_absolute_url), "/n/%s/%s" % (self.note.slug, self.note.pk))