Skip to content
Merged
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
16 changes: 12 additions & 4 deletions tests/test_create_single_guid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,45 @@ 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):
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"})
c._create_single("accounts", "account", {"name": "x"})
15 changes: 5 additions & 10 deletions tests/test_logical_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,29 @@ 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

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 = {
Expand Down
Loading