Skip to content

Commit

Permalink
Merge pull request #221 from metno/verify_whether_new_changes_made_to…
Browse files Browse the repository at this point in the history
…_handle_the_exceptions_in_distribution_works_as_expected

Handle exception in update_parent in solr
  • Loading branch information
shamlymajeed authored Feb 29, 2024
2 parents 222655a + 53ffa55 commit 09e373c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dmci/distributors/solr_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ def _add(self):
logger.debug("Child dataset's parent's id is: %s",
newdoc['related_dataset'])
parentid = newdoc['related_dataset_id']
status, msg = self.mysolr.update_parent(
parentid,
fail_on_missing=self._conf.fail_on_missing_parent
)
try:
status, msg = self.mysolr.update_parent(
parentid,
fail_on_missing=self._conf.fail_on_missing_parent
)
except Exception as e:
msg = "Failed to update parent in SolR.Reason: %s" % str(e)
logger.error(msg)
return False, msg

if status:
status, msg = self._index_record(
newdoc, add_thumbnail=False, level=2)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_dist/test_solr_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,35 @@ def testDistSolR_AddTosolrRaisesException(mockXml, monkeypatch):
)


@pytest.mark.dist
def testDistSolR_AddUpdateParentRaisesException(mockXml, monkeypatch):
""" Test that the _add function fails correctly when
MMD4SolR.tosolr raises an exception.
"""
with monkeypatch.context() as mp:

mp.setattr("dmci.distributors.solr_dist.MMD4SolR",
lambda *args, **kwargs: MockMMD4SolR(*args, **kwargs))
mp.setattr("dmci.distributors.solr_dist.IndexMMD",
lambda *args, **kwargs: MockIndexMMD(*args, **kwargs))
mp.setattr(MockMMD4SolR, "tosolr",
lambda *a, **k: {
"doc": None,
"id": "no-met-dev-250ba38f-1081-4669-a429-f378c569db32",
"metadata_identifier": "no.met.dev:250ba38f-1081-4669-a429-f378c569db32",
"related_dataset": "no.met.dev:350ba38f-1081-4669-a429-f378c569db32",
"related_dataset_id": "no-met-dev-350ba38f-1081-4669-a429-f378c569db32",
})

mp.setattr(MockIndexMMD, "get_dataset", lambda *a, **k: {'doc': None})

mp.setattr(MockIndexMMD, "update_parent", causeException)
tstDist = SolRDist("insert", xml_file=mockXml)
assert tstDist._add() == (
False, "Failed to update parent in SolR.Reason: Test Exception"
)


@pytest.mark.dist
def testDistSolR_AddDocExists(mockXml, monkeypatch):
""" Test that an the _add function fails correctly when the
Expand Down

0 comments on commit 09e373c

Please sign in to comment.