Skip to content

Commit 2218d5b

Browse files
UT fix after logicalname + optionset updates (#21)
* fix unit tests after optionset / logicalname changes * undo code from another branch --------- Co-authored-by: Tim Pellissier <tpellissier@microsoft.com>
1 parent e772134 commit 2218d5b

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

tests/test_create_single_guid.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,45 @@ class DummyHTTP:
1010
def __init__(self, headers):
1111
self._headers = headers
1212
def request(self, method, url, **kwargs):
13-
# Simulate minimal Response-like object
13+
# Simulate minimal Response-like object (subset of requests.Response API used by code)
1414
resp = types.SimpleNamespace()
1515
resp.headers = self._headers
1616
resp.status_code = 204
17+
resp.text = ""
1718
def raise_for_status():
1819
return None
20+
def json_func():
21+
return {}
1922
resp.raise_for_status = raise_for_status
23+
resp.json = json_func
2024
return resp
2125

2226
class TestableOData(ODataClient):
2327
def __init__(self, headers):
2428
super().__init__(DummyAuth(), "https://org.example", None)
2529
# Monkey-patch http client
2630
self._http = types.SimpleNamespace(request=lambda method, url, **kwargs: DummyHTTP(headers).request(method, url, **kwargs))
31+
# Bypass optionset label conversion to keep response sequence stable for tests
32+
def _convert_labels_to_ints(self, logical_name, record): # pragma: no cover - test shim
33+
return record
2734

2835
def test__create_single_uses_odata_entityid():
2936
guid = "11111111-2222-3333-4444-555555555555"
3037
headers = {"OData-EntityId": f"https://org.example/api/data/v9.2/accounts({guid})"}
3138
c = TestableOData(headers)
32-
result = c._create_single("accounts", {"name": "x"})
39+
# Current signature requires logical name explicitly
40+
result = c._create_single("accounts", "account", {"name": "x"})
3341
assert result == guid
3442

3543
def test__create_single_fallback_location():
3644
guid = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
3745
headers = {"Location": f"https://org.example/api/data/v9.2/contacts({guid})"}
3846
c = TestableOData(headers)
39-
result = c._create_single("contacts", {"firstname": "x"})
47+
result = c._create_single("contacts", "contact", {"firstname": "x"})
4048
assert result == guid
4149

4250
def test__create_single_missing_headers_raises():
4351
c = TestableOData({})
4452
import pytest
4553
with pytest.raises(RuntimeError):
46-
c._create_single("accounts", {"name": "x"})
54+
c._create_single("accounts", "account", {"name": "x"})

tests/test_logical_crud.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,29 @@ def __init__(self, responses):
1313
self.calls = []
1414
def request(self, method, url, **kwargs):
1515
self.calls.append((method, url, kwargs))
16-
# Pop next prepared response tuple
1716
if not self._responses:
1817
raise AssertionError("No more dummy responses configured")
1918
status, headers, body = self._responses.pop(0)
2019
resp = types.SimpleNamespace()
2120
resp.status_code = status
2221
resp.headers = headers
23-
resp.text = body if isinstance(body, str) else (body and "{}")
22+
resp.text = "" if body is None else ("{}" if isinstance(body, dict) else str(body))
2423
def raise_for_status():
2524
if status >= 400:
2625
raise RuntimeError(f"HTTP {status}")
2726
return None
28-
resp.raise_for_status = raise_for_status
2927
def json_func():
30-
import json as _json
31-
if isinstance(body, dict):
32-
return body
33-
try:
34-
return _json.loads(body) if isinstance(body, str) else {}
35-
except Exception:
36-
return {}
28+
return body if isinstance(body, dict) else {}
29+
resp.raise_for_status = raise_for_status
3730
resp.json = json_func
3831
return resp
3932

4033
class TestableClient(ODataClient):
4134
def __init__(self, responses):
4235
super().__init__(DummyAuth(), "https://org.example", None)
4336
self._http = DummyHTTPClient(responses)
37+
def _convert_labels_to_ints(self, logical_name, record): # pragma: no cover - test shim
38+
return record
4439

4540
# Helper metadata response for logical name resolution
4641
MD_ACCOUNT = {

0 commit comments

Comments
 (0)