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

Function test_to_json

tests/test_models.py:575–600  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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