-
-
Notifications
You must be signed in to change notification settings - Fork 837
Expand file tree
/
Copy pathtest_instance_no_args.py
More file actions
32 lines (26 loc) · 1001 Bytes
/
test_instance_no_args.py
File metadata and controls
32 lines (26 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pytest
from pydantic import ValidationError
from sqlmodel import Field, Session, SQLModel, create_engine, select
def test_not_allow_instantiation_without_arguments(clear_sqlmodel):
class Item(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
description: str | None = None
with pytest.raises(ValidationError):
Item()
def test_allow_instantiation_with_required_arguments(clear_sqlmodel):
class Item(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
description: str | None = None
engine = create_engine("sqlite:///:memory:")
SQLModel.metadata.create_all(engine)
with Session(engine) as db:
item = Item(name="Rick")
db.add(item)
db.commit()
statement = select(Item)
result = db.exec(statement).all()
assert len(result) == 1
assert isinstance(item.id, int)
SQLModel.metadata.clear()