Skip to content

Commit

Permalink
fix(tests): fix tests for bootstrap 5
Browse files Browse the repository at this point in the history
Add test mix-in to support formatted templates.
  • Loading branch information
dchiller committed Dec 6, 2024
1 parent 22a34cf commit 17286fe
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 62 deletions.
64 changes: 64 additions & 0 deletions django/cantusdb_project/main_app/tests/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Assorted mixins for test cases.
"""

from typing import Optional
from django.http import HttpResponse
from bs4 import BeautifulSoup


class HTMLContentsTestMixin:
"""
A mixin that provides a `assertParsedContains` method to check for
HTML contents in the response content.
This mixin uses the BeautifulSoup library to parse the response content
which is especially useful for dealing with more complex HTML checks and
for dealing with disparate whitespace in rendered HTML.
"""

def assertParsedContains(
self,
response: HttpResponse,
text: str,
count: Optional[int] = None,
status_code: int = 200,
html: bool = True,
) -> None:
"""
Asserts that the response content contains the given text.
:param response: The response object to check.
:param text: The text to check for in the response content.
:param count: The number of times the text should appear in the
response content.
:param status_code: The expected status code of the response.
:param html: If True, the text is treated as HTML and parsed with
BeautifulSoup.
"""
self.assertEqual(
response.status_code,
status_code,
"Response status code was %d (expected %d)"
% (response.status_code, status_code),
)

if html:
soup = BeautifulSoup(response.content, "lxml")
contents = str(soup)
else:
contents = response.content.decode("utf-8")

if count is not None:
self.assertEqual(
contents.count(text),
count,
"The text '%s' appeared %d times in the response "
"content (expected %d times)" % (text, contents.count(text), count),
)
else:
self.assertIn(
text,
contents,
"The text '%s' was not found in the response content" % text,
)
92 changes: 38 additions & 54 deletions django/cantusdb_project/main_app/tests/test_views/test_chant.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,12 +1549,10 @@ def test_source_link_column(self):
response = self.client.get(
reverse("chant-search"), {"keyword": search_term, "op": "contains"}
)
html = str(response.content)
# self.assertContains(response, source_heading, html=True)
# self.assertContains(response, source_short_heading, html=True)
# self.assertContains(response, url, html=True)
self.assertIn(
f'<a href="{url}" title="{source.heading}">{source.short_heading}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{source.heading}">{source.short_heading}</a>',
html=True,
)

def test_folio_column(self):
Expand Down Expand Up @@ -1587,12 +1585,10 @@ def test_feast_column(self):
response = self.client.get(
reverse("chant-search"), {"keyword": search_term, "op": "contains"}
)
html = str(response.content)
self.assertIn(feast_name, html)
self.assertIn(feast_description, html)
self.assertIn(url, html)
self.assertIn(
f'<a href="{url}" title="{feast_description}">{feast_name}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{feast_description}">{feast_name}</a>',
html=True,
)

def test_service_column(self):
Expand All @@ -1611,12 +1607,10 @@ def test_service_column(self):
response = self.client.get(
reverse("chant-search"), {"keyword": search_term, "op": "contains"}
)
html = str(response.content)
self.assertIn(service_name, html)
self.assertIn(service_description, html)
self.assertIn(url, html)
self.assertIn(
f'<a href="{url}" title="{service_description}">{service_name}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{service_description}">{service_name}</a>',
html=True,
)

def test_genre_column(self):
Expand All @@ -1635,12 +1629,10 @@ def test_genre_column(self):
response = self.client.get(
reverse("chant-search"), {"keyword": search_term, "op": "contains"}
)
html = str(response.content)
self.assertIn(genre_name, html)
self.assertIn(genre_description, html)
self.assertIn(url, html)
self.assertIn(
f'<a href="{url}" title="{genre_description}">{genre_name}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{genre_description}">{genre_name}</a>',
html=True,
)

def test_position_column(self):
Expand Down Expand Up @@ -1671,10 +1663,9 @@ def test_cantus_id_column(self):
response = self.client.get(
reverse("chant-search"), {"keyword": search_term, "op": "contains"}
)
html = str(response.content)
self.assertIn(cantus_id, html)
self.assertIn(url, html)
self.assertIn(f'<a href="{url}" target="_blank">{cantus_id}</a>', html)
self.assertContains(
response, f'<a href="{url}" target="_blank">{cantus_id}</a>', html=True
)

def test_mode_column(self):
source = make_fake_source(published=True)
Expand Down Expand Up @@ -1712,7 +1703,7 @@ def test_manuscript_full_text_column(self):
"\\xe2\\x9c\\x94", # checkmark character
html,
)
self.assertIn(
self.assertInHTML(
'<span title="Chant record includes Manuscript Full Text">\\xe2\\x9c\\x94</span>',
html,
)
Expand Down Expand Up @@ -1775,9 +1766,9 @@ def test_image_link_column(self):
response = self.client.get(
reverse("chant-search"), {"keyword": search_term, "op": "contains"}
)
html = str(response.content)
self.assertIn(image_link, html)
self.assertIn(f'<a href="{image_link}" target="_blank">Image</a>', html)
self.assertContains(
response, f'<a href="{image_link}" target="_blank">Image</a>', html=True
)


class ChantSearchMSViewTest(TestCase):
Expand Down Expand Up @@ -2550,12 +2541,10 @@ def test_feast_column(self):
reverse("chant-search-ms", args=[source.id]),
{"keyword": search_term, "op": "contains"},
)
html = str(response.content)
self.assertIn(feast_name, html)
self.assertIn(feast_description, html)
self.assertIn(url, html)
self.assertIn(
f'<a href="{url}" title="{feast_description}">{feast_name}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{feast_description}">{feast_name}</a>',
html=True,
)

def test_service_column(self):
Expand All @@ -2575,12 +2564,10 @@ def test_service_column(self):
reverse("chant-search-ms", args=[source.id]),
{"keyword": search_term, "op": "contains"},
)
html = str(response.content)
self.assertIn(service_name, html)
self.assertIn(service_description, html)
self.assertIn(url, html)
self.assertIn(
f'<a href="{url}" title="{service_description}">{service_name}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{service_description}">{service_name}</a>',
html=True,
)

def test_genre_column(self):
Expand All @@ -2600,12 +2587,10 @@ def test_genre_column(self):
reverse("chant-search-ms", args=[source.id]),
{"keyword": search_term, "op": "contains"},
)
html = str(response.content)
self.assertIn(genre_name, html)
self.assertIn(genre_description, html)
self.assertIn(url, html)
self.assertIn(
f'<a href="{url}" title="{genre_description}">{genre_name}</a>', html
self.assertContains(
response,
f'<a href="{url}" title="{genre_description}">{genre_name}</a>',
html=True,
)

def test_position_column(self):
Expand Down Expand Up @@ -2638,10 +2623,9 @@ def test_cantus_id_column(self):
reverse("chant-search-ms", args=[source.id]),
{"keyword": search_term, "op": "contains"},
)
html = str(response.content)
self.assertIn(cantus_id, html)
self.assertIn(url, html)
self.assertIn(f'<a href="{url}" target="_blank">{cantus_id}</a>', html)
self.assertContains(
response, f'<a href="{url}" target="_blank">{cantus_id}</a>', html=True
)

def test_mode_column(self):
source = make_fake_source(published=True)
Expand Down
11 changes: 4 additions & 7 deletions django/cantusdb_project/main_app/tests/test_views/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
make_fake_century,
add_accents_to_string,
)
from main_app.tests.mixins import HTMLContentsTestMixin

# Create a Faker instance with locale set to Latin
faker = Faker("la")
Expand Down Expand Up @@ -210,7 +211,7 @@ def test_chant_list_link(self):
self.assertNotIn(bower_chant_list_link, bower_source_html)


class SourceInventoryViewTest(TestCase):
class SourceInventoryViewTest(HTMLContentsTestMixin, TestCase):
def test_url_and_templates(self):
source = make_fake_source()
response = self.client.get(reverse("source-inventory", args=[source.id]))
Expand Down Expand Up @@ -253,12 +254,10 @@ def test_shelfmark_column(self):
source = make_fake_source(published=True, shelfmark=shelfmark)
make_fake_chant(source=source)
response = self.client.get(reverse("source-inventory", args=[source.id]))
html = str(response.content)
self.assertIn(shelfmark, html)
expected_html_substring = (
f'<td title="{source.heading}">{source.short_heading}</td>'
)
self.assertIn(expected_html_substring, html)
self.assertContains(response, expected_html_substring, html=True)

def test_marginalia_column(self):
source = make_fake_source(published=True)
Expand Down Expand Up @@ -429,12 +428,10 @@ def test_dd_column(self):
chant.save()

response = self.client.get(reverse("source-inventory", args=[source.id]))
html: str = str(response.content)
self.assertIn(diff_id, html)
expected_html_substring: str = (
f'<a href="https://differentiaedatabase.ca/differentia/{diff_id}" target="_blank">'
)
self.assertIn(expected_html_substring, html)
self.assertParsedContains(response, expected_html_substring)

def test_redirect_with_source_parameter(self):
cantus_segment = make_fake_segment(id=4063)
Expand Down
Loading

0 comments on commit 17286fe

Please sign in to comment.