Coverage for tests/test_instance_no_args.py: 100%
28 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-27 00:03 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-27 00:03 +0000
1from typing import Optional 1klmnopqrst
3import pytest 1klmnopqrst
4from pydantic import ValidationError 1klmnopqrst
5from sqlmodel import Field, Session, SQLModel, create_engine, select 1klmnopqrst
8def test_allow_instantiation_without_arguments(clear_sqlmodel): 1klmnopqrst
9 class Item(SQLModel, table=True): 1abcdefghij
10 id: Optional[int] = Field(default=None, primary_key=True) 1abcdefghij
11 name: str 1abcdefghij
12 description: Optional[str] = None 1abcdefghij
14 engine = create_engine("sqlite:///:memory:") 1abcdefghij
15 SQLModel.metadata.create_all(engine) 1abcdefghij
16 with Session(engine) as db: 1abcdefghij
17 item = Item() 1abcdefghij
18 item.name = "Rick" 1abcdefghij
19 db.add(item) 1abcdefghij
20 db.commit() 1abcdefghij
21 statement = select(Item) 1abcdefghij
22 result = db.exec(statement).all() 1abcdefghij
23 assert len(result) == 1 1abcdefghij
24 assert isinstance(item.id, int) 1abcdefghij
25 SQLModel.metadata.clear() 1abcdefghij
28def test_not_allow_instantiation_without_arguments_if_not_table(): 1klmnopqrst
29 class Item(SQLModel): 1uvwxyzABCD
30 id: Optional[int] = Field(default=None, primary_key=True) 1uvwxyzABCD
31 name: str 1uvwxyzABCD
32 description: Optional[str] = None 1uvwxyzABCD
34 with pytest.raises(ValidationError): 1uvwxyzABCD
35 Item() 1uvwxyzABCD