diff --git a/dmci/distributors/pycsw_dist.py b/dmci/distributors/pycsw_dist.py index ff7f671..345ca47 100644 --- a/dmci/distributors/pycsw_dist.py +++ b/dmci/distributors/pycsw_dist.py @@ -103,21 +103,13 @@ def _update(self): properties against a csw:Constraint, to update: Define overwriting property, search for places to overwrite. """ - headers = requests.structures.CaseInsensitiveDict() - headers["Content-Type"] = "application/xml" - headers["Accept"] = "application/xml" - xml = ( - b'' - b'' - ) - xml += self._translate() - xml += b"" - return self._post_request(headers, xml, "update", self.TOTAL_UPDATED) + del_status, del_response_text = self._delete() + if not del_status: + return del_status, del_response_text.replace("delete", "update") + ins_status, ins_response_text = self._insert() + response_text = ins_response_text.replace("insert", "update") + # Handle insertion + return ins_status, response_text def _delete(self): """Delete entry with a specified metadata_id.""" diff --git a/tests/test_dist/test_pycsw_dist.py b/tests/test_dist/test_pycsw_dist.py index 63172cb..bc2ee44 100644 --- a/tests/test_dist/test_pycsw_dist.py +++ b/tests/test_dist/test_pycsw_dist.py @@ -20,9 +20,10 @@ import os import pytest import requests -from tools import causeException from lxml import etree +from unittest import mock +from tools import causeException from dmci.api.worker import Worker from dmci.distributors.pycsw_dist import PyCSWDist @@ -106,6 +107,7 @@ def testDistPyCSW_Update(monkeypatch, mockXml, mockXslt, tmpUUID, tmpConf): tstWorker = Worker("update", None, None) tstWorker._file_metadata_id = tmpUUID + tstWorker._namespace = "no.test" tstWorker._conf = tmpConf # Update returns True @@ -136,15 +138,32 @@ def testDistPyCSW_Update(monkeypatch, mockXml, mockXslt, tmpUUID, tmpConf): # Update returns False if the http post request fails with monkeypatch.context() as mp: + # Delete within update fails mp.setattr( "dmci.distributors.pycsw_dist.requests.post", causeException) tstPyCSW = PyCSWDist("update", xml_file=mockXml) + tstPyCSW._worker = tstWorker tstPyCSW._conf = tmpConf assert tstPyCSW.run() == ( False, "http://localhost: service unavailable. Failed to update." ) + # Insert within update fails + def new_delete(cls, *a, **k): + return True, "" + + with mock.patch.object(PyCSWDist, '_delete', new=new_delete): + mp.setattr( + "dmci.distributors.pycsw_dist.requests.post", causeException) + tstPyCSW = PyCSWDist("update", xml_file=mockXml) + tstPyCSW._worker = tstWorker + tstPyCSW._conf = tmpConf + assert tstPyCSW.run() == ( + False, + "http://localhost: service unavailable. Failed to update." + ) + # END Test testDistPyCSW_Update