Skip to content

Commit

Permalink
refactor(Court): make Markdown loading/rendering reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonj04 committed Jan 21, 2025
1 parent 1448a71 commit 0013f53
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/ds_caselaw_utils/courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ def get_jurisdiction(self, code: str) -> Optional[Jurisdiction]:
def expand_jurisdictions(self) -> list["Court"]:
return [self] + [CourtWithJurisdiction(self, jurisdiction) for jurisdiction in self.jurisdictions]

@cached_property
def description_text_as_html(self) -> Optional[str]:
def _render_markdown_text(self, type: str) -> Optional[str]:
if not self.canonical_param:
return None

filename = self.canonical_param.replace("/", "_")
description_md_file_path = pathlib.Path(__file__).parent / f"data/descriptions/{filename}.md"
description_md_file_path = pathlib.Path(__file__).parent / f"data/markdown/{type}/{filename}.md"
try:
with open(description_md_file_path) as file:
return str(md.render(file.read()))
except FileNotFoundError:
return None

@cached_property
def description_text_as_html(self) -> Optional[str]:
return self._render_markdown_text("description")

def __repr__(self) -> str:
return self.name

Expand Down
16 changes: 12 additions & 4 deletions src/ds_caselaw_utils/test_courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,26 @@ def test_expand_jurisdictions(self):
for c in expanded:
assert issubclass(type(c), Court)

def test_description_text_as_html_if_no_canonical_param(self):
def test_render_markdown_text_if_no_canonical_param(self):
court = CourtFactory({"param": "test"})
assert court.description_text_as_html is None

def test_description_text_as_html_if_no_file(self):
def test_render_markdown_text_if_no_file(self):
court = CourtFactory({"param": "test"})
assert court.description_text_as_html is None

def test_description_text_as_html_if_file(self):
def test_render_markdown_text_if_file(self):
court = CourtFactory({"param": "test"})
with patch("pathlib.Path.is_file", True), patch("builtins.open", mock_open(read_data="**Test** description.")):
assert court.description_text_as_html == "<p><strong>Test</strong> description.</p>\n"
assert court._render_markdown_text("test") == "<p><strong>Test</strong> description.</p>\n"

@patch("ds_caselaw_utils.courts.Court._render_markdown_text")
def test_description_text_as_html(self, mock_render):
court = CourtFactory({"param": "test"})
mock_render.return_value = "<p>Description.</p>"

assert court.description_text_as_html == "<p>Description.</p>"
mock_render.assert_called_once_with("description")


class TestCourtWithJurisdiction(unittest.TestCase):
Expand Down

0 comments on commit 0013f53

Please sign in to comment.