Skip to content

Commit

Permalink
fix(Identifiers): fix case where unpacking unknown identifier type wo…
Browse files Browse the repository at this point in the history
…uld raise an exception
  • Loading branch information
jacksonj04 committed Dec 20, 2024
1 parent 734df18 commit 90e7b53
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog 1.0.0].

## Unreleased

### Fix

- **Identifiers**: fix case where unpacking unknown identifier type would raise an exception

## v29.0.0 (2024-12-18)

### BREAKING CHANGE
Expand Down
13 changes: 10 additions & 3 deletions src/caselawclient/models/identifiers/unpacker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional
from warnings import warn

from lxml import etree

Expand All @@ -19,12 +20,13 @@ def unpack_all_identifiers_from_etree(identifiers_etree: Optional[etree._Element
return identifiers
for identifier_etree in identifiers_etree.findall("identifier"):
identifier = unpack_an_identifier_from_etree(identifier_etree)
identifiers.add(identifier)
if identifier:
identifiers.add(identifier)
return identifiers


def unpack_an_identifier_from_etree(identifier_xml: etree._Element) -> Identifier:
"""Given an etree representation of a single identifier, unpack it into an appropriate instance of an Identifier."""
def unpack_an_identifier_from_etree(identifier_xml: etree._Element) -> Optional[Identifier]:
"""Given an etree representation of a single identifier, unpack it into an appropriate instance of an Identifier if the type is known (otherwise return `None`)."""

namespace_element = identifier_xml.find("namespace")

Expand All @@ -33,6 +35,11 @@ def unpack_an_identifier_from_etree(identifier_xml: etree._Element) -> Identifie
"Identifer XML representation is not valid: namespace not present or empty"
)

# If the identifier namespace isn't known, fail out
if namespace_element.text not in IDENTIFIER_NAMESPACE_MAP:
warn(f"Identifier type {namespace_element.text} is not known.")
return None

kwargs: dict[str, str] = {}

for attribute in IDENTIFIER_UNPACKABLE_ATTRIBUTES:
Expand Down
18 changes: 17 additions & 1 deletion tests/models/identifiers/test_identifer_unpacking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import unittest
from unittest.mock import patch

from lxml import etree
Expand All @@ -7,7 +8,7 @@
from caselawclient.models.identifiers.unpacker import unpack_all_identifiers_from_etree, unpack_an_identifier_from_etree


class TestIdentifierUnpacking:
class TestIdentifierUnpacking(unittest.TestCase):
@patch("caselawclient.models.identifiers.unpacker.IDENTIFIER_NAMESPACE_MAP", {"test": TestIdentifier})
def test_unpack_identifier(self):
xml_tree = etree.fromstring("""
Expand All @@ -24,6 +25,21 @@ def test_unpack_identifier(self):
assert unpacked_identifier.uuid == "2d80bf1d-e3ea-452f-965c-041f4399f2dd"
assert unpacked_identifier.value == "TEST-123"

@patch("caselawclient.models.identifiers.unpacker.IDENTIFIER_NAMESPACE_MAP", {"test": TestIdentifier})
def test_unpack_unknown_identifier(self):
xml_tree = etree.fromstring("""
<identifier>
<namespace>unknown</namespace>
<uuid>2d80bf1d-e3ea-452f-965c-041f4399f2dd</uuid>
<value>UK-123</value>
</identifier>
""")

with self.assertWarnsRegex(Warning, "Identifier type unknown is not known."):
unpacked_identifier = unpack_an_identifier_from_etree(xml_tree)

assert unpacked_identifier is None


class TestIdentifierPackUnpackRoundTrip:
@patch("caselawclient.models.identifiers.unpacker.IDENTIFIER_NAMESPACE_MAP", {"test": TestIdentifier})
Expand Down

0 comments on commit 90e7b53

Please sign in to comment.