Coverage for tests/test_instance_no_args.py: 100%
27 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-16 00:02 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-16 00:02 +0000
1import pytest 1nopqrstuvwxyz
2from pydantic import ValidationError 1nopqrstuvwxyz
3from sqlalchemy import Engine 1nopqrstuvwxyz
4from sqlmodel import Field, Session, SQLModel, select 1nopqrstuvwxyz
7def test_allow_instantiation_without_arguments(database_engine: Engine): 1nopqrstuvwxyz
8 class Item(SQLModel, table=True): 1abcdefghijklm
9 id: int | None = Field(default=None, primary_key=True) 1abcdefghijklm
10 name: str 1abcdefghijklm
11 description: str | None = None 1abcdefghijklm
13 engine = database_engine 1abcdefghijklm
14 SQLModel.metadata.create_all(engine) 1abcdefghijklm
15 with Session(engine) as db: 1abcdefghijklm
16 item = Item() 1abcdefghijklm
17 item.name = "Rick" 1abcdefghijklm
18 db.add(item) 1abcdefghijklm
19 db.commit() 1abcdefghijklm
20 statement = select(Item) 1abcdefghijklm
21 result = db.exec(statement).all() 1abcdefghijklm
22 assert len(result) == 1 1abcdefghijklm
23 assert isinstance(item.id, int) 1abcdefghijklm
26def test_not_allow_instantiation_without_arguments_if_not_table(): 1nopqrstuvwxyz
27 class Item(SQLModel): 1ABCDEFGHIJKLM
28 id: int | None = Field(default=None, primary_key=True) 1ABCDEFGHIJKLM
29 name: str 1ABCDEFGHIJKLM
30 description: str | None = None 1ABCDEFGHIJKLM
32 with pytest.raises(ValidationError): 1ABCDEFGHIJKLM
33 Item() 1ABCDEFGHIJKLM