-
-
Notifications
You must be signed in to change notification settings - Fork 844
Expand file tree
/
Copy pathtest_tablename.py
More file actions
85 lines (57 loc) · 2.45 KB
/
test_tablename.py
File metadata and controls
85 lines (57 loc) · 2.45 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from sqlalchemy import inspect
from sqlmodel import Field, Session, SQLModel, create_engine, select
from sqlmodel.pool import StaticPool
def _engine():
return create_engine(
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
)
def test_default_tablename() -> None:
"""table=True models get __tablename__ = classname.lower() by default."""
class Gadget(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
assert Gadget.__tablename__ == "gadget"
engine = _engine()
SQLModel.metadata.create_all(engine)
assert inspect(engine).has_table("gadget")
def test_explicit_tablename() -> None:
"""An explicit __tablename__ overrides the default."""
class Widget(SQLModel, table=True):
__tablename__ = "custom_widgets"
id: int | None = Field(default=None, primary_key=True)
name: str
assert Widget.__tablename__ == "custom_widgets"
engine = _engine()
SQLModel.metadata.create_all(engine)
assert inspect(engine).has_table("custom_widgets")
assert not inspect(engine).has_table("widget")
with Session(engine) as session:
session.add(Widget(name="sprocket"))
session.commit()
with Session(engine) as session:
row = session.exec(select(Widget)).first()
assert row is not None
assert row.name == "sprocket"
def test_tablename_inheritance_default() -> None:
"""A subclass that is also a table gets its own default __tablename__."""
class BaseThing(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
kind: str = "base"
class SubThing(BaseThing, table=True):
extra: str | None = None
assert BaseThing.__tablename__ == "basething"
assert SubThing.__tablename__ == "subthing"
def test_tablename_inheritance_explicit_child() -> None:
"""A subclass can set its own __tablename__, visible on the class."""
class Vehicle(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
kind: str = ""
class Truck(Vehicle, table=True):
__tablename__ = "trucks"
payload: int | None = None
assert Vehicle.__tablename__ == "vehicle"
assert Truck.__tablename__ == "trucks"
def test_tablename_default_on_plain_model() -> None:
"""Non-table models also get a default __tablename__."""
class Schema(SQLModel):
name: str
assert Schema.__tablename__ == "schema"