Skip to content

Commit

Permalink
fix indexError on getting a non-existent jurisdiction
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Feb 7, 2024
1 parent bf4eeeb commit ede2340
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog 1.0.0].

## [Unreleased]

- Fix bug where getting a nonexistent jurisdiction for a court raised an IndexError. Now we default to returning the parent court for unknown jurisdictions.

## [Release 1.4.0]

- Add jurisdiction metadata to courts to support GRC subdivisions
Expand Down
4 changes: 3 additions & 1 deletion src/ds_caselaw_utils/courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, data):
]

def get_jurisdiction(self, code):
return [j for j in self.jurisdictions if j.code == code][0]
return next((j for j in self.jurisdictions if j.code == code), None)

def expand_jurisdictions(self):
return [self] + [
Expand Down Expand Up @@ -133,6 +133,8 @@ def get_court_with_jurisdiction_by_code(self, court_code, jursidiction_code):
jurisdiction = court.get_jurisdiction(jursidiction_code)
if jurisdiction is not None:
return CourtWithJurisdiction(court, jurisdiction)
elif court:
return court
else:
raise CourtNotFoundException()

Expand Down
25 changes: 25 additions & 0 deletions src/ds_caselaw_utils/test_courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ def test_loads_court_with_jurisdiction_by_code(self):
"Court 1 – Jurisdiction 1", repo.get_by_code("court1/jurisdiction1").name
)

def test_defaults_to_parent_court_for_nonexistent_jurisdictions(self):
data = [
{
"name": "court_group",
"courts": [
{
"code": "court1",
"name": "Court 1",
"jurisdictions": [
{"code": "jurisdiction1", "name": "Jurisdiction 1"}
],
}
],
}
]
repo = CourtsRepository(data)
self.assertEqual("Court 1", repo.get_by_code("court1/jurisdiction2").name)

def test_raises_on_unknown_court_code(self):
data = [
{
Expand Down Expand Up @@ -354,6 +372,13 @@ def test_get_jurisdiction(self):
jurisdiction = court.get_jurisdiction("jurisdiction1")
self.assertEqual("Jurisdiction 1", jurisdiction.name)

def test_get_nonexistent_jurisdiction(self):
court = Court(
{"jurisdictions": [{"code": "jurisdiction1", "name": "Jurisdiction 1"}]}
)
jurisdiction = court.get_jurisdiction("jurisdiction2")
self.assertIsNone(jurisdiction)

def test_expand_jurisdictions(self):
court = Court(
{
Expand Down

0 comments on commit ede2340

Please sign in to comment.