Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/http-client-python"
---

Add mock API tests for XML model with enum and datetime properties, and fix XML deserialization for enum and datetime types
Original file line number Diff line number Diff line change
Expand Up @@ -1020,16 +1020,20 @@ def _deserialize_with_callable(
return float(value.text) if value.text else None
if deserializer is bool:
return value.text == "true" if value.text else None
if callable(deserializer) and deserializer in _DESERIALIZE_MAPPING.values():
return deserializer(value.text)
if callable(deserializer) and deserializer in _DESERIALIZE_MAPPING_WITHFORMAT.values():
return deserializer(value.text)
if deserializer is None:
return value
if deserializer in [int, float, bool]:
return deserializer(value)
if isinstance(deserializer, CaseInsensitiveEnumMeta):
try:
return deserializer(value)
return deserializer(value.text if isinstance(value, ET.Element) else value)
except ValueError:
# for unknown value, return raw value
return value
return value.text if isinstance(value, ET.Element) else value
if isinstance(deserializer, type) and issubclass(deserializer, Model):
return deserializer._deserialize(value, [])
return typing.cast(typing.Callable[[typing.Any], typing.Any], deserializer)(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import datetime
import pytest
from payload.xml.aio import XmlClient
from payload.xml.models import (
Expand All @@ -18,6 +19,8 @@
ModelWithText,
ModelWithDictionary,
ModelWithEncodedNames,
ModelWithEnum,
ModelWithDatetime,
)


Expand Down Expand Up @@ -119,6 +122,24 @@ async def test_model_with_encoded_names(client: XmlClient):
await client.model_with_encoded_names_value.put(model)


@pytest.mark.asyncio
async def test_model_with_enum(client: XmlClient):
model = ModelWithEnum(status="success")
assert await client.model_with_enum_value.get() == model
await client.model_with_enum_value.put(model)


@pytest.mark.asyncio
async def test_model_with_datetime(client: XmlClient):
model = ModelWithDatetime(
rfc3339=datetime.datetime(2022, 8, 26, 18, 38, 0, tzinfo=datetime.timezone.utc),
rfc7231=datetime.datetime(2022, 8, 26, 14, 38, 0, tzinfo=datetime.timezone.utc),
)
result = await client.model_with_datetime_value.get()
assert result.rfc3339 == model.rfc3339
assert result.rfc7231 == model.rfc7231


@pytest.mark.asyncio
async def test_xml_error_value(client: XmlClient, core_library):
with pytest.raises(core_library.exceptions.HttpResponseError) as ex:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import datetime
import pytest
from payload.xml import XmlClient
from payload.xml.models import (
Expand All @@ -18,6 +19,8 @@
ModelWithText,
ModelWithDictionary,
ModelWithEncodedNames,
ModelWithEnum,
ModelWithDatetime,
)


Expand Down Expand Up @@ -107,6 +110,22 @@ def test_model_with_encoded_names(client: XmlClient):
client.model_with_encoded_names_value.put(model)


def test_model_with_enum(client: XmlClient):
model = ModelWithEnum(status="success")
assert client.model_with_enum_value.get() == model
client.model_with_enum_value.put(model)


def test_model_with_datetime(client: XmlClient):
model = ModelWithDatetime(
rfc3339=datetime.datetime(2022, 8, 26, 18, 38, 0, tzinfo=datetime.timezone.utc),
rfc7231=datetime.datetime(2022, 8, 26, 14, 38, 0, tzinfo=datetime.timezone.utc),
)
result = client.model_with_datetime_value.get()
assert result.rfc3339 == model.rfc3339
assert result.rfc7231 == model.rfc7231


def test_xml_error_value(client: XmlClient, core_library):
with pytest.raises(core_library.exceptions.HttpResponseError) as ex:
client.xml_error_value.get()
Expand Down
19 changes: 10 additions & 9 deletions packages/http-client-python/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/http-client-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"@typespec/sse": "~0.79.0",
"@typespec/streams": "~0.79.0",
"@typespec/xml": "~0.79.0",
"@typespec/http-specs": "0.1.0-alpha.32",
"@typespec/http-specs": "0.1.0-alpha.33-dev.2",
"@types/js-yaml": "~4.0.5",
"@types/node": "~25.0.2",
"@types/semver": "7.5.8",
Expand Down
Loading