Skip to content

Commit 91d2d8e

Browse files
committed
Simplify JSON-LD tests with helper methods
- Add _extract_jsonld_by_type() helper to eliminate repetitive JSON-LD extraction - Add _get_organization_jsonld() helper for cleaner test methods - Reduce test_organization_jsonld_social_links from 30+ lines to 8 lines - All tests still pass with same coverage - Much more maintainable and readable
1 parent 896c186 commit 91d2d8e

File tree

1 file changed

+60
-99
lines changed

1 file changed

+60
-99
lines changed

tbx/core/tests/test_jsonld.py

Lines changed: 60 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,11 @@ def _get_template_context(self, page):
3333

3434
return {"page": page, "request": request, **settings_context}
3535

36-
def test_organization_jsonld_renders(self):
37-
"""Test that Organization JSON-LD is rendered on the homepage."""
38-
# Render the homepage template directly
39-
context = self._get_template_context(self.homepage)
40-
content = render_to_string("patterns/pages/home/home_page.html", context)
41-
42-
# Check that the JSON-LD content is valid
43-
self.assertIn("application/ld+json", content)
44-
self.assertIn("Organization", content)
45-
46-
def test_organization_jsonld_structure(self):
47-
"""Test that Organization JSON-LD contains all required fields."""
48-
# Render the homepage template directly
49-
context = self._get_template_context(self.homepage)
50-
content = render_to_string("patterns/pages/home/home_page.html", context)
51-
52-
# Extract Organization JSON-LD from the response
36+
def _extract_jsonld_by_type(self, content, jsonld_type):
37+
"""Helper method to extract JSON-LD by type from rendered content."""
5338
start_marker = '<script type="application/ld+json">'
5439
end_marker = "</script>"
5540

56-
# Find all JSON-LD scripts and look for the organization one
5741
json_scripts = []
5842
start_idx = 0
5943
while True:
@@ -67,15 +51,30 @@ def test_organization_jsonld_structure(self):
6751
json_content = content[start_idx + len(start_marker) : end_idx].strip()
6852
try:
6953
json_data = json.loads(json_content)
70-
if json_data.get("@type") == "Organization":
54+
if json_data.get("@type") == jsonld_type:
7155
json_scripts.append(json_data)
7256
except json.JSONDecodeError:
7357
pass
7458
start_idx = end_idx + len(end_marker)
7559

60+
return json_scripts
61+
62+
def _get_organization_jsonld(self):
63+
"""Helper method to get Organization JSON-LD from homepage."""
64+
context = self._get_template_context(self.homepage)
65+
content = render_to_string("patterns/pages/home/home_page.html", context)
66+
json_scripts = self._extract_jsonld_by_type(content, "Organization")
7667
self.assertGreater(len(json_scripts), 0, "Organization JSON-LD not found")
68+
return json_scripts[0]
7769

78-
org_data = json_scripts[0]
70+
def test_organization_jsonld_renders(self):
71+
"""Test that Organization JSON-LD is rendered on the homepage."""
72+
org_data = self._get_organization_jsonld()
73+
self.assertEqual(org_data["@type"], "Organization")
74+
75+
def test_organization_jsonld_structure(self):
76+
"""Test that Organization JSON-LD contains all required fields."""
77+
org_data = self._get_organization_jsonld()
7978

8079
# Test required fields
8180
self.assertEqual(org_data["@context"], "https://schema.org")
@@ -106,34 +105,7 @@ def test_organization_jsonld_structure(self):
106105

107106
def test_organization_jsonld_social_links(self):
108107
"""Test that Organization JSON-LD includes correct social media links."""
109-
# Render the homepage template directly
110-
context = self._get_template_context(self.homepage)
111-
content = render_to_string("patterns/pages/home/home_page.html", context)
112-
113-
# Extract Organization JSON-LD
114-
start_marker = '<script type="application/ld+json">'
115-
end_marker = "</script>"
116-
117-
json_scripts = []
118-
start_idx = 0
119-
while True:
120-
start_idx = content.find(start_marker, start_idx)
121-
if start_idx == -1:
122-
break
123-
end_idx = content.find(end_marker, start_idx)
124-
if end_idx == -1:
125-
break
126-
127-
json_content = content[start_idx + len(start_marker) : end_idx].strip()
128-
try:
129-
json_data = json.loads(json_content)
130-
if json_data.get("@type") == "Organization":
131-
json_scripts.append(json_data)
132-
except json.JSONDecodeError:
133-
pass
134-
start_idx = end_idx + len(end_marker)
135-
136-
org_data = json_scripts[0]
108+
org_data = self._get_organization_jsonld()
137109
same_as = org_data["sameAs"]
138110

139111
# Test that all social links are valid URLs
@@ -145,34 +117,7 @@ def test_organization_jsonld_social_links(self):
145117

146118
def test_organization_jsonld_logo_url(self):
147119
"""Test that Organization JSON-LD includes correct logo URL."""
148-
# Render the homepage template directly
149-
context = self._get_template_context(self.homepage)
150-
content = render_to_string("patterns/pages/home/home_page.html", context)
151-
152-
# Extract Organization JSON-LD
153-
start_marker = '<script type="application/ld+json">'
154-
end_marker = "</script>"
155-
156-
json_scripts = []
157-
start_idx = 0
158-
while True:
159-
start_idx = content.find(start_marker, start_idx)
160-
if start_idx == -1:
161-
break
162-
end_idx = content.find(end_marker, start_idx)
163-
if end_idx == -1:
164-
break
165-
166-
json_content = content[start_idx + len(start_marker) : end_idx].strip()
167-
try:
168-
json_data = json.loads(json_content)
169-
if json_data.get("@type") == "Organization":
170-
json_scripts.append(json_data)
171-
except json.JSONDecodeError:
172-
pass
173-
start_idx = end_idx + len(end_marker)
174-
175-
org_data = json_scripts[0]
120+
org_data = self._get_organization_jsonld()
176121
logo_url = org_data["logo"]
177122

178123
# Test that logo URL is correct
@@ -201,15 +146,44 @@ def _get_template_context(self, page):
201146

202147
return {"page": page, "request": request, **settings_context}
203148

204-
def test_base_template_includes_jsonld_block(self):
205-
"""Test that the base template includes the extra_jsonld block."""
206-
# Render the homepage template directly
149+
def _extract_jsonld_by_type(self, content, jsonld_type):
150+
"""Helper method to extract JSON-LD by type from rendered content."""
151+
start_marker = '<script type="application/ld+json">'
152+
end_marker = "</script>"
153+
154+
json_scripts = []
155+
start_idx = 0
156+
while True:
157+
start_idx = content.find(start_marker, start_idx)
158+
if start_idx == -1:
159+
break
160+
end_idx = content.find(end_marker, start_idx)
161+
if end_idx == -1:
162+
break
163+
164+
json_content = content[start_idx + len(start_marker) : end_idx].strip()
165+
try:
166+
json_data = json.loads(json_content)
167+
if json_data.get("@type") == jsonld_type:
168+
json_scripts.append(json_data)
169+
except json.JSONDecodeError:
170+
pass
171+
start_idx = end_idx + len(end_marker)
172+
173+
return json_scripts
174+
175+
def _get_organization_jsonld(self):
176+
"""Helper method to get Organization JSON-LD from homepage."""
207177
context = self._get_template_context(self.homepage)
208178
content = render_to_string("patterns/pages/home/home_page.html", context)
179+
json_scripts = self._extract_jsonld_by_type(content, "Organization")
180+
self.assertGreater(len(json_scripts), 0, "Organization JSON-LD not found")
181+
return json_scripts[0]
209182

210-
# Check that JSON-LD content is present (which means the block is working)
211-
self.assertIn("application/ld+json", content)
212-
self.assertIn("Organization", content)
183+
def test_base_template_includes_jsonld_block(self):
184+
"""Test that the base template includes the extra_jsonld block."""
185+
org_data = self._get_organization_jsonld()
186+
self.assertEqual(org_data["@type"], "Organization")
213187

214188
def test_breadcrumb_template_included(self):
215189
"""Test that breadcrumb JSON-LD template is included."""
@@ -245,23 +219,10 @@ def test_breadcrumb_template_exists(self):
245219

246220
def test_jsonld_script_tags_present(self):
247221
"""Test that JSON-LD script tags are present in the rendered HTML."""
248-
# Render the homepage template directly
249-
context = self._get_template_context(self.homepage)
250-
content = render_to_string("patterns/pages/home/home_page.html", context)
251-
252-
# Check for JSON-LD script tags
253-
self.assertIn('<script type="application/ld+json">', content)
254-
self.assertIn("</script>", content)
222+
org_data = self._get_organization_jsonld()
223+
self.assertIsNotNone(org_data)
255224

256225
def test_multiple_jsonld_scripts(self):
257226
"""Test that multiple JSON-LD scripts can be present on a page."""
258-
# Render the homepage template directly
259-
context = self._get_template_context(self.homepage)
260-
content = render_to_string("patterns/pages/home/home_page.html", context)
261-
262-
# Count JSON-LD script tags
263-
script_count = content.count('<script type="application/ld+json">')
264-
self.assertGreater(script_count, 0, "No JSON-LD scripts found")
265-
266-
# Should have at least the organization schema
267-
self.assertIn("Organization", content)
227+
org_data = self._get_organization_jsonld()
228+
self.assertEqual(org_data["@type"], "Organization")

0 commit comments

Comments
 (0)