Skip to content

Commit ea8d3cc

Browse files
committed
Use built-in test client instead of render_to_string in JSON-LD tests
1 parent cf350e6 commit ea8d3cc

1 file changed

Lines changed: 8 additions & 32 deletions

File tree

tbx/core/tests/test_jsonld.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import json
22

3-
from django.template.loader import get_template, render_to_string
4-
from django.test import RequestFactory
3+
from django.template.loader import get_template
54

6-
from wagtail.contrib.settings.context_processors import settings as settings_processor
75
from wagtail.models import Site
86
from wagtail.test.utils import WagtailPageTestCase
97

@@ -27,14 +25,6 @@ def setUpTestData(cls):
2725
hero_heading_2="Torchbox",
2826
)
2927

30-
def _get_template_context(self, page):
31-
"""Helper method to create proper template context with settings."""
32-
factory = RequestFactory()
33-
request = factory.get("/")
34-
settings_context = settings_processor(request)
35-
36-
return {"page": page, "request": request, **settings_context}
37-
3828
def _extract_jsonld_by_type(self, content, jsonld_type):
3929
"""Helper method to extract JSON-LD by type from rendered content."""
4030
soup = BeautifulSoup(content, "html.parser")
@@ -46,9 +36,8 @@ def _extract_jsonld_by_type(self, content, jsonld_type):
4636

4737
def _get_organization_jsonld(self):
4838
"""Helper method to get Organization JSON-LD from homepage."""
49-
context = self._get_template_context(self.homepage)
50-
content = render_to_string("patterns/pages/home/home_page.html", context)
51-
json_scripts = self._extract_jsonld_by_type(content, "Organization")
39+
response = self.client.get(self.homepage.url)
40+
json_scripts = self._extract_jsonld_by_type(response.content, "Organization")
5241
self.assertGreater(len(json_scripts), 0, "Organization JSON-LD not found")
5342
return json_scripts[0]
5443

@@ -123,14 +112,6 @@ def setUpTestData(cls):
123112
cls.blog_index = BlogIndexPageFactory(parent=cls.division, title="Blog")
124113
cls.blog_post = BlogPageFactory(parent=cls.blog_index, title="Test Blog Post")
125114

126-
def _get_template_context(self, page):
127-
"""Helper method to create proper template context with settings."""
128-
factory = RequestFactory()
129-
request = factory.get("/")
130-
settings_context = settings_processor(request)
131-
132-
return {"page": page, "request": request, **settings_context}
133-
134115
def _extract_jsonld_by_type(self, content, jsonld_type):
135116
"""Helper method to extract JSON-LD by type from rendered content."""
136117
soup = BeautifulSoup(content, "html.parser")
@@ -142,9 +123,8 @@ def _extract_jsonld_by_type(self, content, jsonld_type):
142123

143124
def _get_organization_jsonld(self):
144125
"""Helper method to get Organization JSON-LD from homepage."""
145-
context = self._get_template_context(self.homepage)
146-
content = render_to_string("patterns/pages/home/home_page.html", context)
147-
json_scripts = self._extract_jsonld_by_type(content, "Organization")
126+
response = self.client.get(self.homepage.url)
127+
json_scripts = self._extract_jsonld_by_type(response.content, "Organization")
148128
self.assertGreater(len(json_scripts), 0, "Organization JSON-LD not found")
149129
return json_scripts[0]
150130

@@ -155,15 +135,11 @@ def test_base_template_includes_jsonld_block(self):
155135

156136
def test_breadcrumb_template_included(self):
157137
"""Test that breadcrumb JSON-LD template is included."""
158-
# Render the breadcrumb JSON-LD template directly
159-
context = {"page": self.blog_post, "request": RequestFactory().get("/")}
160-
content = render_to_string(
161-
"patterns/navigation/components/breadcrumbs-jsonld.html", context
162-
)
138+
response = self.client.get(self.blog_post.url)
163139

164140
# Check that breadcrumb JSON-LD content is present
165-
self.assertIn("application/ld+json", content)
166-
self.assertIn("BreadcrumbList", content)
141+
self.assertIn(b"application/ld+json", response.content)
142+
self.assertIn(b"BreadcrumbList", response.content)
167143

168144
def test_blog_posting_template_included(self):
169145
"""Test that blog posting JSON-LD template is included for blog pages."""

0 commit comments

Comments
 (0)