From dcf22519cebcf7e08448dc04fea7c111e5c46e79 Mon Sep 17 00:00:00 2001 From: Shwetansh Singh Date: Wed, 28 Oct 2026 18:39:28 +0000 Subject: [PATCH 1/5] test: add unit tests for get_databus_id_parts_from_file_url --- tests/test_api_utils.py | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/test_api_utils.py diff --git a/tests/test_api_utils.py b/tests/test_api_utils.py new file mode 100644 index 0000000..847ee66 --- /dev/null +++ b/tests/test_api_utils.py @@ -0,0 +1,81 @@ +from databusclient.api.utils import get_databus_id_parts_from_file_url + + +def test_split_full_url(): + result = get_databus_id_parts_from_file_url( + "https://databus.dbpedia.org/dbpedia/mappings/geo-coordinates/2022.12.01/file.ttl" + ) + assert result == ( + "databus.dbpedia.org", + "dbpedia", + "mappings", + "geo-coordinates", + "2022.12.01", + "file.ttl", + + ) + +def test_split_short_url_fills_none(): + result = get_databus_id_parts_from_file_url( + "https://databus.dbpedia.org/dbpedia/mappings" + ) + assert result == ( + "databus.dbpedia.org", + "dbpedia", + "mappings", + None, + None, + None, + ) + +def test_split_http_same_as_https(): + result = get_databus_id_parts_from_file_url( + "http://databus.dbpedia.org/dbpedia/mappings/geo-coordinates/2022.12.01/file.ttl" + ) + assert result == ( + "databus.dbpedia.org", + "dbpedia", + "mappings", + "geo-coordinates", + "2022.12.01", + "file.ttl", + ) + +def test_split_trailing_slash_ignored(): + result = get_databus_id_parts_from_file_url( + "https://databus.dbpedia.org/dbpedia/mappings/geo-coordinates/2022.12.01/file.ttl/" + ) + assert result == ( + "databus.dbpedia.org", + "dbpedia", + "mappings", + "geo-coordinates", + "2022.12.01", + "file.ttl", + ) + +def test_split_empty_string(): + result = get_databus_id_parts_from_file_url( + "" + ) + assert result == ( + "", + None, + None, + None, + None, + None, + ) + +def test_split_too_long_keeps_first_six(): + result = get_databus_id_parts_from_file_url( + "https://databus.dbpedia.org/a/b/c/d/e/f/g/h" + ) + assert result == ( + "databus.dbpedia.org", + "a", + "b", + "c", + "d", + "e", + ) \ No newline at end of file From 6b3d78c26142e99ad12490901a9199feebec0a54 Mon Sep 17 00:00:00 2001 From: Shwetansh Singh Date: Wed, 28 Oct 2026 18:53:59 +0000 Subject: [PATCH 2/5] test: add unit tests for compute_sha256_and_length --- tests/test_api_utils.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/test_api_utils.py b/tests/test_api_utils.py index 847ee66..a5b87bb 100644 --- a/tests/test_api_utils.py +++ b/tests/test_api_utils.py @@ -1,4 +1,5 @@ -from databusclient.api.utils import get_databus_id_parts_from_file_url +import pytest +from databusclient.api.utils import get_databus_id_parts_from_file_url, compute_sha256_and_length def test_split_full_url(): @@ -78,4 +79,28 @@ def test_split_too_long_keeps_first_six(): "c", "d", "e", - ) \ No newline at end of file + ) + +def test_sha256_hello_file(tmp_path): + f = tmp_path / "hello.txt" + f.write_text("hello") + result = compute_sha256_and_length(f) + + assert result == ( + "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", + 5, + ) + +def test_sha256_empty_file_(tmp_path): + f = tmp_path / "empty.txt" + f.write_text("") + result = compute_sha256_and_length(f) + + assert result == ( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",0 + ) + +def test_sha256_missing_file_raises(tmp_path): + missing = tmp_path / "does_not_exist.txt" + with pytest.raises(FileNotFoundError): + compute_sha256_and_length(missing) \ No newline at end of file From 02992f23919844f780f90ffc8dd1b1a070dd9903 Mon Sep 17 00:00:00 2001 From: Shwetansh Singh Date: Wed, 28 Oct 2026 20:32:12 +0000 Subject: [PATCH 3/5] test: add unit tests for fetch_databus_jsonld --- tests/test_api_utils.py | 64 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/tests/test_api_utils.py b/tests/test_api_utils.py index a5b87bb..734a674 100644 --- a/tests/test_api_utils.py +++ b/tests/test_api_utils.py @@ -1,5 +1,7 @@ import pytest -from databusclient.api.utils import get_databus_id_parts_from_file_url, compute_sha256_and_length +import requests +from databusclient.api import utils +from databusclient.api.utils import get_databus_id_parts_from_file_url, compute_sha256_and_length, fetch_databus_jsonld def test_split_full_url(): @@ -91,7 +93,7 @@ def test_sha256_hello_file(tmp_path): 5, ) -def test_sha256_empty_file_(tmp_path): +def test_sha256_empty_file(tmp_path): f = tmp_path / "empty.txt" f.write_text("") result = compute_sha256_and_length(f) @@ -103,4 +105,60 @@ def test_sha256_empty_file_(tmp_path): def test_sha256_missing_file_raises(tmp_path): missing = tmp_path / "does_not_exist.txt" with pytest.raises(FileNotFoundError): - compute_sha256_and_length(missing) \ No newline at end of file + compute_sha256_and_length(missing) + +class FakeResponse: + def __init__(self,text="", ok=True): + self.text = text + self.ok = ok + + def raise_for_status(self): + if not self.ok: + raise requests.HTTPError("404 Not Found") + + +def test_fetch_sends_accept_header_without_key(monkeypatch): + calls = {} + + def fake_get(uri, headers, timeout): + calls["uri"] = uri + calls["headers"] = headers + return FakeResponse(text='{"ok": true}') + + monkeypatch.setattr(utils.requests, "get", fake_get) + + result = fetch_databus_jsonld("https://databus.dbpedia.org/dbpedia/mappings") + + assert result == '{"ok": true}' + assert calls["headers"] == {"Accept": "application/ld+json"} + + +def test_fetch_adds_api_key_header(monkeypatch): + calls = {} + + def fake_get(uri, headers, timeout): + calls["uri"] = uri + calls["headers"] = headers + return FakeResponse(text='{"ok": true}') + + monkeypatch.setattr(utils.requests, "get", fake_get) + + result = fetch_databus_jsonld("https://databus.dbpedia.org/dbpedia/mappings", databus_key="secret123") + + assert result == '{"ok": true}' + assert calls["headers"] == {"Accept": "application/ld+json","X-API-KEY": "secret123",} + +def test_fetch_raises_on_server_error(monkeypatch): + + def fake_get(uri, headers, timeout): + + return FakeResponse(ok=False) + + monkeypatch.setattr(utils.requests, "get", fake_get) + + with pytest.raises(requests.HTTPError): + fetch_databus_jsonld("https://databus.dbpedia.org/dbpedia/mappings") + + + + From 204d20b2501c71b6c7f2a9e75e3ab83621ac83c5 Mon Sep 17 00:00:00 2001 From: Shwetansh Singh Date: Wed, 28 Oct 2026 20:35:45 +0000 Subject: [PATCH 4/5] docs: remove duplicate docstring in get_databus_id_parts_from_file_url --- databusclient/api/utils.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/databusclient/api/utils.py b/databusclient/api/utils.py index df4f49d..ba6dfe5 100644 --- a/databusclient/api/utils.py +++ b/databusclient/api/utils.py @@ -28,12 +28,7 @@ def get_databus_id_parts_from_file_url( A tuple containing (host, accountId, groupId, artifactId, versionId, fileId). Each element is a string or None if not present. """ - """Split a Databus URI into its six parts. - - The returned tuple is (host, accountId, groupId, artifactId, versionId, fileId). - Missing parts are returned as ``None``. - """ - + uri = uri.removeprefix("https://").removeprefix("http://") parts = uri.strip("/").split("/") parts += [None] * (6 - len(parts)) # pad with None if less than 6 parts From 0701e66ff12ccb80cceb09ef3c6b91d7b8ec5333 Mon Sep 17 00:00:00 2001 From: Shwetansh Singh Date: Wed, 28 Oct 2026 20:47:42 +0000 Subject: [PATCH 5/5] style: format test_api_utils.py with ruff --- tests/test_api_utils.py | 49 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/tests/test_api_utils.py b/tests/test_api_utils.py index 734a674..fe9ec05 100644 --- a/tests/test_api_utils.py +++ b/tests/test_api_utils.py @@ -1,7 +1,11 @@ import pytest -import requests -from databusclient.api import utils -from databusclient.api.utils import get_databus_id_parts_from_file_url, compute_sha256_and_length, fetch_databus_jsonld +import requests +from databusclient.api import utils +from databusclient.api.utils import ( + get_databus_id_parts_from_file_url, + compute_sha256_and_length, + fetch_databus_jsonld, +) def test_split_full_url(): @@ -15,9 +19,9 @@ def test_split_full_url(): "geo-coordinates", "2022.12.01", "file.ttl", - ) + def test_split_short_url_fills_none(): result = get_databus_id_parts_from_file_url( "https://databus.dbpedia.org/dbpedia/mappings" @@ -31,6 +35,7 @@ def test_split_short_url_fills_none(): None, ) + def test_split_http_same_as_https(): result = get_databus_id_parts_from_file_url( "http://databus.dbpedia.org/dbpedia/mappings/geo-coordinates/2022.12.01/file.ttl" @@ -44,6 +49,7 @@ def test_split_http_same_as_https(): "file.ttl", ) + def test_split_trailing_slash_ignored(): result = get_databus_id_parts_from_file_url( "https://databus.dbpedia.org/dbpedia/mappings/geo-coordinates/2022.12.01/file.ttl/" @@ -57,10 +63,9 @@ def test_split_trailing_slash_ignored(): "file.ttl", ) + def test_split_empty_string(): - result = get_databus_id_parts_from_file_url( - "" - ) + result = get_databus_id_parts_from_file_url("") assert result == ( "", None, @@ -70,6 +75,7 @@ def test_split_empty_string(): None, ) + def test_split_too_long_keeps_first_six(): result = get_databus_id_parts_from_file_url( "https://databus.dbpedia.org/a/b/c/d/e/f/g/h" @@ -83,6 +89,7 @@ def test_split_too_long_keeps_first_six(): "e", ) + def test_sha256_hello_file(tmp_path): f = tmp_path / "hello.txt" f.write_text("hello") @@ -93,29 +100,33 @@ def test_sha256_hello_file(tmp_path): 5, ) + def test_sha256_empty_file(tmp_path): f = tmp_path / "empty.txt" f.write_text("") result = compute_sha256_and_length(f) assert result == ( - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",0 + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + 0, ) + def test_sha256_missing_file_raises(tmp_path): missing = tmp_path / "does_not_exist.txt" with pytest.raises(FileNotFoundError): compute_sha256_and_length(missing) + class FakeResponse: - def __init__(self,text="", ok=True): + def __init__(self, text="", ok=True): self.text = text - self.ok = ok + self.ok = ok def raise_for_status(self): if not self.ok: raise requests.HTTPError("404 Not Found") - + def test_fetch_sends_accept_header_without_key(monkeypatch): calls = {} @@ -143,22 +154,22 @@ def fake_get(uri, headers, timeout): monkeypatch.setattr(utils.requests, "get", fake_get) - result = fetch_databus_jsonld("https://databus.dbpedia.org/dbpedia/mappings", databus_key="secret123") + result = fetch_databus_jsonld( + "https://databus.dbpedia.org/dbpedia/mappings", databus_key="secret123" + ) assert result == '{"ok": true}' - assert calls["headers"] == {"Accept": "application/ld+json","X-API-KEY": "secret123",} + assert calls["headers"] == { + "Accept": "application/ld+json", + "X-API-KEY": "secret123", + } -def test_fetch_raises_on_server_error(monkeypatch): +def test_fetch_raises_on_server_error(monkeypatch): def fake_get(uri, headers, timeout): - return FakeResponse(ok=False) monkeypatch.setattr(utils.requests, "get", fake_get) with pytest.raises(requests.HTTPError): fetch_databus_jsonld("https://databus.dbpedia.org/dbpedia/mappings") - - - -