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