Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions databusclient/api/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
110 changes: 110 additions & 0 deletions tests/test_delete_group.py
Original file line number Diff line number Diff line change
@@ -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"]