Skip to content

Commit 40df10d

Browse files
Copilotmsyyc
andcommitted
feat: add XML enum/datetime mock API tests and fix XML deserialization for enum/datetime types
Co-authored-by: msyyc <[email protected]>
1 parent 371f8d2 commit 40df10d

6 files changed

Lines changed: 64 additions & 12 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: internal
3+
packages:
4+
- "@typespec/http-client-python"
5+
---
6+
7+
Add mock API tests for XML model with enum and datetime properties, and fix XML deserialization for enum and datetime types

packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,16 +1020,20 @@ def _deserialize_with_callable(
10201020
return float(value.text) if value.text else None
10211021
if deserializer is bool:
10221022
return value.text == "true" if value.text else None
1023+
if callable(deserializer) and deserializer in _DESERIALIZE_MAPPING.values():
1024+
return deserializer(value.text)
1025+
if callable(deserializer) and deserializer in _DESERIALIZE_MAPPING_WITHFORMAT.values():
1026+
return deserializer(value.text)
10231027
if deserializer is None:
10241028
return value
10251029
if deserializer in [int, float, bool]:
10261030
return deserializer(value)
10271031
if isinstance(deserializer, CaseInsensitiveEnumMeta):
10281032
try:
1029-
return deserializer(value)
1033+
return deserializer(value.text if isinstance(value, ET.Element) else value)
10301034
except ValueError:
10311035
# for unknown value, return raw value
1032-
return value
1036+
return value.text if isinstance(value, ET.Element) else value
10331037
if isinstance(deserializer, type) and issubclass(deserializer, Model):
10341038
return deserializer._deserialize(value, [])
10351039
return typing.cast(typing.Callable[[typing.Any], typing.Any], deserializer)(value)

packages/http-client-python/generator/test/generic_mock_api_tests/asynctests/test_payload_xml_async.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6+
import datetime
67
import pytest
78
from payload.xml.aio import XmlClient
89
from payload.xml.models import (
@@ -18,6 +19,8 @@
1819
ModelWithText,
1920
ModelWithDictionary,
2021
ModelWithEncodedNames,
22+
ModelWithEnum,
23+
ModelWithDatetime,
2124
)
2225

2326

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

121124

125+
@pytest.mark.asyncio
126+
async def test_model_with_enum(client: XmlClient):
127+
model = ModelWithEnum(status="success")
128+
assert await client.model_with_enum_value.get() == model
129+
await client.model_with_enum_value.put(model)
130+
131+
132+
@pytest.mark.asyncio
133+
async def test_model_with_datetime(client: XmlClient):
134+
model = ModelWithDatetime(
135+
rfc3339=datetime.datetime(2022, 8, 26, 18, 38, 0, tzinfo=datetime.timezone.utc),
136+
rfc7231=datetime.datetime(2022, 8, 26, 14, 38, 0, tzinfo=datetime.timezone.utc),
137+
)
138+
result = await client.model_with_datetime_value.get()
139+
assert result.rfc3339 == model.rfc3339
140+
assert result.rfc7231 == model.rfc7231
141+
142+
122143
@pytest.mark.asyncio
123144
async def test_xml_error_value(client: XmlClient, core_library):
124145
with pytest.raises(core_library.exceptions.HttpResponseError) as ex:

packages/http-client-python/generator/test/generic_mock_api_tests/test_payload_xml.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under the MIT License. See License.txt in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6+
import datetime
67
import pytest
78
from payload.xml import XmlClient
89
from payload.xml.models import (
@@ -18,6 +19,8 @@
1819
ModelWithText,
1920
ModelWithDictionary,
2021
ModelWithEncodedNames,
22+
ModelWithEnum,
23+
ModelWithDatetime,
2124
)
2225

2326

@@ -107,6 +110,22 @@ def test_model_with_encoded_names(client: XmlClient):
107110
client.model_with_encoded_names_value.put(model)
108111

109112

113+
def test_model_with_enum(client: XmlClient):
114+
model = ModelWithEnum(status="success")
115+
assert client.model_with_enum_value.get() == model
116+
client.model_with_enum_value.put(model)
117+
118+
119+
def test_model_with_datetime(client: XmlClient):
120+
model = ModelWithDatetime(
121+
rfc3339=datetime.datetime(2022, 8, 26, 18, 38, 0, tzinfo=datetime.timezone.utc),
122+
rfc7231=datetime.datetime(2022, 8, 26, 14, 38, 0, tzinfo=datetime.timezone.utc),
123+
)
124+
result = client.model_with_datetime_value.get()
125+
assert result.rfc3339 == model.rfc3339
126+
assert result.rfc7231 == model.rfc7231
127+
128+
110129
def test_xml_error_value(client: XmlClient, core_library):
111130
with pytest.raises(core_library.exceptions.HttpResponseError) as ex:
112131
client.xml_error_value.get()

packages/http-client-python/package-lock.json

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/http-client-python/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"@typespec/sse": "~0.79.0",
9595
"@typespec/streams": "~0.79.0",
9696
"@typespec/xml": "~0.79.0",
97-
"@typespec/http-specs": "0.1.0-alpha.32",
97+
"@typespec/http-specs": "0.1.0-alpha.33-dev.2",
9898
"@types/js-yaml": "~4.0.5",
9999
"@types/node": "~25.0.2",
100100
"@types/semver": "7.5.8",

0 commit comments

Comments
 (0)