Skip to content

Commit e562654

Browse files
committed
Add unit test for Literal parsing
1 parent 35c5b40 commit e562654

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,4 @@
1-
from typing import List, Optional
1+
from typing import List, Optional, Literal
22

33
import pytest
44
from sqlalchemy.exc import IntegrityError
@@ -125,3 +125,26 @@ class Hero(SQLModel, table=True):
125125
# The next statement should not raise an AttributeError
126126
assert hero_rusty_man.team
127127
assert hero_rusty_man.team.name == "Preventers"
128+
129+
130+
def test_literal_typehints_are_treated_as_strings(clear_sqlmodel):
131+
"""Test https://github.com/fastapi/sqlmodel/issues/57"""
132+
133+
class Hero(SQLModel, table=True):
134+
id: Optional[int] = Field(default=None, primary_key=True)
135+
name: str = Field(unique=True)
136+
weakness: Literal["Kryptonite", "Dehydration", "Munchies"]
137+
138+
139+
superman = Hero(name="Superman", weakness="Kryptonite")
140+
141+
engine = create_engine("sqlite://", echo=True)
142+
143+
SQLModel.metadata.create_all(engine)
144+
145+
with Session(engine) as session:
146+
session.add(superman)
147+
session.commit()
148+
session.refresh(superman)
149+
assert superman.weakness == "Kryptonite"
150+
assert isinstance(superman.weakness, str)

0 commit comments

Comments
 (0)