From 995bda9330a8529b24b9ad208180a9d0fd2a6351 Mon Sep 17 00:00:00 2001 From: sutharshikhar-gif Date: Sat, 21 Nov 2026 23:56:10 +0530 Subject: [PATCH] fix: handle single-artifact group deletion --- databusclient/api/delete.py | 17 +++++- tests/test_delete_group.py | 110 ++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 tests/test_delete_group.py diff --git a/databusclient/api/delete.py b/databusclient/api/delete.py index 199e5a4..d631f42 100644 --- a/databusclient/api/delete.py +++ b/databusclient/api/delete.py @@ -265,12 +265,25 @@ def _delete_group( group_body = fetch_databus_jsonld(databusURI, databus_key) json_dict = json.loads(group_body) - artifacts = json_dict.get("databus:hasArtifact", []) + artifacts = json_dict.get("databus:hasArtifact") + + # The Databus returns a dict when a group has a single artifact and a + # list when it has multiple; normalise to a list (see _delete_artifact + # and _get_databus_artifacts_of_group for the same convention). + if isinstance(artifacts, dict): + artifacts = [artifacts] + elif isinstance(artifacts, list): + pass + else: + # Missing key, None, or any unexpected type: treat as no artifacts. + artifacts = [] artifact_uris = [] for item in artifacts: + if not isinstance(item, dict): + continue uri = item.get("@id") - if not uri: + if not isinstance(uri, str) or not uri: continue _, _, _, _, version, _ = get_databus_id_parts_from_file_url(uri) if version is None: diff --git a/tests/test_delete_group.py b/tests/test_delete_group.py new file mode 100644 index 0000000..0db8010 --- /dev/null +++ b/tests/test_delete_group.py @@ -0,0 +1,110 @@ +import json + +import pytest + +from databusclient.api import delete as delmod + + +def _group_json(artifacts): + return json.dumps({"databus:hasArtifact": artifacts}) + + +@pytest.fixture(autouse=True) +def _mock_helpers(monkeypatch): + monkeypatch.setattr(delmod, "fetch_databus_jsonld", lambda *a, **k: "") + monkeypatch.setattr(delmod, "_delete_artifact", lambda *a, **k: None) + monkeypatch.setattr(delmod, "_delete_resource", lambda *a, **k: None) + + +def _run(artifacts_payload, monkeypatch): + monkeypatch.setattr( + delmod, "fetch_databus_jsonld", lambda *a, **k: _group_json(artifacts_payload) + ) + captured = {"artifacts": []} + + def fake_delete_artifact(uri, *a, **k): + captured["artifacts"].append(uri) + + monkeypatch.setattr(delmod, "_delete_artifact", fake_delete_artifact) + delmod._delete_group("https://example.org/g", "key", dry_run=True) + return captured + + +def test_single_artifact_dict(monkeypatch): + payload = {"@id": "https://example.org/g/a"} + captured = _run(payload, monkeypatch) + assert captured["artifacts"] == ["https://example.org/g/a"] + + +def test_multiple_artifact_list(monkeypatch): + payload = [ + {"@id": "https://example.org/g/a1"}, + {"@id": "https://example.org/g/a2"}, + ] + captured = _run(payload, monkeypatch) + assert captured["artifacts"] == [ + "https://example.org/g/a1", + "https://example.org/g/a2", + ] + + +def test_missing_key(monkeypatch): + monkeypatch.setattr( + delmod, "fetch_databus_jsonld", lambda *a, **k: json.dumps({}) + ) + called = {"n": 0} + + def fake_delete_artifact(*a, **k): + called["n"] += 1 + + monkeypatch.setattr(delmod, "_delete_artifact", fake_delete_artifact) + delmod._delete_group("https://example.org/g", "key", dry_run=True) + assert called["n"] == 0 + + +def test_none_value(monkeypatch): + monkeypatch.setattr( + delmod, "fetch_databus_jsonld", lambda *a, **k: json.dumps({"databus:hasArtifact": None}) + ) + called = {"n": 0} + + def fake_delete_artifact(*a, **k): + called["n"] += 1 + + monkeypatch.setattr(delmod, "_delete_artifact", fake_delete_artifact) + delmod._delete_group("https://example.org/g", "key", dry_run=True) + assert called["n"] == 0 + + +def test_unexpected_type(monkeypatch): + monkeypatch.setattr( + delmod, "fetch_databus_jsonld", lambda *a, **k: json.dumps({"databus:hasArtifact": "not-a-list"}) + ) + called = {"n": 0} + + def fake_delete_artifact(*a, **k): + called["n"] += 1 + + monkeypatch.setattr(delmod, "_delete_artifact", fake_delete_artifact) + delmod._delete_group("https://example.org/g", "key", dry_run=True) + assert called["n"] == 0 + + +def test_non_dict_and_missing_id_skipped(monkeypatch): + payload = [ + "not-a-dict", + {"foo": "bar"}, # no @id + {"@id": "https://example.org/g/a"}, + ] + captured = _run(payload, monkeypatch) + assert captured["artifacts"] == ["https://example.org/g/a"] + + +def test_non_string_id_skipped(monkeypatch): + payload = [ + {"@id": 123}, + {"@id": {"x": 1}}, + {"@id": "https://example.org/g/a"}, + ] + captured = _run(payload, monkeypatch) + assert captured["artifacts"] == ["https://example.org/g/a"]