Skip to content

Commit 24f66a0

Browse files
Abel Milashclaude
andcommitted
Add async tests for CreateEntities API mirror (PR #183)
The sync side added test_url_targets_create_entities and updated several _create_table assertions to use Entities[0] in PR #183. Those tests only cover the sync _create_entity; the async client has its own copy. Add three tests to cover the async-side behavior: - test_create_entity_url_targets_create_entities: URL ends with /CreateEntities - test_create_entity_payload_wraps_in_entities_array: body has Entities[0] with @odata.type=ComplexEntityMetadata - test_create_table_posts_complex_attribute_metadata: _create_table forwards complex=True so the posted Attributes use Complex*AttributeMetadata variants Tests: 2190 pass (up from 2187), _async_odata.py coverage at 98%. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 0464890 commit 24f66a0

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

tests/unit/aio/data/test_async_odata_internal.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,26 @@ async def test_invalid_display_name_raises(self):
728728
with pytest.raises(TypeError):
729729
await client._create_table("new_Product", {}, display_name=123)
730730

731+
async def test_create_table_posts_complex_attribute_metadata(self):
732+
"""_create_table forwards complex=True so the POST body uses Complex*AttributeMetadata variants."""
733+
client = _make_client()
734+
not_found = _resp(json_data={"value": []}, status=200)
735+
create_resp = _resp(status=204)
736+
entity_resp = _resp(
737+
json_data=_entity_def(entity_set="new_products", schema="new_Product", logical="new_product"),
738+
status=200,
739+
)
740+
client._request.side_effect = [not_found, create_resp, entity_resp]
741+
await client._create_table("new_Product", {"new_Title": "string"})
742+
# second call is the CreateEntities POST
743+
post_call = client._request.call_args_list[1]
744+
body = post_call.kwargs["json"]
745+
attrs = body["Entities"][0]["Attributes"]
746+
types = [a["@odata.type"] for a in attrs]
747+
assert "Microsoft.Dynamics.CRM.ComplexStringAttributeMetadata" in types
748+
# primary-name attribute is also string, so both attrs should be Complex variants
749+
assert all(t.startswith("Microsoft.Dynamics.CRM.Complex") for t in types)
750+
731751

732752
# ---------------------------------------------------------------------------
733753
# _create_columns() / _delete_columns()
@@ -1717,6 +1737,35 @@ async def test_create_entity_missing_metadata_id_raises(self):
17171737
with pytest.raises(RuntimeError, match="MetadataId missing"):
17181738
await client._create_entity("t", "t", "T", [])
17191739

1740+
async def test_create_entity_url_targets_create_entities(self):
1741+
"""_create_entity POSTs to /CreateEntities (not /EntityDefinitions)."""
1742+
client = _make_client()
1743+
client._request.return_value = _resp(status=204)
1744+
client._get_entity_by_table_schema_name = AsyncMock(
1745+
return_value={"EntitySetName": "ts", "MetadataId": "m1", "LogicalName": "t"}
1746+
)
1747+
await client._create_entity("new_table", "New Table", [])
1748+
args, _ = client._request.call_args
1749+
url = args[1]
1750+
assert url.endswith("/CreateEntities")
1751+
1752+
async def test_create_entity_payload_wraps_in_entities_array(self):
1753+
"""_create_entity payload wraps the entity body in an Entities[0] array with ComplexEntityMetadata."""
1754+
client = _make_client()
1755+
client._request.return_value = _resp(status=204)
1756+
client._get_entity_by_table_schema_name = AsyncMock(
1757+
return_value={"EntitySetName": "ts", "MetadataId": "m1", "LogicalName": "t"}
1758+
)
1759+
await client._create_entity("new_table", "New Table", [{"SchemaName": "new_col"}])
1760+
_, kwargs = client._request.call_args
1761+
body = kwargs["json"]
1762+
assert "Entities" in body
1763+
assert isinstance(body["Entities"], list) and len(body["Entities"]) == 1
1764+
entity = body["Entities"][0]
1765+
assert entity["@odata.type"] == "Microsoft.Dynamics.CRM.ComplexEntityMetadata"
1766+
assert entity["SchemaName"] == "new_table"
1767+
assert entity["Attributes"] == [{"SchemaName": "new_col"}]
1768+
17201769

17211770
class TestWaitForAttributeVisibilityWithDelay:
17221771
"""Coverage for _wait_for_attribute_visibility() sleep branch."""

0 commit comments

Comments
 (0)