From 39fe2718b3d5306f9e2c809f8bc178b90670b3e3 Mon Sep 17 00:00:00 2001 From: Haydn Greatnews Date: Mon, 22 Jul 2024 13:39:22 +1200 Subject: [PATCH] Add tests for Project.display_tags Also, fix the various failures up the tree which prevented these new tests from passing --- cdhweb/pages/tests/conftest.py | 2 ++ cdhweb/projects/models.py | 10 +++++++ cdhweb/projects/tests/conftest.py | 5 ++-- cdhweb/projects/tests/test_models.py | 42 +++++++++++++++++++++++++++- docker/application/Dockerfile | 3 +- requirements/test.txt | 3 +- 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/cdhweb/pages/tests/conftest.py b/cdhweb/pages/tests/conftest.py index 010a20e87..318239a6a 100644 --- a/cdhweb/pages/tests/conftest.py +++ b/cdhweb/pages/tests/conftest.py @@ -8,6 +8,7 @@ from django.views.generic.detail import DetailView from django.views.generic.list import ListView from wagtail.models import Page, Site +from wagtail_factories import ImageFactory from cdhweb.pages.models import ContentPage, ExternalAttachment, HomePage, LandingPage from cdhweb.pages.views import LastModifiedListMixin, LastModifiedMixin @@ -34,6 +35,7 @@ def make_homepage(site): body=json.dumps( [{"type": "paragraph", "value": "

content of the home page

"}] ), + hero_image=ImageFactory(), ) root.add_child(instance=home) root.save() diff --git a/cdhweb/projects/models.py b/cdhweb/projects/models.py index cb22e9b1a..f2259dc4c 100644 --- a/cdhweb/projects/models.py +++ b/cdhweb/projects/models.py @@ -282,6 +282,16 @@ def get_sitemap_urls(self, request): return urls def display_tags(self): + """ + Get role, method and field values as tag-y objects + + The method/field/role fields are used for filtering + the projects in the search, but CDH have also + requested that they be shown the same way tags are + for many other types of object -- This function + delivers them in a (template-equivalent) manner so + they can be displayed + """ return sorted( str(t) for t in itertools.chain( diff --git a/cdhweb/projects/tests/conftest.py b/cdhweb/projects/tests/conftest.py index 6e409a80c..2009d4d44 100644 --- a/cdhweb/projects/tests/conftest.py +++ b/cdhweb/projects/tests/conftest.py @@ -4,6 +4,7 @@ import pytest from django.utils import timezone +from wagtail_factories import ImageFactory from cdhweb.people.models import Person from cdhweb.projects.models import ( @@ -34,9 +35,7 @@ def add_project_member(project, role, start_date=None, end_date=None, **person_o def make_projects_landing_page(homepage): """create a test projects landing page underneath the homepage""" - plp = ProjectsLandingPageArchived( - title="projects", slug="projects", tagline="let's do some stuff" - ) + plp = ProjectsLandingPageArchived(title="projects", slug="projects") homepage.add_child(instance=plp) homepage.save() return plp diff --git a/cdhweb/projects/tests/test_models.py b/cdhweb/projects/tests/test_models.py index 655c7747e..f8e6de692 100644 --- a/cdhweb/projects/tests/test_models.py +++ b/cdhweb/projects/tests/test_models.py @@ -1,7 +1,15 @@ from datetime import datetime, timedelta from cdhweb.pages.models import RelatedLinkType -from cdhweb.projects.models import GrantType, Project, ProjectRelatedLink, Role +from cdhweb.projects.models import ( + GrantType, + Project, + ProjectField, + ProjectMethod, + ProjectRelatedLink, + ProjectRole, + Role, +) class TestGrantType: @@ -140,3 +148,35 @@ def test_display_url(self, derrida): # https res.url = "https://%s" % base_url assert res.display_url == base_url + + +class TestProjectDisplayTags: + def test_no_values(self, derrida): + assert derrida.display_tags() == [] + + def test_just_role(self, derrida): + role = ProjectRole(role="test role") + role.save() + derrida.role.add(role) + assert derrida.display_tags() == ["test role"] + + def test_all_relations(self, derrida): + role1 = ProjectRole(role="test role 1") + role2 = ProjectRole(role="test role 2") + method = ProjectMethod(method="test method") + field = ProjectField(field="test field") + role1.save() + role2.save() + method.save() + field.save() + + derrida.role.add(role1) + derrida.role.add(role2) + derrida.method.add(method) + derrida.field.add(field) + assert derrida.display_tags() == [ + "test field", + "test method", + "test role 1", + "test role 2", + ] diff --git a/docker/application/Dockerfile b/docker/application/Dockerfile index f4aacf06c..e7989928e 100644 --- a/docker/application/Dockerfile +++ b/docker/application/Dockerfile @@ -53,8 +53,7 @@ RUN --mount=type=cache,target=/var/lib/apt/lists \ netcat RUN --mount=type=cache,target=/root/.cache/pip \ - pip3 install -r /app/requirements/dev.txt \ - npm install + pip3 install -r /app/requirements/dev.txt # No COPY here as the local directory is volume-mounted in docker-compose diff --git a/requirements/test.txt b/requirements/test.txt index 07d28f275..c2c515c8d 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -9,4 +9,5 @@ wheel # fix issue with taggit fields: https://github.com/makecodes/django-dbml/pull/4 django-dbml #git+https://github.com/thatbudakguy/django-dbml@patch-1#egg=django-dbml -percy-selenium \ No newline at end of file +percy-selenium +wagtail-factories~=4.2.1