diff options
Diffstat (limited to 'pydis_site/apps/content')
-rw-r--r-- | pydis_site/apps/content/tests/helpers.py | 13 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_utils.py | 8 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_views.py | 22 |
3 files changed, 37 insertions, 6 deletions
diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index be91b95a..59cd3bd6 100644 --- a/pydis_site/apps/content/tests/helpers.py +++ b/pydis_site/apps/content/tests/helpers.py @@ -8,6 +8,7 @@ description: TestDescription relevant_links: Python Discord: https://pythondiscord.com Discord: https://discord.com +toc: 0 --- # This is a header. """ @@ -32,7 +33,8 @@ PARSED_METADATA = { "relevant_links": { "Python Discord": "https://pythondiscord.com", "Discord": "https://discord.com" - } + }, + "toc": 0 } # The YAML data parsed from the above _info.yml file @@ -50,7 +52,10 @@ class MockPagesTestCase(TestCase): ├── not_a_page.md ├── tmp | ├── _info.yml - | └── category_without_info + | ├── tmp.md + | └── category + | ├── _info.yml + | └── subcategory_without_info └── category ├── _info.yml ├── with_metadata.md @@ -81,4 +86,6 @@ class MockPagesTestCase(TestCase): # for testing purposes. # See: https://jmcgeheeiv.github.io/pyfakefs/release/usage.html#os-temporary-directories self.fs.create_file("tmp/_info.yml", contents=CATEGORY_INFO) - self.fs.create_dir("tmp/category_without_info") + self.fs.create_file("tmp/tmp.md", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("tmp/category/_info.yml", contents=MARKDOWN_WITH_METADATA) + self.fs.create_dir("tmp/category/subcategory_without_info") diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py index 658a6d4e..6612e44c 100644 --- a/pydis_site/apps/content/tests/test_utils.py +++ b/pydis_site/apps/content/tests/test_utils.py @@ -28,7 +28,7 @@ class GetCategoryTests(MockPagesTestCase): def test_get_category_without_info_yml(self): # Categories should provide an _info.yml file with self.assertRaises(FileNotFoundError): - utils.get_category(Path("tmp/category_without_info")) + utils.get_category(Path("tmp/category/subcategory_without_info")) class GetCategoriesTests(MockPagesTestCase): @@ -73,10 +73,12 @@ class GetPageTests(MockPagesTestCase): """Tests for the get_page function.""" def test_get_page(self): + # TOC is a special case because the markdown converter outputs the TOC as HTML + updated_metadata = {**PARSED_METADATA, "toc": '<div class="toc">\n<ul></ul>\n</div>\n'} cases = [ - ("Root page with metadata", "root.md", PARSED_HTML, PARSED_METADATA), + ("Root page with metadata", "root.md", PARSED_HTML, updated_metadata), ("Root page without metadata", "root_without_metadata.md", PARSED_HTML, {}), - ("Page with metadata", "category/with_metadata.md", PARSED_HTML, PARSED_METADATA), + ("Page with metadata", "category/with_metadata.md", PARSED_HTML, updated_metadata), ("Page without metadata", "category/subcategory/without_metadata.md", PARSED_HTML, {}), ] diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index 46a0f7da..81c4012b 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -90,6 +90,11 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): with self.assertRaises(Http404): self.ViewClass.get_template_names() + def test_get_template_names_returns_page_template_for_category_with_page(self): + """Make sure the proper page is returned for category locations with pages.""" + patch_dispatch_attributes(self.ViewClass, "tmp") + self.assertEqual(self.ViewClass.get_template_names(), ["content/page.html"]) + def test_get_context_data_with_valid_page(self): """The method should return required fields in the template context.""" request = self.factory.get("/root") @@ -145,6 +150,23 @@ class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): with self.subTest(msg=msg): self.assertEqual(context[key], expected_value) + def test_get_context_data_for_category_with_page(self): + """Make sure the proper page is returned for category locations with pages.""" + request = self.factory.get("/category") + self.ViewClass.setup(request) + self.ViewClass.dispatch(request, location="tmp") + + context = self.ViewClass.get_context_data() + expected_page_context = { + "page": PARSED_HTML, + "page_title": PARSED_METADATA["title"], + "page_description": PARSED_METADATA["description"], + "relevant_links": PARSED_METADATA["relevant_links"] + } + for key, expected_value in expected_page_context.items(): + with self.subTest(): + self.assertEqual(context[key], expected_value) + def test_get_context_data_breadcrumbs(self): """The method should return correct breadcrumbs.""" request = self.factory.get("/category/subcategory/with_metadata") |