Skip to content

Commit 0970f8f

Browse files
Adding raw data-model tests
1 parent 21721a5 commit 0970f8f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/codegen/snapshot/test_enum.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
import json
23
from io import StringIO
34

45
from pytest_snapshot.plugin import Snapshot
@@ -203,3 +204,46 @@ def test_unknown_enum(snapshot: Snapshot) -> None:
203204
x = NeedsenumErrorsTypeAdapter.validate_python(error)
204205
assert x.code == error["code"]
205206
assert x.message == error["message"]
207+
208+
209+
def test_unknown_enum_field_aliases(snapshot: Snapshot) -> None:
210+
validate_codegen(
211+
snapshot=snapshot,
212+
read_schema=lambda: StringIO(test_unknown_enum_schema),
213+
target_path="test_unknown_enum",
214+
client_name="foo",
215+
)
216+
217+
import tests.codegen.snapshot.snapshots.test_unknown_enum
218+
219+
importlib.reload(tests.codegen.snapshot.snapshots.test_unknown_enum)
220+
from tests.codegen.snapshot.snapshots.test_unknown_enum.enumService.needsEnumObject import ( # noqa
221+
NeedsenumobjectOutputTypeAdapter,
222+
NeedsenumobjectOutput,
223+
NeedsenumobjectOutputFooOneOf_out_first,
224+
)
225+
226+
initial = NeedsenumobjectOutput(foo=NeedsenumobjectOutputFooOneOf_out_first(foo=5))
227+
result = NeedsenumobjectOutputTypeAdapter.dump_json(
228+
initial,
229+
by_alias=True,
230+
)
231+
232+
obj = json.loads(result)
233+
234+
# Make sure we are testing what we think we are testing
235+
assert "foo" in obj
236+
237+
# We must not include the un-aliased field name
238+
assert "kind" not in obj["foo"]
239+
240+
# We must include the aliased field name
241+
assert "$kind" in obj["foo"]
242+
243+
# ... and finally that the values are what we think they should be
244+
assert obj["foo"]["$kind"] == "out_first"
245+
assert obj["foo"]["foo"] == 5
246+
247+
# And one more sanity check for the decoder
248+
decoded = NeedsenumobjectOutputTypeAdapter.validate_json(result)
249+
assert decoded == initial

0 commit comments

Comments
 (0)