diff options
author | 2021-04-03 15:32:20 +0800 | |
---|---|---|
committer | 2021-04-03 15:32:20 +0800 | |
commit | f822d4fce05d228021450714a73cd84e143bbddc (patch) | |
tree | 1c1d617891f6657766c9a2b15d4f6f199bfbf9e8 /pydis_site/apps/content/tests | |
parent | Merge pull request #421 from ks129/resources-lists (diff) | |
parent | Use metadata titles in Sub-Articles dropdown. (diff) |
Merge pull request #468 from python-discord/content-app-improvements
Dewikification: Content app improvements.
Diffstat (limited to 'pydis_site/apps/content/tests')
-rw-r--r-- | pydis_site/apps/content/tests/helpers.py | 17 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_utils.py | 10 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_views.py | 53 |
3 files changed, 64 insertions, 16 deletions
diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py index 4e0cca34..29140375 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. """ @@ -16,7 +17,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 """ @@ -32,11 +33,12 @@ 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 -PARSED_CATEGORY_INFO = {"name": "Category Name", "description": "Description"} +PARSED_CATEGORY_INFO = {"title": "Category Name", "description": "Description"} class MockPagesTestCase(TestCase): @@ -48,9 +50,12 @@ class MockPagesTestCase(TestCase): ├── root.md ├── root_without_metadata.md ├── not_a_page.md + ├── tmp.md ├── tmp | ├── _info.yml - | └── category_without_info + | └── 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.md", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("tmp/category/_info.yml", contents=CATEGORY_INFO) + 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 58175d6f..6612e44c 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): @@ -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 560378bc..74d38f78 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -17,7 +17,23 @@ with fake_filesystem_unittest.Patcher() as _: BASE_PATH = Path(".") -@override_settings(PAGES_PATH=BASE_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_path = view.full_location + view.page_path = view.full_location.with_suffix(".md") + + +@override_settings(CONTENT_PAGES_PATH=BASE_PATH) class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): """Tests for the PageOrCategoryView class.""" @@ -63,16 +79,21 @@ 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() + 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") @@ -120,7 +141,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() @@ -128,6 +149,24 @@ 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"], + "subarticles": [{"path": "category", "name": "Category Name"}] + } + 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") @@ -138,8 +177,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"}, ] ) |