From 938c0830e544e0d2a322109dd167832a763d0ab4 Mon Sep 17 00:00:00 2001 From: Partha Shankar <152790727+Partha-Shankar@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:06:27 +0530 Subject: [PATCH] test: add offline tests for the JSON-LD parsers in download.py Cover _get_file_download_urls_from_artifact_jsonld and _get_databus_artifacts_of_group: Part-only file extraction, skipped malformed nodes, single vs list artifacts, version URIs dropped, and the ValueError for an unexpected type. --- tests/test_download_jsonld_parsers.py | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/test_download_jsonld_parsers.py diff --git a/tests/test_download_jsonld_parsers.py b/tests/test_download_jsonld_parsers.py new file mode 100644 index 0000000..3607088 --- /dev/null +++ b/tests/test_download_jsonld_parsers.py @@ -0,0 +1,89 @@ +"""Offline tests for the JSON-LD parsers in databusclient.api.download.""" + +import json + +import pytest + +import databusclient.api.download as dl + +BASE = "https://databus.dbpedia.org/acc/grp" + + +def test_file_urls_come_from_part_nodes_only(): + jsonld = json.dumps( + { + "@graph": [ + {"@type": "Version", "@id": f"{BASE}/art/2024.01.01"}, + {"@type": "Part", "file": f"{BASE}/art/2024.01.01/a.ttl"}, + {"@type": "Dataset", "file": f"{BASE}/art/not-a-part.ttl"}, + {"@type": "Part", "file": f"{BASE}/art/2024.01.01/b.nt.gz"}, + ] + } + ) + + assert dl._get_file_download_urls_from_artifact_jsonld(jsonld) == [ + f"{BASE}/art/2024.01.01/a.ttl", + f"{BASE}/art/2024.01.01/b.nt.gz", + ] + + +def test_file_urls_skip_parts_without_a_string_file(): + jsonld = json.dumps( + { + "@graph": [ + {"@type": "Part"}, + {"@type": "Part", "file": {"@id": f"{BASE}/art/v1/as-object.ttl"}}, + {"@type": "Part", "file": f"{BASE}/art/v1/ok.ttl"}, + ] + } + ) + + assert dl._get_file_download_urls_from_artifact_jsonld(jsonld) == [ + f"{BASE}/art/v1/ok.ttl" + ] + + +def test_file_urls_without_graph_are_empty(): + assert dl._get_file_download_urls_from_artifact_jsonld("{}") == [] + + +def test_group_artifacts_keep_artifact_uris_and_drop_version_uris(): + jsonld = json.dumps( + { + "databus:hasArtifact": [ + {"@id": f"{BASE}/art-a"}, + {"@id": f"{BASE}/art-a/2024.01.01"}, + {"@id": f"{BASE}/art-b"}, + ] + } + ) + + assert dl._get_databus_artifacts_of_group(jsonld) == [ + f"{BASE}/art-a", + f"{BASE}/art-b", + ] + + +def test_group_artifacts_accept_a_single_artifact_object(): + jsonld = json.dumps({"databus:hasArtifact": {"@id": f"{BASE}/art"}}) + + assert dl._get_databus_artifacts_of_group(jsonld) == [f"{BASE}/art"] + + +def test_group_artifacts_skip_entries_without_an_id(): + jsonld = json.dumps( + {"databus:hasArtifact": [f"{BASE}/as-string", {"label": "no id"}, {"@id": ""}]} + ) + + assert dl._get_databus_artifacts_of_group(jsonld) == [] + + +def test_group_without_artifacts_is_empty(): + assert dl._get_databus_artifacts_of_group("{}") == [] + + +def test_group_artifacts_reject_an_unexpected_type(): + jsonld = json.dumps({"databus:hasArtifact": "not-a-list"}) + + with pytest.raises(ValueError, match="databus:hasArtifact"): + dl._get_databus_artifacts_of_group(jsonld)