Skip to content

Commit

Permalink
Write tests for the find_h7_headers function
Browse files Browse the repository at this point in the history
Pretty simple function, so only 1 test.

The main thing is to make clarify that subsections with H7s inside of
them do not get returned in the find_h7_headers function.
  • Loading branch information
pcraig3 authored and admoorgit committed Nov 12, 2024
1 parent 0407484 commit 089f585
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve

### Added

- New alert box for NOFOs that have H7 headings
- H7 headings need to be manually fixed in the PDF
- New column for the heading level in the nofo_edit view
- Slight change to subsection_edit page to match new heading tag styling

### Changed

### Fixed
Expand Down
17 changes: 15 additions & 2 deletions bloom_nofos/nofos/nofo.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,22 @@ def _href_starts_with_h(tag):

def find_h7_headers(nofo):
"""
Identifies and returns a list of H7 headings
Identifies and returns a list of H7 subsections from a given NOFO.
Note that it doesn't work if H7s are created within body text, they have to be subsections.
This function iterates through all sections and subsections of a NOFO object,
identifying subsections tagged as "h7".
Note:
- H7 tags must be directly defined as subsections. H7s created within
the body text of a subsection are not identified.
Returns:
list: A list of dictionaries, each containing details of subsections
tagged as "h7". Each dictionary includes the following keys:
- "section": The parent section object of the H7 subsection.
- "subsection": The subsection object tagged as "h7".
- "name": The name of the H7 subsection.
- "html_id": The HTML ID associated with the H7 subsection.
"""

h7_headers = []
Expand Down
68 changes: 68 additions & 0 deletions bloom_nofos/nofos/test_nofo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
decompose_instructions_tables,
find_broken_links,
find_external_links,
find_h7_headers,
find_incorrectly_nested_heading_levels,
find_same_or_higher_heading_levels_consecutive,
get_cover_image,
Expand Down Expand Up @@ -2687,6 +2688,73 @@ def test_find_broken_links_ignores_valid_links(self):
self.assertEqual(len(valid_links), 0)


class TestFindH7Headers(TestCase):
def setUp(self):
self.sections = [
{
"name": "New Section H7",
"order": 1,
"html_id": "",
"has_section_page": True,
"subsections": [
{
"name": "New Subsection H7",
"order": 1,
"tag": "h7",
"body": ["<p>New Section H7 body</p>"],
},
{
"name": "New Subsection H6",
"order": 2,
"tag": "h6",
"body": ["<p>New Section H6 body</p>"],
},
{
"name": "New Subsection H5",
"order": 3,
"tag": "h5",
"body": [
"<p>New Section H5 body</p><h7>This h7 will not be recognized</h7>"
],
},
{
"name": "New Subsection H4",
"order": 4,
"tag": "h4",
"body": [
'<p>New Section H4 body</p><div role="heading" aria-level="7">This shimmed h7 will not be recognized</div>'
],
},
],
}
]

def test_find_h7_headers_find_all_h7s(self):
"""
Test finding the h7 in a nofo object successfully
"""
nofo = create_nofo("Test Nofo", self.sections)
self.assertEqual(nofo.title, "Test Nofo")
self.assertEqual(nofo.number, "NOFO #999")
self.assertEqual(nofo.sections.first().name, "New Section H7")

subsections = list(nofo.sections.first().subsections.all().order_by("order"))

self.assertEqual(len(subsections), 4)
self.assertEqual(subsections[0].name, "New Subsection H7")
self.assertEqual(subsections[1].name, "New Subsection H6")
self.assertEqual(subsections[2].name, "New Subsection H5")
self.assertEqual(subsections[3].name, "New Subsection H4")

h7_headers = find_h7_headers(nofo)
self.assertEqual(len(h7_headers), 1)
self.assertEqual(h7_headers[0]["name"], "New Subsection H7")
self.assertEqual(
h7_headers[0]["html_id"], "1--new-section-h7--new-subsection-h7"
)
self.assertEqual(h7_headers[0]["section"], nofo.sections.first())


class TestUpdateLinkStatuses(TestCase):
@patch("nofos.nofo.requests.head")
def test_status_code_200(self, mock_head):
Expand Down

0 comments on commit 089f585

Please sign in to comment.