Coverage for tests / test_instance_no_args.py: 100%
27 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-06-10 09:40 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-06-10 09:40 +0000
1import pytest 1qrstuvwx
2from pydantic import ValidationError 1qrstuvwx
3from sqlmodel import Field, Session, SQLModel, create_engine, select 1qrstuvwx
6def test_allow_instantiation_without_arguments(clear_sqlmodel): 1qrstuvwx
7 class Item(SQLModel, table=True): 1abcdefgh
8 id: int | None = Field(default=None, primary_key=True) 1abcdefgh
9 name: str 1abcdefgh
10 description: str | None = None 1abcdefgh
12 engine = create_engine("sqlite:///:memory:") 1abcdefgh
13 SQLModel.metadata.create_all(engine) 1abcdefgh
14 with Session(engine) as db: 1abcdefgh
15 item = Item() 1abcdefgh
16 item.name = "Rick" 1abcdefgh
17 db.add(item) 1abcdefgh
18 db.commit() 1abcdefgh
19 statement = select(Item) 1abcdefgh
20 result = db.exec(statement).all() 1abcdefgh
21 assert len(result) == 1 1abcdefgh
22 assert isinstance(item.id, int) 1abcdefgh
23 SQLModel.metadata.clear() 1abcdefgh
26def test_not_allow_instantiation_without_arguments_if_not_table(): 1qrstuvwx
27 class Item(SQLModel): 1ijklmnop
28 id: int | None = Field(default=None, primary_key=True) 1ijklmnop
29 name: str 1ijklmnop
30 description: str | None = None 1ijklmnop
32 with pytest.raises(ValidationError): 1ijklmnop
33 Item() 1ijklmnop