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