Skip to content

Commit

Permalink
tests(FCL-343): Ensure tests no longer have incomplete RawCourtRepo data
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Sep 27, 2024
1 parent 84aa896 commit 9b550e6
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ repos:
args:
- --strict
files: ^(src/ds_caselaw_utils|scripts)
exclude: (?:test_|courts_schema_types_autogenerated\.py)
exclude: (?:test_|courts_schema_types_autogenerated\.py|factory.py)

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
Expand Down
29 changes: 29 additions & 0 deletions src/ds_caselaw_utils/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from .courts import Court
from .courts_schema_types_autogenerated import RawCourt, RawCourtRepository


class CourtFactory(Court):
def __init__(self, data):
data = make_court_valid(data)
super().__init__(data)


def make_court_valid(data) -> RawCourt:
for keyword in ["code", "name", "link"]:
if keyword not in data:
data[keyword] = f"placeholder {keyword}"
for keyword in ["selectable", "listable"]:
if keyword not in data:
data[keyword] = True
return data


def make_court_repo_valid(data) -> RawCourtRepository:
new_court_groups = []
for court_group in data:
new_courts = []
for court in court_group["courts"]:
new_courts.append(make_court_valid(court))
court_group["court"] = new_courts
new_court_groups.append(court_group)
return new_court_groups
20 changes: 17 additions & 3 deletions src/ds_caselaw_utils/test_courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from ruamel.yaml import YAML

from ds_caselaw_utils.factory import CourtFactory, make_court_repo_valid

from .courts import (
Court,
CourtCode,
Expand All @@ -15,7 +17,6 @@
CourtWithJurisdiction,
courts,
)
from .factory import CourtFactory, make_court_repo_valid


def mock_with_properties(properties={}):
Expand All @@ -33,9 +34,10 @@ def test_loads_all_courts_without_jurisdictions(self):
{
"name": "court_group",
"display_name": "court group 1",
"courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1"}]}],
"courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1", "code": "code"}]}],
}
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
courts = repo.get_all()
self.assertIn("court1", [c.name for c in courts])
Expand All @@ -46,7 +48,7 @@ def test_loads_all_courts_with_jurisdictions(self):
{
"name": "court_group",
"display_name": "court group 1",
"courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1"}]}],
"courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1", "code": "code"}]}],
}
]
data = make_court_repo_valid(data)
Expand Down Expand Up @@ -74,6 +76,7 @@ def test_loads_selectable_courts(self):
"courts": [{"name": "court3", "selectable": False}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
selectable = repo.get_selectable()
self.assertIn("court1", [c.name for c in selectable])
Expand Down Expand Up @@ -104,6 +107,7 @@ def test_loads_listable_courts(self):
"courts": [{"name": "court3", "listable": False}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
groups = repo.get_listable_groups()
self.assertIn("court group 1", [g.name for g in groups])
Expand All @@ -123,6 +127,7 @@ def test_loads_court_by_param(self):
"courts": [{"param": "court2", "name": "Court 2"}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertEqual("Court 2", repo.get_by_param(CourtParam("court2")).name)

Expand All @@ -137,6 +142,7 @@ def test_raises_on_unknown_court_param(self):
"courts": [{"param": "court2", "name": "Court 2"}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertRaises(CourtNotFoundException, repo.get_by_param, "court3")

Expand All @@ -151,6 +157,7 @@ def test_loads_court_by_code(self):
"courts": [{"code": "court2", "name": "Court 2"}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertEqual("Court 2", repo.get_by_code(CourtCode("court2")).name)

Expand All @@ -167,6 +174,7 @@ def test_loads_court_with_jurisdiction_by_code(self):
],
}
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertEqual("Court 1 – Jurisdiction 1", repo.get_by_code(CourtCode("court1/jurisdiction1")).name)

Expand All @@ -183,6 +191,7 @@ def test_raises_error_for_nonexistent_jurisdictions(self):
],
}
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertRaises(CourtNotFoundException, repo.get_by_code, "court1/jurisdiction2")
self.assertRaises(CourtNotFoundException, repo.get_by_code, "court2/jurisdiction1")
Expand All @@ -198,6 +207,7 @@ def test_raises_on_unknown_court_code(self):
"courts": [{"code": "court2", "name": "Court 2"}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertRaises(CourtNotFoundException, repo.get_by_code, "court3")

Expand All @@ -217,6 +227,7 @@ def test_returns_listable_courts(self):
"courts": [{"param": "court3", "listable": True, "name": "Court 3"}],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertIn("court1", [c.canonical_param for c in repo.get_listable_courts()])
self.assertNotIn("court2", [c.canonical_param for c in repo.get_listable_courts()])
Expand All @@ -240,6 +251,7 @@ def test_returns_listable_tribunals(self):
],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
self.assertNotIn("court1", [c.canonical_param for c in repo.get_listable_tribunals()])
self.assertNotIn("court2", [c.canonical_param for c in repo.get_listable_tribunals()])
Expand Down Expand Up @@ -273,6 +285,7 @@ def test_returns_grouped_selectable_courts(self):
],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
groups = repo.get_grouped_selectable_courts()
self.assertIn("Court group", [g.name for g in groups])
Expand Down Expand Up @@ -309,6 +322,7 @@ def test_returns_grouped_selectable_tribunals(self):
],
},
]
data = make_court_repo_valid(data)
repo = CourtsRepository(data)
groups = repo.get_grouped_selectable_tribunals()
self.assertIn("Tribunal group", [g.name for g in groups])
Expand Down
84 changes: 84 additions & 0 deletions src/ds_caselaw_utils/test_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from .factory import make_court_repo_valid


def test_factory():
input_data = [
{
"name": "court_group",
"display_name": "court group 1",
"courts": [
{
"name": "court1",
"selectable": True,
},
{"name": "court2", "selectable": False},
],
},
{
"name": "court_group2",
"display_name": "court group 2",
"courts": [{"name": "court3", "selectable": False}],
},
]

output_data = [
{
"name": "court_group",
"display_name": "court group 1",
"courts": [
{
"name": "court1",
"selectable": True,
"code": "placeholder code",
"link": "placeholder link",
"listable": True,
},
{
"name": "court2",
"selectable": False,
"code": "placeholder code",
"link": "placeholder link",
"listable": True,
},
],
"court": [
{
"name": "court1",
"selectable": True,
"code": "placeholder code",
"link": "placeholder link",
"listable": True,
},
{
"name": "court2",
"selectable": False,
"code": "placeholder code",
"link": "placeholder link",
"listable": True,
},
],
},
{
"name": "court_group2",
"display_name": "court group 2",
"courts": [
{
"name": "court3",
"selectable": False,
"code": "placeholder code",
"link": "placeholder link",
"listable": True,
}
],
"court": [
{
"name": "court3",
"selectable": False,
"code": "placeholder code",
"link": "placeholder link",
"listable": True,
}
],
},
]
assert str(output_data) == str(make_court_repo_valid(input_data))

0 comments on commit 9b550e6

Please sign in to comment.