Browse Source

fix: better test case

pull/12504/head
Nikita Zavadin 6 months ago
parent
commit
c7282a7eef
No known key found for this signature in database GPG Key ID: 9F14940057AA9BBD
  1. 26
      tests/test_compat.py

26
tests/test_compat.py

@ -88,25 +88,41 @@ def test_propagates_pydantic2_model_config():
def __bool__(self): def __bool__(self):
return False return False
class EmbeddedModel(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
value: Union[str, Missing] = Missing()
class Model(BaseModel): class Model(BaseModel):
model_config = ConfigDict( model_config = ConfigDict(
arbitrary_types_allowed=True, arbitrary_types_allowed=True,
) )
value: Union[str, Missing] = Missing() value: Union[str, Missing] = Missing()
embedded_model: EmbeddedModel = EmbeddedModel()
@app.post("/") @app.post("/")
def foo(req: Model) -> Union[str, None]: def foo(req: Model) -> Dict[str, Union[str, None]]:
return req.value or None return {
"value": req.value or None,
"embedded_value": req.embedded_model.value or None,
}
client = TestClient(app) client = TestClient(app)
response = client.post("/", json={}) response = client.post("/", json={})
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
assert response.json() is None assert response.json() == {
"value": None,
"embedded_value": None,
}
response2 = client.post("/", json={"value": "foo"}) response2 = client.post(
"/", json={"value": "foo", "embedded_model": {"value": "bar"}}
)
assert response2.status_code == 200, response2.text assert response2.status_code == 200, response2.text
assert response2.json() == "foo" assert response2.json() == {
"value": "foo",
"embedded_value": "bar",
}
def test_is_bytes_sequence_annotation_union(): def test_is_bytes_sequence_annotation_union():

Loading…
Cancel
Save