From b926b702297e0da6a167c1a5fd024e3e23425f4d Mon Sep 17 00:00:00 2001 From: kosayoda Date: Wed, 24 Mar 2021 00:14:57 +0800 Subject: Fix content app tests. --- .../apps/content/tests/test_content/_info.yml | 2 + pydis_site/apps/content/tests/test_content/test.md | 6 ++- pydis_site/apps/content/tests/test_utils.py | 50 +++++++++++----------- pydis_site/apps/content/tests/test_views.py | 24 +++-------- 4 files changed, 37 insertions(+), 45 deletions(-) create mode 100644 pydis_site/apps/content/tests/test_content/_info.yml (limited to 'pydis_site') diff --git a/pydis_site/apps/content/tests/test_content/_info.yml b/pydis_site/apps/content/tests/test_content/_info.yml new file mode 100644 index 00000000..ad5fc113 --- /dev/null +++ b/pydis_site/apps/content/tests/test_content/_info.yml @@ -0,0 +1,2 @@ +name: Base Category +description: Base Description diff --git a/pydis_site/apps/content/tests/test_content/test.md b/pydis_site/apps/content/tests/test_content/test.md index 175c1fdb..ca06c1d5 100644 --- a/pydis_site/apps/content/tests/test_content/test.md +++ b/pydis_site/apps/content/tests/test_content/test.md @@ -1,8 +1,10 @@ --- title: Test short_description: Testing -relevant_links: https://pythondiscord.com/pages/resources/guides/asking-good-questions/,https://pythondiscord.com/pages/resources/guides/help-channels/,https://pythondiscord.com/pages/code-of-conduct/ -relevant_link_values: Asking Good Questions,Help Channel Guide,Code of Conduct +relevant_links: + Asking Good Questions: https://pythondiscord.com/pages/resources/guides/asking-good-questions/ + Help Channel Guide: https://pythondiscord.com/pages/resources/guides/help-channels/ + Code of Conduct: https://pythondiscord.com/pages/code-of-conduct/ --- This is test content. diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py index e1f1b92a..3831ad33 100644 --- a/pydis_site/apps/content/tests/test_utils.py +++ b/pydis_site/apps/content/tests/test_utils.py @@ -1,6 +1,5 @@ -from datetime import datetime from pathlib import Path -from unittest.mock import MagicMock, patch +from unittest.mock import patch from django.conf import settings from django.http import Http404 @@ -16,7 +15,8 @@ class TestGetCategory(TestCase): @override_settings(PAGES_PATH=BASE_PATH) def test_get_category_successfully(self): """Check does this get right data from category data file.""" - result = utils.get_category(["category"]) + path = BASE_PATH.joinpath("category") + result = utils.get_category(path) self.assertEqual(result, {"name": "My Category", "description": "My Description"}) @@ -24,13 +24,15 @@ class TestGetCategory(TestCase): def test_get_category_not_exists(self): """Check does this raise 404 error when category don't exists.""" with self.assertRaises(Http404): - utils.get_category(["invalid"]) + path = BASE_PATH.joinpath("invalid") + utils.get_category(path) @override_settings(PAGES_PATH=BASE_PATH) def test_get_category_not_directory(self): """Check does this raise 404 error when category isn't directory.""" with self.assertRaises(Http404): - utils.get_category(["test.md"]) + path = BASE_PATH.joinpath("test.md") + utils.get_category(path) class TestGetCategories(TestCase): @@ -40,25 +42,18 @@ class TestGetCategories(TestCase): """Check does this return test content categories.""" get_category_mock.return_value = {"name": "My Category", "description": "My Description"} - result = utils.get_categories() - get_category_mock.assert_called_once_with(["category"]) + path = BASE_PATH.joinpath("category") + result = utils.get_categories(path) self.assertEqual( - result, {"category": {"name": "My Category", "description": "My Description"}} - ) - - @override_settings(PAGES_PATH=BASE_PATH) - def test_get_categories_root_path(self): - """Check does this doesn't call joinpath when getting root categories.""" - result = utils.get_categories() - self.assertEqual( - result, {"category": {"name": "My Category", "description": "My Description"}} + result, {"subcategory": {"name": "My Category", "description": "My Description"}} ) @override_settings(PAGES_PATH=BASE_PATH) def test_get_categories_in_category(self): """Check does this call joinpath when getting subcategories.""" - result = utils.get_categories(["category"]) + path = BASE_PATH.joinpath("category") + result = utils.get_categories(path) self.assertEqual( result, {"subcategory": {"name": "My Category 1", "description": "My Description 1"}} ) @@ -68,7 +63,8 @@ class TestGetPages(TestCase): @override_settings(PAGES_PATH=BASE_PATH) def test_get_all_root_pages(self): """Check does this return all root level testing content.""" - result = utils.get_pages() + path = BASE_PATH + result = utils.get_pages(path) for case in ["test", "test2"]: with self.subTest(guide=case): @@ -80,7 +76,8 @@ class TestGetPages(TestCase): @override_settings(PAGES_PATH=BASE_PATH) def test_get_all_category_pages(self): """Check does this return all category testing content.""" - result = utils.get_pages(["category"]) + path = BASE_PATH.joinpath("category") + result = utils.get_pages(path) md = markdown(BASE_PATH.joinpath("category", "test3.md").read_text(), extras=["metadata"]) @@ -92,7 +89,8 @@ class TestGetPage(TestCase): @override_settings(PAGES_PATH=BASE_PATH) def test_get_root_page_success(self): """Check does this return page HTML and metadata when root page exist.""" - result = utils.get_page(["test"]) + path = BASE_PATH.joinpath("test.md") + result = utils.get_page(path) md = markdown( BASE_PATH.joinpath("test.md").read_text(), @@ -113,12 +111,14 @@ class TestGetPage(TestCase): def test_get_root_page_dont_exist(self): """Check does this raise Http404 when root page don't exist.""" with self.assertRaises(Http404): - utils.get_page(["invalid"]) + path = BASE_PATH.joinpath("invalid") + utils.get_page(path) @override_settings(PAGES_PATH=BASE_PATH) def test_get_category_page_success(self): """Check does this return page HTML and metadata when category guide exist.""" - result = utils.get_page(["category", "test3"]) + path = BASE_PATH.joinpath("category", "test3.md") + result = utils.get_page(path) md = markdown( BASE_PATH.joinpath("category", "test3.md").read_text(), @@ -139,10 +139,12 @@ class TestGetPage(TestCase): def test_get_category_page_dont_exist(self): """Check does this raise Http404 when category page don't exist.""" with self.assertRaises(Http404): - utils.get_page(["category", "invalid"]) + path = BASE_PATH.joinpath("category", "invalid") + utils.get_page(path) @patch("pydis_site.settings.PAGES_PATH", new=BASE_PATH) def test_get_category_page_category_dont_exist(self): """Check does this raise Http404 when category don't exist.""" with self.assertRaises(Http404): - utils.get_page(["invalid", "some-guide"]) + path = BASE_PATH.joinpath("invalid", "some-guide") + utils.get_page(path) diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index 5ce18afb..c61671a2 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -11,33 +11,17 @@ from pydis_site.apps.content.views import PageOrCategoryView BASE_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "content", "tests", "test_content") -class TestPagesIndexView(TestCase): - @patch("pydis_site.apps.content.views.pages.get_pages") - @patch("pydis_site.apps.content.views.pages.get_categories") - def test_pages_index_return_200(self, get_categories_mock, get_page_mock): - """Check that content index return HTTP code 200.""" - get_categories_mock.return_value = {} - get_page_mock.return_value = {} - - url = reverse('content:pages') - response = self.client.get(url) - self.assertEqual(response.status_code, 200) - get_page_mock.assert_called_once() - get_categories_mock.assert_called_once() - - class TestPageOrCategoryView(TestCase): @override_settings(PAGES_PATH=BASE_PATH) @patch("pydis_site.apps.content.views.page_category.utils.get_page") @patch("pydis_site.apps.content.views.page_category.utils.get_category") - @patch("pydis_site.apps.content.views.page_category.utils.get_github_information") def test_page_return_code_200(self, get_category_mock, get_page_mock): get_page_mock.return_value = {"guide": "test", "metadata": {}} url = reverse("content:page_category", args=["test2"]) response = self.client.get(url) self.assertEqual(response.status_code, 200) - get_category_mock.assert_not_called() + get_category_mock.assert_called_once() get_page_mock.assert_called_once() @patch("pydis_site.apps.content.views.page_category.utils.get_page") @@ -72,7 +56,7 @@ class TestPageOrCategoryView(TestCase): self.assertEqual(response.status_code, 200) get_pages_mock.assert_called_once() - get_category_mock.assert_called_once() + self.assertEqual(get_category_mock.call_count, 2) get_categories_mock.assert_called_once() @patch("pydis_site.apps.content.views.page_category.utils.get_category") @@ -145,7 +129,9 @@ class TestPageOrCategoryView(TestCase): request = factory.get(f"/pages/{case['location']}") instance = PageOrCategoryView() instance.request = request - instance.kwargs = {"location": case["location"]} + location = Path(case["location"]) + instance.location = location + instance.full_location = BASE_PATH / location if "raises" in case: with self.assertRaises(case["raises"]): -- cgit v1.2.3