Skip to content

Commit 605291f

Browse files
committed
add test for optional annotated decimal field
1 parent bef1602 commit 605291f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

tests/test_main.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import List, Optional
1+
from decimal import Decimal
2+
from typing import Annotated, List, Optional
23

34
import pytest
45
from sqlalchemy.exc import IntegrityError
@@ -125,3 +126,25 @@ class Hero(SQLModel, table=True):
125126
# The next statement should not raise an AttributeError
126127
assert hero_rusty_man.team
127128
assert hero_rusty_man.team.name == "Preventers"
129+
130+
131+
def test_optional_annotated_decimal():
132+
class Model(SQLModel, table=True):
133+
id: Optional[int] = Field(default=None, primary_key=True)
134+
dec: Annotated[Decimal, Field(max_digits=4, decimal_places=2)] | None = None
135+
136+
engine = create_engine("sqlite://")
137+
138+
SQLModel.metadata.create_all(engine)
139+
140+
with Session(engine) as session:
141+
session.add(model := Model(dec=Decimal("3.14")))
142+
session.commit()
143+
session.refresh(model)
144+
assert model.dec == Decimal("3.14")
145+
146+
with Session(engine) as session:
147+
session.add(model := Model(dec=Decimal("3.142")))
148+
session.commit()
149+
session.refresh(model)
150+
assert model.dec == Decimal("3.14")

0 commit comments

Comments
 (0)