Coverage for tests / test_instance_no_args.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-09 00:02 +0000

1import pytest 1jklmnopqr

2from pydantic import ValidationError 1jklmnopqr

3from sqlalchemy import Engine 1jklmnopqr

4from sqlmodel import Field, Session, SQLModel, select 1jklmnopqr

5 

6 

7def test_allow_instantiation_without_arguments(database_engine: Engine): 1jklmnopqr

8 class Item(SQLModel, table=True): 1abcdefghi

9 id: int | None = Field(default=None, primary_key=True) 1abcdefghi

10 name: str 1abcdefghi

11 description: str | None = None 1abcdefghi

12 

13 engine = database_engine 1abcdefghi

14 SQLModel.metadata.create_all(engine) 1abcdefghi

15 with Session(engine) as db: 1abcdefghi

16 item = Item() 1abcdefghi

17 item.name = "Rick" 1abcdefghi

18 db.add(item) 1abcdefghi

19 db.commit() 1abcdefghi

20 statement = select(Item) 1abcdefghi

21 result = db.exec(statement).all() 1abcdefghi

22 assert len(result) == 1 1abcdefghi

23 assert isinstance(item.id, int) 1abcdefghi

24 

25 

26def test_not_allow_instantiation_without_arguments_if_not_table(): 1jklmnopqr

27 class Item(SQLModel): 1stuvwxyzA

28 id: int | None = Field(default=None, primary_key=True) 1stuvwxyzA

29 name: str 1stuvwxyzA

30 description: str | None = None 1stuvwxyzA

31 

32 with pytest.raises(ValidationError): 1stuvwxyzA

33 Item() 1stuvwxyzA