From ba79f69e2922e68b1d1361f8bb84668468e6e5d3 Mon Sep 17 00:00:00 2001 From: Tim Pellissier Date: Sat, 18 Oct 2025 00:15:08 -0700 Subject: [PATCH 1/2] fix unit tests after optionset / logicalname changes --- tests/test_create_single_guid.py | 21 ++++++++++++++++----- tests/test_logical_crud.py | 15 +++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tests/test_create_single_guid.py b/tests/test_create_single_guid.py index b9a24704..6ed6166c 100644 --- a/tests/test_create_single_guid.py +++ b/tests/test_create_single_guid.py @@ -10,13 +10,17 @@ class DummyHTTP: def __init__(self, headers): self._headers = headers def request(self, method, url, **kwargs): - # Simulate minimal Response-like object + # Simulate minimal Response-like object (subset of requests.Response API used by code) resp = types.SimpleNamespace() resp.headers = self._headers resp.status_code = 204 + resp.text = "" def raise_for_status(): return None + def json_func(): + return {} resp.raise_for_status = raise_for_status + resp.json = json_func return resp class TestableOData(ODataClient): @@ -24,23 +28,30 @@ def __init__(self, headers): super().__init__(DummyAuth(), "https://org.example", None) # Monkey-patch http client self._http = types.SimpleNamespace(request=lambda method, url, **kwargs: DummyHTTP(headers).request(method, url, **kwargs)) + # Bypass optionset label conversion to keep response sequence stable for tests + def _convert_labels_to_ints(self, logical_name, record): # pragma: no cover - test shim + return record def test__create_single_uses_odata_entityid(): guid = "11111111-2222-3333-4444-555555555555" headers = {"OData-EntityId": f"https://org.example/api/data/v9.2/accounts({guid})"} c = TestableOData(headers) - result = c._create_single("accounts", {"name": "x"}) + # Current signature requires logical name explicitly + result = c._create_single("accounts", "account", {"name": "x"}) assert result == guid def test__create_single_fallback_location(): guid = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" headers = {"Location": f"https://org.example/api/data/v9.2/contacts({guid})"} c = TestableOData(headers) - result = c._create_single("contacts", {"firstname": "x"}) + result = c._create_single("contacts", "contact", {"firstname": "x"}) assert result == guid def test__create_single_missing_headers_raises(): c = TestableOData({}) import pytest - with pytest.raises(RuntimeError): - c._create_single("accounts", {"name": "x"}) + from dataverse_sdk.errors import MetadataError + from dataverse_sdk.error_codes import METADATA_CREATE_GUID_MISSING + with pytest.raises(RuntimeError) as ei: + c._create_single("accounts", "account", {"name": "x"}) + assert ei.value.subcode == METADATA_CREATE_GUID_MISSING diff --git a/tests/test_logical_crud.py b/tests/test_logical_crud.py index e11cd0b2..8b552fee 100644 --- a/tests/test_logical_crud.py +++ b/tests/test_logical_crud.py @@ -13,27 +13,20 @@ def __init__(self, responses): self.calls = [] def request(self, method, url, **kwargs): self.calls.append((method, url, kwargs)) - # Pop next prepared response tuple if not self._responses: raise AssertionError("No more dummy responses configured") status, headers, body = self._responses.pop(0) resp = types.SimpleNamespace() resp.status_code = status resp.headers = headers - resp.text = body if isinstance(body, str) else (body and "{}") + resp.text = "" if body is None else ("{}" if isinstance(body, dict) else str(body)) def raise_for_status(): if status >= 400: raise RuntimeError(f"HTTP {status}") return None - resp.raise_for_status = raise_for_status def json_func(): - import json as _json - if isinstance(body, dict): - return body - try: - return _json.loads(body) if isinstance(body, str) else {} - except Exception: - return {} + return body if isinstance(body, dict) else {} + resp.raise_for_status = raise_for_status resp.json = json_func return resp @@ -41,6 +34,8 @@ class TestableClient(ODataClient): def __init__(self, responses): super().__init__(DummyAuth(), "https://org.example", None) self._http = DummyHTTPClient(responses) + def _convert_labels_to_ints(self, logical_name, record): # pragma: no cover - test shim + return record # Helper metadata response for logical name resolution MD_ACCOUNT = { From f80ca040ead99b214fe61c528038650b95c8d8c6 Mon Sep 17 00:00:00 2001 From: Tim Pellissier Date: Sun, 19 Oct 2025 22:37:28 -0700 Subject: [PATCH 2/2] undo code from another branch --- tests/test_create_single_guid.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_create_single_guid.py b/tests/test_create_single_guid.py index 6ed6166c..f75d320d 100644 --- a/tests/test_create_single_guid.py +++ b/tests/test_create_single_guid.py @@ -50,8 +50,5 @@ def test__create_single_fallback_location(): def test__create_single_missing_headers_raises(): c = TestableOData({}) import pytest - from dataverse_sdk.errors import MetadataError - from dataverse_sdk.error_codes import METADATA_CREATE_GUID_MISSING - with pytest.raises(RuntimeError) as ei: + with pytest.raises(RuntimeError): c._create_single("accounts", "account", {"name": "x"}) - assert ei.value.subcode == METADATA_CREATE_GUID_MISSING