Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(neutral_url): neutral_url no longer returns URIs with a leading slash #218

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ python = "^3.9"
"ruamel.yaml" = "^0.18.0"
typing-extensions = "^4.12.2"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
pytest = "^8.3.3"

[tool.ruff]
line-length = 120
Expand Down
4 changes: 2 additions & 2 deletions src/ds_caselaw_utils/neutral.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

def neutral_url(citation: NeutralCitationString) -> Optional[NCNBasedUriString]:
"""Given a neutral citation such as `[2020] EAT 17`,
return a public-API URL like `/eat/2020/17`, or None
return a public-API URL like `eat/2020/17`, or None
if no match is found.
"""
for regex, groups in citation_data:
if match := re.match(regex, citation):
url_components = "/".join([match.groups()[x - 1] for x in groups])
return NCNBasedUriString(f"/{url_components}".lower())
return NCNBasedUriString(url_components.lower())
return None
67 changes: 38 additions & 29 deletions src/ds_caselaw_utils/test_neutral.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
import unittest
import pytest

from .neutral import neutral_url
from .types import NeutralCitationString


class TestNeutralURL(unittest.TestCase):
def test_good_neutral_urls(self):
self.assertEqual(neutral_url(NeutralCitationString("[2022] UKSC 1")), "/uksc/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[1604] EWCA Crim 555")), "/ewca/crim/1604/555")
self.assertEqual(neutral_url(NeutralCitationString("[2022] EWHC 1 (Comm)")), "/ewhc/comm/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[1999] EWCOP 7")), "/ewcop/1999/7")
self.assertEqual(neutral_url(NeutralCitationString("[2022] UKUT 1 (IAC)")), "/ukut/iac/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[2022] EAT 1")), "/eat/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[2022] UKFTT 1 (TC)")), "/ukftt/tc/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[2022] UKFTT 1 (GRC)")), "/ukftt/grc/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[2022] EWHC 1 (KB)")), "/ewhc/kb/2022/1")
self.assertEqual(neutral_url(NeutralCitationString("[2023] UKAIT 1")), "/ukait/2023/1")
self.assertEqual(neutral_url(NeutralCitationString("[2024] EWCOP 17 (T2)")), "/ewcop/t2/2024/17")
self.assertEqual(neutral_url(NeutralCitationString("[2000] UKIPTrib 99")), "/ukiptrib/2000/99")
self.assertEqual(neutral_url(NeutralCitationString("[2000] EWCR 99")), "/ewcr/2000/99")
self.assertEqual(neutral_url(NeutralCitationString("[2000] EWCC 99")), "/ewcc/2000/99")
class TestNeutralURL:
@pytest.mark.parametrize(
"ncn, expected_uri",
[
("[2022] UKSC 1", "uksc/2022/1"),
("[1604] EWCA Crim 555", "ewca/crim/1604/555"),
("[2022] EWHC 1 (Comm)", "ewhc/comm/2022/1"),
("[1999] EWCOP 7", "ewcop/1999/7"),
("[2022] UKUT 1 (IAC)", "ukut/iac/2022/1"),
("[2022] EAT 1", "eat/2022/1"),
("[2022] UKFTT 1 (TC)", "ukftt/tc/2022/1"),
("[2022] UKFTT 1 (GRC)", "ukftt/grc/2022/1"),
("[2022] EWHC 1 (KB)", "ewhc/kb/2022/1"),
("[2023] UKAIT 1", "ukait/2023/1"),
("[2024] EWCOP 17 (T2)", "ewcop/t2/2024/17"),
("[2000] UKIPTrib 99", "ukiptrib/2000/99"),
("[2000] EWCR 99", "ewcr/2000/99"),
],
)
def test_good_neutral_urls(self, ncn, expected_uri):
assert neutral_url(NeutralCitationString(ncn)) == expected_uri

def test_bad_neutral_urls(self):
self.assertEqual(neutral_url(NeutralCitationString("")), None)
self.assertEqual(neutral_url(NeutralCitationString("1604] EWCA Crim 555")), None)
self.assertEqual(neutral_url(NeutralCitationString("[2022 EWHC 1 Comm")), None)
self.assertEqual(neutral_url(NeutralCitationString("[1999] EWCOP")), None)
self.assertEqual(
neutral_url(NeutralCitationString("[2022] UKUT B1 IAC")), None
) # Could be a Bailii reference, might want to drop B in future.
self.assertEqual(neutral_url(NeutralCitationString("[2022] EAT A")), None)
self.assertEqual(neutral_url(NeutralCitationString("[2022] NOTACOURT 1 TC")), None)
self.assertEqual(neutral_url(NeutralCitationString("[2022] EWHC 1 (T2)")), None)
self.assertEqual(neutral_url(NeutralCitationString("[2000] EWCRC 99")), None)
@pytest.mark.parametrize(
"ncn",
[
"",
"1604] EWCA Crim 555",
"[2022 EWHC 1 Comm",
"[1999] EWCOP",
"[2022] UKUT B1 IAC", # Could be a Bailii reference, might want to drop B in future.
"[2022] EAT A",
"[2022] NOTACOURT 1 TC",
"[2022] EWHC 1 (T2)",
"[2000] EWCRC 99",
],
)
def test_bad_neutral_urls(self, ncn):
assert neutral_url(NeutralCitationString(ncn)) is None