Coverage for tests / test_instance_no_args.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-06 21:09 +0000

1from typing import Optional 1jklmnopqr

2 

3import pytest 1jklmnopqr

4from pydantic import ValidationError 1jklmnopqr

5from sqlmodel import Field, Session, SQLModel, create_engine, select 1jklmnopqr

6 

7 

8def test_allow_instantiation_without_arguments(clear_sqlmodel): 1jklmnopqr

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

10 id: Optional[int] = Field(default=None, primary_key=True) 1abcdefghi

11 name: str 1abcdefghi

12 description: Optional[str] = None 1abcdefghi

13 

14 engine = create_engine("sqlite:///:memory:") 1abcdefghi

15 SQLModel.metadata.create_all(engine) 1abcdefghi

16 with Session(engine) as db: 1abcdefghi

17 item = Item() 1abcdefghi

18 item.name = "Rick" 1abcdefghi

19 db.add(item) 1abcdefghi

20 db.commit() 1abcdefghi

21 statement = select(Item) 1abcdefghi

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

23 assert len(result) == 1 1abcdefghi

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

25 SQLModel.metadata.clear() 1abcdefghi

26 

27 

28def test_not_allow_instantiation_without_arguments_if_not_table(): 1jklmnopqr

29 class Item(SQLModel): 1stuvwxyzA

30 id: Optional[int] = Field(default=None, primary_key=True) 1stuvwxyzA

31 name: str 1stuvwxyzA

32 description: Optional[str] = None 1stuvwxyzA

33 

34 with pytest.raises(ValidationError): 1stuvwxyzA

35 Item() 1stuvwxyzA