From bc90ff58fd97b5b90a5adb6820f340fdbb9e2b1a Mon Sep 17 00:00:00 2001 From: kosayoda Date: Mon, 29 Mar 2021 18:43:20 +0800 Subject: Fix failing tests. --- pydis_site/apps/content/tests/helpers.py | 4 ++-- pydis_site/apps/content/tests/test_utils.py | 2 +- pydis_site/apps/content/tests/test_views.py | 29 +++++++++++++++++++++++------ 3 files changed, 26 insertions(+), 9 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index 4e0cca34..be91b95a 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -16,7 +16,7 @@ MARKDOWN_WITHOUT_METADATA = """#This is a header.""" # Valid YAML in a _info.yml file CATEGORY_INFO = """ -name: Category Name +title: Category Name description: Description """ @@ -36,7 +36,7 @@ PARSED_METADATA = { } # The YAML data parsed from the above _info.yml file -PARSED_CATEGORY_INFO = {"name": "Category Name", "description": "Description"} +PARSED_CATEGORY_INFO = {"title": "Category Name", "description": "Description"} class MockPagesTestCase(TestCase): diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py index 58175d6f..658a6d4e 100644 --- a/pydis_site/apps/content/tests/test_utils.py +++ b/pydis_site/apps/content/tests/test_utils.py @@ -14,7 +14,7 @@ class GetCategoryTests(MockPagesTestCase): def test_get_valid_category(self): result = utils.get_category(Path("category")) - self.assertEqual(result, {"name": "Category Name", "description": "Description"}) + self.assertEqual(result, {"title": "Category Name", "description": "Description"}) def test_get_nonexistent_category(self): with self.assertRaises(Http404): diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index 560378bc..46a0f7da 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -17,6 +17,23 @@ with fake_filesystem_unittest.Patcher() as _: BASE_PATH = Path(".") +def patch_dispatch_attributes(view: PageOrCategoryView, location: str) -> None: + """ + Set the attributes set in the `dispatch` method manually. + + This is necessary because it is never automatically called during tests. + """ + view.location = Path(location) + + # URL location on the filesystem + view.full_location = view.location + + # Possible places to find page content information + view.category_page_path = view.full_location.joinpath(view.location.stem).with_suffix(".md") + view.category_path = view.full_location + view.page_path = view.full_location.with_suffix(".md") + + @override_settings(PAGES_PATH=BASE_PATH) class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): """Tests for the PageOrCategoryView class.""" @@ -63,13 +80,13 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): for path, expected_template in cases: with self.subTest(path=path, expected_template=expected_template): - self.ViewClass.full_location = Path(path) + patch_dispatch_attributes(self.ViewClass, path) self.assertEqual(self.ViewClass.get_template_names(), [expected_template]) def test_get_template_names_with_nonexistent_paths_returns_404(self): for path in ("invalid", "another_invalid", "nonexistent"): with self.subTest(path=path): - self.ViewClass.full_location = Path(path) + patch_dispatch_attributes(self.ViewClass, path) with self.assertRaises(Http404): self.ViewClass.get_template_names() @@ -120,7 +137,7 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): "page_description", PARSED_CATEGORY_INFO["description"] ), - ("Context includes page title", "page_title", PARSED_CATEGORY_INFO["name"]), + ("Context includes page title", "page_title", PARSED_CATEGORY_INFO["title"]), ] context = self.ViewClass.get_context_data() @@ -138,8 +155,8 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): self.assertEquals( context["breadcrumb_items"], [ - {"name": PARSED_CATEGORY_INFO["name"], "path": "."}, - {"name": PARSED_CATEGORY_INFO["name"], "path": "category"}, - {"name": PARSED_CATEGORY_INFO["name"], "path": "category/subcategory"}, + {"name": PARSED_CATEGORY_INFO["title"], "path": "."}, + {"name": PARSED_CATEGORY_INFO["title"], "path": "category"}, + {"name": PARSED_CATEGORY_INFO["title"], "path": "category/subcategory"}, ] ) -- cgit v1.2.3