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

Provide a meaningful error message if the IngestReader input fails XSD validation #151

Merged
merged 3 commits into from
Mar 20, 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
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Incompatible changes
Bug fixes and minor changes
---------------------------

+ `#151`_: Provide a more meaningful error message if the input to
:class:`icat.ingest.IngestReader` fails validation against the XML
Schema Definition.

+ `#141`_, `#142`_, `#150`_: Review documentation.

+ `#145`_: Review build tool chain.
Expand All @@ -46,6 +50,7 @@ Bug fixes and minor changes
.. _#148: https://github.com/icatproject/python-icat/issues/148
.. _#149: https://github.com/icatproject/python-icat/pull/149
.. _#150: https://github.com/icatproject/python-icat/pull/150
.. _#151: https://github.com/icatproject/python-icat/pull/151


.. _changes-1_2_0:
Expand Down
6 changes: 4 additions & 2 deletions src/icat/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ def __init__(self, client, metadata, investigation):
raise InvalidIngestFileError(e)
with self.get_xsd(ingest_data).open("rb") as f:
schema = etree.XMLSchema(etree.parse(f))
if not schema.validate(ingest_data):
raise InvalidIngestFileError("validation failed")
try:
schema.assertValid(ingest_data)
except etree.DocumentInvalid as exc:
raise InvalidIngestFileError("DocumentInvalid: %s" % exc)
self.add_environment(client, ingest_data)
with self.get_xslt(ingest_data).open("rb") as f:
xslt = etree.XSLT(etree.parse(f))
Expand Down
12 changes: 8 additions & 4 deletions tests/test_06_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections import namedtuple
import datetime
import io
import logging
import pytest
pytest.importorskip("lxml")
from lxml import etree
Expand All @@ -14,6 +15,7 @@
from conftest import (getConfig, gettestdata, icat_version,
get_icatdata_schema, testdatadir)

logger = logging.getLogger(__name__)

def get_test_investigation(client):
query = Query(client, "Investigation", conditions={
Expand Down Expand Up @@ -355,7 +357,7 @@ def test_ingest_schema(client, investigation, schemadir, case):
reader = IngestReader(client, case.metadata, investigation)
with get_icatdata_schema().open("rb") as f:
schema = etree.XMLSchema(etree.parse(f))
assert schema.validate(reader.infile)
schema.assertValid(reader.infile)

@pytest.mark.parametrize("case", [
pytest.param(c, id=c.metadata.name, marks=c.marks) for c in cases
Expand Down Expand Up @@ -560,9 +562,10 @@ def test_ingest_error_invalid(client, investigation, schemadir, case):
datasets = []
for name in case.data:
datasets.append(client.new("Dataset", name=name))
with pytest.raises(icat.InvalidIngestFileError):
with pytest.raises(icat.InvalidIngestFileError) as exc:
reader = IngestReader(client, case.metadata, investigation)
reader.ingest(datasets, dry_run=True, update_ds=True)
logger.info("Raised %s: %s", exc.type.__name__, exc.value)

searcherr_attr_metadata = NamedBytesIO("""<?xml version='1.0' encoding='UTF-8'?>
<icatingest version="1.0">
Expand Down Expand Up @@ -621,9 +624,10 @@ def test_ingest_error_searcherr(client, investigation, schemadir, case):
datasets = []
for name in case.data:
datasets.append(client.new("Dataset", name=name))
with pytest.raises(icat.SearchResultError):
with pytest.raises(icat.SearchResultError) as exc:
reader = IngestReader(client, case.metadata, investigation)
reader.ingest(datasets, dry_run=True, update_ds=True)
logger.info("Raised %s: %s", exc.type.__name__, exc.value)


customcases = [
Expand Down Expand Up @@ -731,7 +735,7 @@ def test_ingest_env(monkeypatch, client, investigation, schemadir, case):
reader = IngestReader(client, case.metadata, investigation)
with get_icatdata_schema().open("rb") as f:
schema = etree.XMLSchema(etree.parse(f))
assert schema.validate(reader.infile)
schema.assertValid(reader.infile)
version_elem = reader.infile.xpath("/icatdata/head/apiversion")
assert version_elem
assert version_elem[0].text == str(client.apiversion)
Loading