diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ddb159b..751aa08 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/src/ds_caselaw_utils/factory.py b/src/ds_caselaw_utils/factory.py new file mode 100644 index 0000000..ada9998 --- /dev/null +++ b/src/ds_caselaw_utils/factory.py @@ -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 diff --git a/src/ds_caselaw_utils/test_courts.py b/src/ds_caselaw_utils/test_courts.py index adceb71..b4d04fd 100644 --- a/src/ds_caselaw_utils/test_courts.py +++ b/src/ds_caselaw_utils/test_courts.py @@ -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, @@ -15,7 +17,6 @@ CourtWithJurisdiction, courts, ) -from .factory import CourtFactory, make_court_repo_valid def mock_with_properties(properties={}): @@ -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]) @@ -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) @@ -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]) @@ -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]) @@ -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) @@ -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") @@ -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) @@ -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) @@ -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") @@ -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") @@ -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()]) @@ -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()]) @@ -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]) @@ -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]) diff --git a/src/ds_caselaw_utils/test_factory.py b/src/ds_caselaw_utils/test_factory.py new file mode 100644 index 0000000..70c0490 --- /dev/null +++ b/src/ds_caselaw_utils/test_factory.py @@ -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))