From 128def50309353fc568f6c5354e65633076e8689 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 22 Sep 2020 20:51:00 +0300 Subject: Create tests for resources app --- pydis_site/apps/resources/tests/__init__.py | 0 pydis_site/apps/resources/tests/test_views.py | 10 ++++++++++ 2 files changed, 10 insertions(+) create mode 100644 pydis_site/apps/resources/tests/__init__.py create mode 100644 pydis_site/apps/resources/tests/test_views.py (limited to 'pydis_site/apps/resources/tests') diff --git a/pydis_site/apps/resources/tests/__init__.py b/pydis_site/apps/resources/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pydis_site/apps/resources/tests/test_views.py b/pydis_site/apps/resources/tests/test_views.py new file mode 100644 index 00000000..b131b2a6 --- /dev/null +++ b/pydis_site/apps/resources/tests/test_views.py @@ -0,0 +1,10 @@ +from django.test import TestCase +from django_hosts import reverse + + +class TestResourcesView(TestCase): + def test_resources_index_200(self): + """Check does index of resources app return 200 HTTP response.""" + url = reverse("resources:resources") + response = self.client.get(url) + self.assertEqual(response.status_code, 200) -- cgit v1.2.3 From 194e3720ad8cfac1c38d8ac1279688aa6dd65c6a Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 28 Oct 2020 20:07:25 +0200 Subject: Change resources home name from resources -> index --- pydis_site/apps/resources/tests/test_views.py | 2 +- pydis_site/apps/resources/urls.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/resources/tests') diff --git a/pydis_site/apps/resources/tests/test_views.py b/pydis_site/apps/resources/tests/test_views.py index b131b2a6..497e9bfe 100644 --- a/pydis_site/apps/resources/tests/test_views.py +++ b/pydis_site/apps/resources/tests/test_views.py @@ -5,6 +5,6 @@ from django_hosts import reverse class TestResourcesView(TestCase): def test_resources_index_200(self): """Check does index of resources app return 200 HTTP response.""" - url = reverse("resources:resources") + url = reverse("resources:index") response = self.client.get(url) self.assertEqual(response.status_code, 200) diff --git a/pydis_site/apps/resources/urls.py b/pydis_site/apps/resources/urls.py index 208d0c93..c91e306e 100644 --- a/pydis_site/apps/resources/urls.py +++ b/pydis_site/apps/resources/urls.py @@ -4,5 +4,5 @@ from pydis_site.apps.resources import views app_name = "resources" urlpatterns = [ - path("", views.ResourcesView.as_view(), name="resources"), + path("", views.ResourcesView.as_view(), name="index"), ] -- cgit v1.2.3 From 7631ccfef001a6213c395bf8a79645220c658e08 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 14 Nov 2020 09:38:36 +0200 Subject: Create as_icon templatetag and tests for it --- pydis_site/apps/resources/templatetags/__init__.py | 3 +++ pydis_site/apps/resources/templatetags/as_icon.py | 14 +++++++++++ pydis_site/apps/resources/tests/test_as_icon.py | 28 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 pydis_site/apps/resources/templatetags/__init__.py create mode 100644 pydis_site/apps/resources/templatetags/as_icon.py create mode 100644 pydis_site/apps/resources/tests/test_as_icon.py (limited to 'pydis_site/apps/resources/tests') diff --git a/pydis_site/apps/resources/templatetags/__init__.py b/pydis_site/apps/resources/templatetags/__init__.py new file mode 100644 index 00000000..2b266b94 --- /dev/null +++ b/pydis_site/apps/resources/templatetags/__init__.py @@ -0,0 +1,3 @@ +from .as_icon import as_icon + +__all__ = ["as_icon"] diff --git a/pydis_site/apps/resources/templatetags/as_icon.py b/pydis_site/apps/resources/templatetags/as_icon.py new file mode 100644 index 00000000..b211407c --- /dev/null +++ b/pydis_site/apps/resources/templatetags/as_icon.py @@ -0,0 +1,14 @@ +from django import template + +register = template.Library() + + +@register.filter +def as_icon(icon: str) -> str: + """Convert icon string in format 'type/icon' to fa-icon HTML classes.""" + icon_type, icon_name = icon.split("/") + if icon_type.lower() == "branding": + icon_type = "fab" + else: + icon_type = "fas" + return f'{icon_type} fa-{icon_name}' diff --git a/pydis_site/apps/resources/tests/test_as_icon.py b/pydis_site/apps/resources/tests/test_as_icon.py new file mode 100644 index 00000000..5b33910d --- /dev/null +++ b/pydis_site/apps/resources/tests/test_as_icon.py @@ -0,0 +1,28 @@ +from django.test import TestCase + +from pydis_site.apps.resources.templatetags import as_icon + + +class TestAsIcon(TestCase): + """Tests for `as_icon` templatetag.""" + + def test_as_icon(self): + """Should return proper icon type class and icon class based on input.""" + test_cases = [ + { + "input": "regular/icon", + "output": "fas fa-icon", + }, + { + "input": "branding/brand", + "output": "fab fa-brand", + }, + { + "input": "fake/my-icon", + "output": "fas fa-my-icon", + } + ] + + for case in test_cases: + with self.subTest(input=case["input"], output=case["output"]): + self.assertEqual(case["output"], as_icon(case["input"])) -- cgit v1.2.3 From a44b900d0b2ce0c200e996bfaa977e10f403517b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 14 Nov 2020 11:45:15 +0200 Subject: Create testing resources --- .../apps/resources/tests/testing_resources/testing/_category_info.yaml | 1 + .../resources/tests/testing_resources/testing/foobar/_category_info.yaml | 1 + .../resources/tests/testing_resources/testing/foobar/resource_test.yaml | 1 + .../apps/resources/tests/testing_resources/testing/my_resource.yaml | 1 + 4 files changed, 4 insertions(+) create mode 100644 pydis_site/apps/resources/tests/testing_resources/testing/_category_info.yaml create mode 100644 pydis_site/apps/resources/tests/testing_resources/testing/foobar/_category_info.yaml create mode 100644 pydis_site/apps/resources/tests/testing_resources/testing/foobar/resource_test.yaml create mode 100644 pydis_site/apps/resources/tests/testing_resources/testing/my_resource.yaml (limited to 'pydis_site/apps/resources/tests') diff --git a/pydis_site/apps/resources/tests/testing_resources/testing/_category_info.yaml b/pydis_site/apps/resources/tests/testing_resources/testing/_category_info.yaml new file mode 100644 index 00000000..bae17ea3 --- /dev/null +++ b/pydis_site/apps/resources/tests/testing_resources/testing/_category_info.yaml @@ -0,0 +1 @@ +name: Testing diff --git a/pydis_site/apps/resources/tests/testing_resources/testing/foobar/_category_info.yaml b/pydis_site/apps/resources/tests/testing_resources/testing/foobar/_category_info.yaml new file mode 100644 index 00000000..eaac32d9 --- /dev/null +++ b/pydis_site/apps/resources/tests/testing_resources/testing/foobar/_category_info.yaml @@ -0,0 +1 @@ +name: Foobar diff --git a/pydis_site/apps/resources/tests/testing_resources/testing/foobar/resource_test.yaml b/pydis_site/apps/resources/tests/testing_resources/testing/foobar/resource_test.yaml new file mode 100644 index 00000000..22835090 --- /dev/null +++ b/pydis_site/apps/resources/tests/testing_resources/testing/foobar/resource_test.yaml @@ -0,0 +1 @@ +name: Resource Test diff --git a/pydis_site/apps/resources/tests/testing_resources/testing/my_resource.yaml b/pydis_site/apps/resources/tests/testing_resources/testing/my_resource.yaml new file mode 100644 index 00000000..61df6173 --- /dev/null +++ b/pydis_site/apps/resources/tests/testing_resources/testing/my_resource.yaml @@ -0,0 +1 @@ +name: My Resource -- cgit v1.2.3 From 74f19a8f879155a3fa2d9455b91bb92c572d3095 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 14 Nov 2020 11:47:07 +0200 Subject: Create view and tests for this view for resources lists --- pydis_site/apps/resources/tests/test_views.py | 24 ++++++++++ pydis_site/apps/resources/views/resources_list.py | 58 +++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 pydis_site/apps/resources/views/resources_list.py (limited to 'pydis_site/apps/resources/tests') diff --git a/pydis_site/apps/resources/tests/test_views.py b/pydis_site/apps/resources/tests/test_views.py index 497e9bfe..53685eef 100644 --- a/pydis_site/apps/resources/tests/test_views.py +++ b/pydis_site/apps/resources/tests/test_views.py @@ -1,6 +1,14 @@ +from pathlib import Path +from unittest.mock import patch + +from django.conf import settings from django.test import TestCase from django_hosts import reverse +TESTING_RESOURCES_PATH = Path( + settings.BASE_DIR, "pydis_site", "apps", "resources", "tests", "testing_resources" +) + class TestResourcesView(TestCase): def test_resources_index_200(self): @@ -8,3 +16,19 @@ class TestResourcesView(TestCase): url = reverse("resources:index") response = self.client.get(url) self.assertEqual(response.status_code, 200) + + +class TestResourcesListView(TestCase): + @patch("pydis_site.apps.resources.views.resources_list.RESOURCES_PATH", TESTING_RESOURCES_PATH) + def test_valid_resource_list_200(self): + """Check does site return code 200 when visiting valid resource list.""" + url = reverse("resources:resources", ("testing",)) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + @patch("pydis_site.apps.resources.views.resources_list.RESOURCES_PATH", TESTING_RESOURCES_PATH) + def test_invalid_resource_list_404(self): + """Check does site return code 404 when trying to visit invalid resource list.""" + url = reverse("resources:resources", ("invalid",)) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py new file mode 100644 index 00000000..c15f84f2 --- /dev/null +++ b/pydis_site/apps/resources/views/resources_list.py @@ -0,0 +1,58 @@ +from pathlib import Path +from typing import Any, Dict + +import yaml +from django.conf import settings +from django.http import Http404 +from django.views.generic import TemplateView + +RESOURCES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "resources", "resources") + + +class ResourcesList(TemplateView): + """Shows specific resources list.""" + + template_name = "resources/resources_list.html" + + def get_context_data(self, **kwargs) -> Dict[str, Any]: + """Add resources and subcategories data into context.""" + context = super().get_context_data(**kwargs) + + resource_path = RESOURCES_PATH / self.kwargs["type"] + if ( + not resource_path.exists() + or not resource_path.is_dir() + or not resource_path.joinpath("_category_info.yaml").exists() + ): + raise Http404 + + resources = [] + subcategories = [] + for item in resource_path.iterdir(): + if item.is_file() and item.suffix == ".yaml" and item.name != "_category_info.yaml": + resources.append(yaml.safe_load(item.read_text())) + elif item.is_dir() and item.joinpath("_category_info.yaml").exists(): + subcategories.append({ + "category_info": {**yaml.safe_load( + item.joinpath("_category_info.yaml").read_text() + ), "raw_name": item.name}, + "resources": sorted([ + yaml.safe_load(subitem.read_text()) + for subitem in item.iterdir() + if ( + subitem.is_file() + and subitem.suffix == ".yaml" + and subitem.name != "_category_info.yaml" + ) + ], key=lambda k: k.get('position', 100)) + }) + + context["resources"] = sorted(resources, key=lambda k: k.get('position', 100)) + context["subcategories"] = sorted( + subcategories, key=lambda k: k.get('category_info', {}).get('position', 100) + ) + context["category_info"] = {**yaml.safe_load( + resource_path.joinpath("_category_info.yaml").read_text() + ), "raw_name": resource_path.name} + + return context -- cgit v1.2.3