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 92cc036
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
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 explicitly raise a CourtNotFoundError for unknown jurisdictions.

## [Release 1.4.0]

- Add jurisdiction metadata to courts to support GRC subdivisions
Expand Down
9 changes: 5 additions & 4 deletions 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 @@ -130,11 +130,12 @@ def get_court_by_code(self, code):

def get_court_with_jurisdiction_by_code(self, court_code, jursidiction_code):
court = self.get_court_by_code(court_code)
if court is None:
raise CourtNotFoundException()
jurisdiction = court.get_jurisdiction(jursidiction_code)
if jurisdiction is not None:
return CourtWithJurisdiction(court, jurisdiction)
else:
if jurisdiction is None:
raise CourtNotFoundException()
return CourtWithJurisdiction(court, jurisdiction)

def get_by_code(self, code):
if "/" in code:
Expand Down
30 changes: 30 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,29 @@ def test_loads_court_with_jurisdiction_by_code(self):
"Court 1 – Jurisdiction 1", repo.get_by_code("court1/jurisdiction1").name
)

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

def test_raises_on_unknown_court_code(self):
data = [
{
Expand Down Expand Up @@ -354,6 +377,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 92cc036

Please sign in to comment.