Skip to content

Commit 27ef917

Browse files
committed
Merge branch 'xml-validate' into develop
2 parents bb57b48 + 6393aff commit 27ef917

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Incompatible changes
3232
Bug fixes and minor changes
3333
---------------------------
3434

35+
+ `#151`_: Provide a more meaningful error message if the input to
36+
:class:`icat.ingest.IngestReader` fails validation against the XML
37+
Schema Definition.
38+
3539
+ `#141`_, `#142`_, `#150`_: Review documentation.
3640

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

5055

5156
.. _changes-1_2_0:

src/icat/ingest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ def __init__(self, client, metadata, investigation):
112112
raise InvalidIngestFileError(e)
113113
with self.get_xsd(ingest_data).open("rb") as f:
114114
schema = etree.XMLSchema(etree.parse(f))
115-
if not schema.validate(ingest_data):
116-
raise InvalidIngestFileError("validation failed")
115+
try:
116+
schema.assertValid(ingest_data)
117+
except etree.DocumentInvalid as exc:
118+
raise InvalidIngestFileError("DocumentInvalid: %s" % exc)
117119
self.add_environment(client, ingest_data)
118120
with self.get_xslt(ingest_data).open("rb") as f:
119121
xslt = etree.XSLT(etree.parse(f))

tests/test_06_ingest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections import namedtuple
55
import datetime
66
import io
7+
import logging
78
import pytest
89
pytest.importorskip("lxml")
910
from lxml import etree
@@ -14,6 +15,7 @@
1415
from conftest import (getConfig, gettestdata, icat_version,
1516
get_icatdata_schema, testdatadir)
1617

18+
logger = logging.getLogger(__name__)
1719

1820
def get_test_investigation(client):
1921
query = Query(client, "Investigation", conditions={
@@ -355,7 +357,7 @@ def test_ingest_schema(client, investigation, schemadir, case):
355357
reader = IngestReader(client, case.metadata, investigation)
356358
with get_icatdata_schema().open("rb") as f:
357359
schema = etree.XMLSchema(etree.parse(f))
358-
assert schema.validate(reader.infile)
360+
schema.assertValid(reader.infile)
359361

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

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

628632

629633
customcases = [
@@ -731,7 +735,7 @@ def test_ingest_env(monkeypatch, client, investigation, schemadir, case):
731735
reader = IngestReader(client, case.metadata, investigation)
732736
with get_icatdata_schema().open("rb") as f:
733737
schema = etree.XMLSchema(etree.parse(f))
734-
assert schema.validate(reader.infile)
738+
schema.assertValid(reader.infile)
735739
version_elem = reader.infile.xpath("/icatdata/head/apiversion")
736740
assert version_elem
737741
assert version_elem[0].text == str(client.apiversion)

0 commit comments

Comments
 (0)