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

Function test_to_json

tests/test_models.py:576–601  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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