From 0f2ee5ec4c114c56a159e20a25bc804e305dd96b Mon Sep 17 00:00:00 2001 From: Rolf Krahl Date: Wed, 20 Mar 2024 17:16:55 +0100 Subject: [PATCH 1/3] Log (expected) exceptions test_06_ingest.py --- tests/test_06_ingest.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_06_ingest.py b/tests/test_06_ingest.py index e0456d6..1d28f30 100644 --- a/tests/test_06_ingest.py +++ b/tests/test_06_ingest.py @@ -4,6 +4,7 @@ from collections import namedtuple import datetime import io +import logging import pytest pytest.importorskip("lxml") from lxml import etree @@ -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={ @@ -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(""" @@ -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 = [ From 3a4adb180db6e42366dc7041632dc8ead8ece865 Mon Sep 17 00:00:00 2001 From: Rolf Krahl Date: Wed, 20 Mar 2024 17:47:25 +0100 Subject: [PATCH 2/3] Call XMLSchema().assertValid() rather than XMLSchema().validate() which raises a more meaningful exception --- src/icat/ingest.py | 6 ++++-- tests/test_06_ingest.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/icat/ingest.py b/src/icat/ingest.py index 2540535..0e51352 100644 --- a/src/icat/ingest.py +++ b/src/icat/ingest.py @@ -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)) diff --git a/tests/test_06_ingest.py b/tests/test_06_ingest.py index 1d28f30..baa0c73 100644 --- a/tests/test_06_ingest.py +++ b/tests/test_06_ingest.py @@ -357,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 @@ -735,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) From 6393affbd2ffe749496d3773aa229d9c6d78969b Mon Sep 17 00:00:00 2001 From: Rolf Krahl Date: Wed, 20 Mar 2024 18:02:15 +0100 Subject: [PATCH 3/3] Update changelog --- CHANGES.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index cc44a85..9685c9c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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. @@ -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: