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

Function test_to_json

tests/test_models.py:609–634  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

607
608
609def test_to_json() -> None:
610 class Model(BaseModel):
611 foo: Optional[str] = Field(alias="FOO", default=None)
612
613 m = Model(FOO="hello")
614 assert json.loads(m.to_json()) == {"FOO": "hello"}
615 assert json.loads(m.to_json(use_api_names=False)) == {"foo": "hello"}
616
617 if PYDANTIC_V1:
618 assert m.to_json(indent=None) == '{"FOO": "hello"}'
619 else:
620 assert m.to_json(indent=None) == '{"FOO":"hello"}'
621
622 m2 = Model()
623 assert json.loads(m2.to_json()) == {}
624 assert json.loads(m2.to_json(exclude_unset=False)) == {"FOO": None}
625 assert json.loads(m2.to_json(exclude_unset=False, exclude_none=True)) == {}
626 assert json.loads(m2.to_json(exclude_unset=False, exclude_defaults=True)) == {}
627
628 m3 = Model(FOO=None)
629 assert json.loads(m3.to_json()) == {"FOO": None}
630 assert json.loads(m3.to_json(exclude_none=True)) == {}
631
632 if PYDANTIC_V1:
633 with pytest.raises(ValueError, match="warnings is only supported in Pydantic v2"):
634 m.to_json(warnings=False)
635
636
637def 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