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

test: test for bad dates #198

Merged
merged 1 commit into from
Jul 10, 2023
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
2 changes: 1 addition & 1 deletion src/ome_types/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ParserKwargs(TypedDict, total=False):

def _get_ome_type(xml: str | bytes) -> type[OMEType]:
"""Resolve a python model class for the root element of an OME XML document."""
if isinstance(xml, str) and not xml.startswith("<"):
if isinstance(xml, str) and not xml.lstrip().startswith("<"):
root = ET.parse(xml).getroot() # noqa: S314
else:
if not isinstance(xml, bytes):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,21 @@ def test_xml_annotation() -> None:
assert isinstance(xml_ann.value.any_elements[0], AnyElement)

assert raw_xml in to_xml(xml_ann, indent=0)


def test_bad_date() -> None:
"""Test that dates with too many microseconds are handled gracefully."""

XML = """
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06">
<Image ID="Image:0">
<AcquisitionDate>2020-09-08T17:26:16.769000000</AcquisitionDate>
<Pixels DimensionOrder="XYCTZ" Type="uint8" SizeC="1" SizeT="1"
SizeX="18" SizeY="24" SizeZ="5">
</Pixels>
</Image>
</OME>
"""

obj = from_xml(XML)
assert obj.images[0].acquisition_date.microsecond == 769000 # type: ignore