|
| 1 | +from pydantic import ConfigDict as PydanticConfigDict |
| 2 | +from sqlmodel import ConfigDict, Field, SQLModel, SQLModelConfig |
| 3 | + |
| 4 | + |
| 5 | +def test_sqlmodel_config_export(): |
| 6 | + import sqlmodel |
| 7 | + import sqlmodel.main |
| 8 | + |
| 9 | + assert hasattr(sqlmodel, "SQLModelConfig") |
| 10 | + assert hasattr(sqlmodel.main, "SQLModelConfig") |
| 11 | + assert hasattr(sqlmodel, "ConfigDict") |
| 12 | + assert sqlmodel.SQLModelConfig is SQLModelConfig |
| 13 | + assert sqlmodel.ConfigDict is ConfigDict |
| 14 | + |
| 15 | + |
| 16 | +def test_model_with_sqlmodel_config(): |
| 17 | + class Item(SQLModel): |
| 18 | + id: int |
| 19 | + name: str |
| 20 | + model_config = SQLModelConfig(from_attributes=True) |
| 21 | + |
| 22 | + assert Item.model_config.get("from_attributes") is True |
| 23 | + |
| 24 | + |
| 25 | +def test_model_with_sqlmodel_reexported_config_dict(): |
| 26 | + class Item(SQLModel): |
| 27 | + id: int |
| 28 | + name: str |
| 29 | + model_config = ConfigDict(from_attributes=True) |
| 30 | + |
| 31 | + assert Item.model_config.get("from_attributes") is True |
| 32 | + |
| 33 | + |
| 34 | +def test_model_with_pydantic_config_dict(): |
| 35 | + class Item(SQLModel): |
| 36 | + id: int |
| 37 | + name: str |
| 38 | + model_config = PydanticConfigDict(from_attributes=True) |
| 39 | + |
| 40 | + assert Item.model_config.get("from_attributes") is True |
| 41 | + |
| 42 | + |
| 43 | +def test_model_with_plain_dict_config(): |
| 44 | + class Item(SQLModel): |
| 45 | + id: int |
| 46 | + name: str |
| 47 | + model_config = {"from_attributes": True} |
| 48 | + |
| 49 | + assert Item.model_config.get("from_attributes") is True |
| 50 | + |
| 51 | + |
| 52 | +def test_table_model_with_sqlmodel_config(): |
| 53 | + class Hero(SQLModel, table=True): |
| 54 | + id: int | None = Field(default=None, primary_key=True) |
| 55 | + name: str |
| 56 | + model_config = SQLModelConfig(extra="ignore") |
| 57 | + |
| 58 | + assert Hero.model_config.get("extra") == "ignore" |
| 59 | + assert Hero.model_config.get("table") is True |
0 commit comments