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

Function test_to_json

tests/test_models.py:572–597  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

570
571
572def test_to_json() -> None:
573 class Model(BaseModel):
574 foo: Optional[str] = Field(alias="FOO", default=None)
575
576 m = Model(FOO="hello")
577 assert json.loads(m.to_json()) == {"FOO": "hello"}
578 assert json.loads(m.to_json(use_api_names=False)) == {"foo": "hello"}
579
580 if PYDANTIC_V2:
581 assert m.to_json(indent=None) == '{"FOO":"hello"}'
582 else:
583 assert m.to_json(indent=None) == '{"FOO": "hello"}'
584
585 m2 = Model()
586 assert json.loads(m2.to_json()) == {}
587 assert json.loads(m2.to_json(exclude_unset=False)) == {"FOO": None}
588 assert json.loads(m2.to_json(exclude_unset=False, exclude_none=True)) == {}
589 assert json.loads(m2.to_json(exclude_unset=False, exclude_defaults=True)) == {}
590
591 m3 = Model(FOO=None)
592 assert json.loads(m3.to_json()) == {"FOO": None}
593 assert json.loads(m3.to_json(exclude_none=True)) == {}
594
595 if not PYDANTIC_V2:
596 with pytest.raises(ValueError, match="warnings is only supported in Pydantic v2"):
597 m.to_json(warnings=False)
598
599
600def 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