MCPcopy Create free account
hub / github.com/anomalyco/opencode-sdk-python / test_to_dict

Function test_to_dict

tests/test_models.py:507–536  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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