From eb2215cd47ed32508d56f572b111202cc83edcf0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 2 Aug 2018 13:50:12 +0100 Subject: [PATCH] Add tests for blog page --- project/blog/tests.py | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 project/blog/tests.py diff --git a/project/blog/tests.py b/project/blog/tests.py new file mode 100644 index 0000000..95e7e96 --- /dev/null +++ b/project/blog/tests.py @@ -0,0 +1,67 @@ +from project.common.tests import BaseTestCase +from project.home.models import HomePage +from .models import BlogIndexPage, BlogPage + + +class BlogIndexPageTestCase(BaseTestCase): + def setUp(self): + super().setUp() + self.create_model(BlogIndexPage, { + 'title': 'The blog', + 'body': '

Some test content

', + }) + self.page = BlogIndexPage.objects.get(title='The blog') + + def test_create_child(self): + create_response = self.create_model(BlogPage, { + 'title': 'Test blog post', + 'body': '

Blogging stuff

', + }, parent=self.page) + self.assertEqual(create_response.status_code, 302) + self.assertTrue(BlogPage.objects.filter(title='Test blog post').exists()) + + def test_create_invalid_child(self): + create_response = self.create_model(HomePage, { + 'title': 'A home page', + 'body': '

Some home page

', + }, parent=self.page) + self.assertEqual(create_response.status_code, 403) + self.assertFalse(HomePage.objects.filter(title='A home page').exists()) + + def test_context(self): + for i in range(3): + create_response = self.create_model(BlogPage, { + 'title': 'Test blog post {}'.format(i), + 'body': '

Blogging stuff for post {}

'.format(i), + }, parent=self.page) + self.assertEqual(create_response.status_code, 302) + create_response = self.create_model(BlogPage, { + 'title': 'Test blog post', + 'body': '

Blogging stuff for post

', + }) + self.assertEqual(create_response.status_code, 302) + self.assertEqual(BlogPage.objects.count(), 4) + response = self.client.get(self.page.url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context_data['blogs'].count(), 3) + self.assertFalse(response.context_data['blogs'].filter(title='Test blog post').exists()) + + def test_page(self): + response = self.client.get(self.page.url) + self.assertEqual(response.status_code, 200) + self.assertContains(response, str(self.page.body)) + + +class BlogPageTestCase(BaseTestCase): + def setUp(self): + super().setUp() + self.create_model(BlogPage, { + 'title': 'Some blog page', + 'body': '

Some test content

', + }) + self.page = BlogPage.objects.get(title='Some blog page') + + def test_page(self): + response = self.client.get(self.page.url) + self.assertEqual(response.status_code, 200) + self.assertContains(response, str(self.page.body))