diff options
Diffstat (limited to 'apps/notes/tests')
-rw-r--r-- | apps/notes/tests/test_models.py | 23 | ||||
-rw-r--r-- | apps/notes/tests/test_views.py | 48 |
2 files changed, 50 insertions, 21 deletions
diff --git a/apps/notes/tests/test_models.py b/apps/notes/tests/test_models.py index cf21aca..05f2618 100644 --- a/apps/notes/tests/test_models.py +++ b/apps/notes/tests/test_models.py @@ -1,16 +1,26 @@ 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 -from accounts.models import User +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): - user = mixer.blend(User, username='tpynchon') - notebook = Notebook(owner=user, name="San Miguel Notes") - self.assertEqual(str(notebook), "San Miguel Notes") + """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): @@ -41,4 +51,5 @@ class NoteModelTest(TestCase): self.assertEqual(str(self.note_no_title), str(self.note_no_title.body_text)[:50]) def test_get_absolute_url(self): - self.assertEqual(str(self.note.get_absolute_url), "/notes/%s/%s" % (self.note.owner.username, self.note.slug)) + """Absolute URL should return /n/slug/pk """ + self.assertEqual(str(self.note.get_absolute_url), "/n/%s/%s" % (self.note.slug, self.note.pk)) diff --git a/apps/notes/tests/test_views.py b/apps/notes/tests/test_views.py index 4f9a4ce..be48226 100644 --- a/apps/notes/tests/test_views.py +++ b/apps/notes/tests/test_views.py @@ -1,6 +1,6 @@ import json from django.test import Client -from django.test import RequestFactory, TestCase +from django.test import RequestFactory, TestCase, TransactionTestCase from django.urls import reverse from django.template.defaultfilters import slugify @@ -14,6 +14,37 @@ from notes.models import Note from notes.views import NoteListView, NoteViewSet +class NoteCreateViewTests(TransactionTestCase): + """ + Because I need pks to get URL of object I need + a clean database so I know the one and only object + has a pk of 1. There's probably a better way to do + this though. + Especially because this is a private API and subject + to change/deletion: + https://docs.djangoproject.com/en/2.1/topics/testing/advanced/#django.test.TransactionTestCase.reset_sequences + """ + reset_sequences = True + + def setUp(self): + self.client = Client() + self.user = User.objects.create(username='testuser', password='password') + + def test_note_create_view(self): + data = { + 'pk': 2, + 'title': "test note post", + 'body_text': "the body of the note", + 'url': "https://luxagraf.net/", + 'tags': [], + } + self.client.force_login(self.user) + url = reverse("notes:create") + response = self.client.post(url, data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, '/n/%s/%s' % (slugify(data['title']), Note.objects.count())) + + class NotesViewsTest(TestCase): def setUp(self): # Every test needs access to the request factory. @@ -35,25 +66,12 @@ class NotesViewsTest(TestCase): self.note.save() def test_list_view(self): - request = self.factory.get('/notes/') + request = self.factory.get('/n/') request.user = self.user response = NoteListView.as_view()(request) self.assertEqual(response.status_code, 200) # bad_user - def test_note_create_view(self): - data = { - 'title': "test note post", - 'body_text': "the body of the note", - 'url': "https://luxagraf.net/", - 'tags': [], - } - self.client.force_login(self.user) - url = reverse("notes:note-create") - response = self.client.post(url, data) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/notes/%s/%s' % (self.user.username, slugify(data['title']))) - def test_api_list(self): # Make an authenticated request to the view... request = self.factory.get('/api/notes/') |