MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / test_to_json

Function test_to_json

tests/test_models.py:590–615  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

588
589
590def test_to_json() -> None:
591 class Model(BaseModel):
592 foo: Optional[str] = Field(alias="FOO", default=None)
593
594 m = Model(FOO="hello")
595 assert json.loads(m.to_json()) == {"FOO": "hello"}
596 assert json.loads(m.to_json(use_api_names=False)) == {"foo": "hello"}
597
598 if PYDANTIC_V1:
599 assert m.to_json(indent=None) == '{"FOO": "hello"}'
600 else:
601 assert m.to_json(indent=None) == '{"FOO":"hello"}'
602
603 m2 = Model()
604 assert json.loads(m2.to_json()) == {}
605 assert json.loads(m2.to_json(exclude_unset=False)) == {"FOO": None}
606 assert json.loads(m2.to_json(exclude_unset=False, exclude_none=True)) == {}
607 assert json.loads(m2.to_json(exclude_unset=False, exclude_defaults=True)) == {}
608
609 m3 = Model(FOO=None)
610 assert json.loads(m3.to_json()) == {"FOO": None}
611 assert json.loads(m3.to_json(exclude_none=True)) == {}
612
613 if PYDANTIC_V1:
614 with pytest.raises(ValueError, match="warnings is only supported in Pydantic v2"):
615 m.to_json(warnings=False)
616
617
618def test_forwards_compat_model_dump_json_method() -> None:

Callers

nothing calls this directly

Calls 2

to_jsonMethod · 0.80
ModelClass · 0.70

Tested by

no test coverage detected