Skip to content

Commit

Permalink
Merge pull request #249 from metno/issue236_update
Browse files Browse the repository at this point in the history
Issue236 update
  • Loading branch information
mortenwh authored Nov 13, 2024
2 parents 05079f7 + b5e6955 commit 8b47573
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
22 changes: 7 additions & 15 deletions dmci/distributors/pycsw_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<?xml version="1.0" encoding="UTF-8"?>'
b'<csw:Transaction xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" '
b'xmlns:ows="http://www.opengis.net/ows" '
b'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
b'xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 '
b'http://schemas.opengis.net/csw/2.0.2/CSW-publication.xsd" '
b'service="CSW" version="2.0.2"><csw:Update>'
)
xml += self._translate()
xml += b"</csw:Update></csw:Transaction>"
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."""
Expand Down
21 changes: 20 additions & 1 deletion tests/test_dist/test_pycsw_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 8b47573

Please sign in to comment.