MCPcopy
hub / github.com/openai/openai-python / test_to_dict

Function test_to_dict

tests/test_models.py:508–537  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

506
507
508def test_to_dict() -> None:
509 class Model(BaseModel):
510 foo: Optional[str] = Field(alias="FOO", default=None)
511
512 m = Model(FOO="hello")
513 assert m.to_dict() == {"FOO": "hello"}
514 assert m.to_dict(use_api_names=False) == {"foo": "hello"}
515
516 m2 = Model()
517 assert m2.to_dict() == {}
518 assert m2.to_dict(exclude_unset=False) == {"FOO": None}
519 assert m2.to_dict(exclude_unset=False, exclude_none=True) == {}
520 assert m2.to_dict(exclude_unset=False, exclude_defaults=True) == {}
521
522 m3 = Model(FOO=None)
523 assert m3.to_dict() == {"FOO": None}
524 assert m3.to_dict(exclude_none=True) == {}
525 assert m3.to_dict(exclude_defaults=True) == {}
526
527 class Model2(BaseModel):
528 created_at: datetime
529
530 time_str = "2024-03-21T11:39:01.275859"
531 m4 = Model2.construct(created_at=time_str)
532 assert m4.to_dict(mode="python") == {"created_at": datetime.fromisoformat(time_str)}
533 assert m4.to_dict(mode="json") == {"created_at": time_str}
534
535 if PYDANTIC_V1:
536 with pytest.raises(ValueError, match="warnings is only supported in Pydantic v2"):
537 m.to_dict(warnings=False)
538
539
540def test_forwards_compat_model_dump_method() -> None:

Callers

nothing calls this directly

Calls 3

to_dictMethod · 0.80
ModelClass · 0.70
constructMethod · 0.45

Tested by

no test coverage detected