Skip to content

Commit

Permalink
Raise exception if document is not a version
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Sep 12, 2023
1 parent e837a0c commit 2b0d3ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/caselawclient/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ class NotSupportedOnVersion(MarklogicAPIError):
# This error does not come from Marklogic, but is an error raised by this API...
status_code = 400
default_message = "An operation was attempted on a version of a document which cannot occur on a version."


class OnlySupportedOnVersion(MarklogicAPIError):
status_code = 400
default_message = "The operation requested cannot be performed on a document that is not a version."
20 changes: 16 additions & 4 deletions src/caselawclient/models/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

from caselawclient.models.utilities import extract_version

from ..errors import DocumentNotFoundError, NotSupportedOnVersion
from ..errors import (
DocumentNotFoundError,
NotSupportedOnVersion,
OnlySupportedOnVersion,
)
from ..xml_helpers import get_xpath_match_string, get_xpath_match_strings
from .utilities import VersionsDict, get_judgment_root, render_versions
from .utilities.aws import (
Expand Down Expand Up @@ -261,13 +265,21 @@ def versions_as_documents(self) -> list[Any]:

@cached_property
def version_number(self) -> int:
"Zero if the version is current, otherwise the highest number is the most recent version"
return extract_version(self.uri)
"""
Note that the highest number is the most recent version.
Raises an exception if it is not a version (e.g. /2022/eat/1 is not a version)
"""
version = extract_version(self.uri)
if version == 0:
raise OnlySupportedOnVersion(
f"Version number requested for {self.uri} which is not a version"
)
return version

@cached_property
def is_version(self) -> bool:
"Is this document a potentially historic version of a document, or is it the main document itself?"
return self.version_number != 0
return extract_version(self.uri) != 0

@cached_property
def content_as_xml(self) -> str:
Expand Down
9 changes: 7 additions & 2 deletions tests/models/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from lxml import etree

from caselawclient.Client import MarklogicApiClient
from caselawclient.errors import DocumentNotFoundError, NotSupportedOnVersion
from caselawclient.errors import (
DocumentNotFoundError,
NotSupportedOnVersion,
OnlySupportedOnVersion,
)
from caselawclient.models.documents import (
DOCUMENT_STATUS_HOLD,
DOCUMENT_STATUS_IN_PROGRESS,
Expand Down Expand Up @@ -186,7 +190,8 @@ def test_document_versions_happy_case(self, mock_api_client):

def test_document_version_number_when_not_version(self, mock_api_client):
base_document = Document("test/1234", mock_api_client)
assert base_document.version_number == 0
with pytest.raises(OnlySupportedOnVersion):
base_document.version_number
assert not base_document.is_version

def test_document_version_number_when_is_version(self, mock_api_client):
Expand Down

0 comments on commit 2b0d3ba

Please sign in to comment.