Skip to content

Commit d1208f7

Browse files
committed
Extend test_ingest_env in test_06_ingest.py to also add a custom
environment attribute
1 parent b59bc2c commit d1208f7

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

tests/data/ingest-env.xslt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
<apiversion>
1919
<xsl:copy-of select="string(/icatingest/_environment/@icat_version)"/>
2020
</apiversion>
21-
<generator>ingest-env.xslt</generator>
21+
<generator>
22+
<xsl:copy-of select="string(/icatingest/_environment/@generator)"/>
23+
</generator>
2224
</head>
2325
</xsl:template>
2426

tests/test_06_ingest.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,17 @@ def schemadir(monkeypatch):
6969
monkeypatch.setattr(IngestReader, "SchemaDir", testdatadir)
7070

7171

72-
class CapturingIngestReader(IngestReader):
73-
"""Modified version of Ingest reader that captures ingest_data in
74-
add_environment().
72+
class EnvironmentIngestReader(IngestReader):
73+
"""Modified version of IngestReader
74+
- Allow custom environment settings to be included.
75+
- Capture the ingest data after injection of the environment in an
76+
attribute.
7577
"""
78+
_add_env = dict()
79+
def get_environment(self, client):
80+
env = super().get_environment(client)
81+
env.update(self._add_env)
82+
return env
7683
def add_environment(self, client, ingest_data):
7784
super().add_environment(client, ingest_data)
7885
self._ingest_data = ingest_data
@@ -414,7 +421,7 @@ def test_ingest_schema(client, investigation, schemadir, case):
414421
datasets = []
415422
for name in case.data:
416423
datasets.append(client.new("Dataset", name=name))
417-
reader = CapturingIngestReader(client, case.metadata, investigation)
424+
reader = EnvironmentIngestReader(client, case.metadata, investigation)
418425
print_xml(reader._ingest_data)
419426
print_xml(reader.infile)
420427
with get_icatdata_schema().open("rb") as f:
@@ -839,22 +846,30 @@ def test_custom_ingest(client, investigation, samples, schemadir, case):
839846
def test_ingest_env(monkeypatch, client, investigation, schemadir, case):
840847
"""Test using the _environment element.
841848
842-
Applying a custom XSLT that extracts an attribute from the
843-
_environment element that is injected by IngestReader into the
844-
input data and puts that values into the head element of the
845-
transformed input. This is to test that adding the _environment
846-
element works and it is in principle possible to make use of the
847-
values in the XSLT.
849+
Add a custom attribute to the _environment that is injected by
850+
IngestReader into the input data. Apply a custom XSLT that
851+
extracts attributes from the _environment element and puts the
852+
values into the head element of the transformed input. This is to
853+
test that adding the _environment element works and it is in
854+
principle possible to make use of the values in the XSLT.
848855
"""
849-
monkeypatch.setattr(IngestReader,
856+
generator = "test_ingest_env (python-icat %s)" % icat.__version__
857+
monkeypatch.setattr(EnvironmentIngestReader,
858+
"_add_env", dict(generator=generator))
859+
monkeypatch.setattr(EnvironmentIngestReader,
850860
"XSLT_Map", dict(icatingest="ingest-env.xslt"))
851861
datasets = []
852862
for name in case.data:
853863
datasets.append(client.new("Dataset", name=name))
854-
reader = IngestReader(client, case.metadata, investigation)
864+
reader = EnvironmentIngestReader(client, case.metadata, investigation)
865+
print_xml(reader._ingest_data)
866+
print_xml(reader.infile)
855867
with get_icatdata_schema().open("rb") as f:
856868
schema = etree.XMLSchema(etree.parse(f))
857869
schema.assertValid(reader.infile)
858870
version_elem = reader.infile.xpath("/icatdata/head/apiversion")
859871
assert version_elem
860872
assert version_elem[0].text == str(client.apiversion)
873+
generator_elem = reader.infile.xpath("/icatdata/head/generator")
874+
assert generator_elem
875+
assert generator_elem[0].text == generator

0 commit comments

Comments
 (0)