From 52597d6fda15a24cc0654140c0122b7f45a111c2 Mon Sep 17 00:00:00 2001 From: "Gantner, Florian Klaus" Date: Mon, 18 Nov 2024 13:49:11 +0100 Subject: [PATCH 1/2] fix: serialization of from parameter fixes serialization of from parameter --- invenio_oaiserver/response.py | 2 +- invenio_oaiserver/verbs.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/invenio_oaiserver/response.py b/invenio_oaiserver/response.py index f78eef6..79a9e73 100644 --- a/invenio_oaiserver/response.py +++ b/invenio_oaiserver/response.py @@ -71,7 +71,7 @@ def envelope(**kwargs): e_responseDate.text = datetime_to_datestamp(datetime.utcnow()) e_request = SubElement(e_oaipmh, etree.QName(NS_OAIPMH, "request")) for key, value in kwargs.items(): - if key == "from_" or key == "until": + if key == "from" or key == "until": value = datetime_to_datestamp(value) elif key == "resumptionToken": value = value["token"] diff --git a/invenio_oaiserver/verbs.py b/invenio_oaiserver/verbs.py index ebb3994..a3b8a51 100644 --- a/invenio_oaiserver/verbs.py +++ b/invenio_oaiserver/verbs.py @@ -92,7 +92,7 @@ def validate(self, data, **kwargs): field_names=["verb"], ) - if "from_" in data and "until" in data and data["from_"] > data["until"]: + if "from" in data and "until" in data and data["from"] > data["until"]: raise ValidationError('Date "from" must be before "until".') @@ -119,8 +119,9 @@ class ListIdentifiers(OAISchema): from_ = DateTime( format="permissive", - metadata={"load_from": "from", "data_key": "from", "dump_to": "from"}, + metadata={"data_key": "from"}, data_key="from", + attribute="from", ) until = DateTime(format="permissive") set = fields.Str() From a3e321bd22d328e3532c3a758a66625071f02976 Mon Sep 17 00:00:00 2001 From: "Gantner, Florian Klaus" Date: Tue, 19 Nov 2024 14:02:17 +0100 Subject: [PATCH 2/2] tests: check from data and until date in responses - new test case to check from date after until date - enhance test cases for from and until attribute in responses --- tests/test_verbs.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_verbs.py b/tests/test_verbs.py index 4293683..6d2c766 100644 --- a/tests/test_verbs.py +++ b/tests/test_verbs.py @@ -865,6 +865,12 @@ def test_listrecords(app): == 10 ) + from_ = tree.xpath("/x:OAI-PMH/x:request/@from", namespaces=NAMESPACES) + assert len(from_) == 1 + + until = tree.xpath("/x:OAI-PMH/x:request/@until", namespaces=NAMESPACES) + assert len(until) == 1 + # Check from:until range in resumption token resumption_token = tree.xpath( "/x:OAI-PMH/x:ListRecords/x:resumptionToken", namespaces=NAMESPACES @@ -964,12 +970,36 @@ def test_listidentifiers(app): assert result.status_code == 200 tree = etree.fromstring(result.data) + + from_ = tree.xpath("/x:OAI-PMH/x:request/@from", namespaces=NAMESPACES) + assert len(from_) == 1 + + until = tree.xpath("/x:OAI-PMH/x:request/@until", namespaces=NAMESPACES) + assert len(until) == 1 + identifier = tree.xpath( "/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier", namespaces=NAMESPACES, ) assert len(identifier) == 1 + # Check from:until range with invalid values + with app.test_client() as c: + # Check date and datetime timestamps. from should be before until + for granularity in (False, True): + result = c.get( + "/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc" + "&from={0}&until={1}".format( + datetime_to_datestamp( + record.updated + timedelta(2), day_granularity=granularity + ), + datetime_to_datestamp( + record.updated - timedelta(2), day_granularity=granularity + ), + ) + ) + assert result.status_code == 422 + # check set param with app.test_client() as c: for granularity in (False, True): @@ -1010,6 +1040,13 @@ def test_listidentifiers(app): assert result.status_code == 200 tree = etree.fromstring(result.data) + + from_ = tree.xpath("/x:OAI-PMH/x:request/@from", namespaces=NAMESPACES) + assert len(from_) == 1 + + until = tree.xpath("/x:OAI-PMH/x:request/@until", namespaces=NAMESPACES) + assert len(until) == 1 + identifier = tree.xpath( "/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier", namespaces=NAMESPACES,