-
-
Notifications
You must be signed in to change notification settings - Fork 849
Expand file tree
/
Copy pathtest_update.py
More file actions
32 lines (28 loc) · 1.01 KB
/
test_update.py
File metadata and controls
32 lines (28 loc) · 1.01 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
from pytest import raises
from sqlmodel import Field, SQLModel
def test_sqlmodel_update():
class Organization(SQLModel, table=True):
id: int = Field(default=None, primary_key=True)
name: str
city: str
headquarters: str
class OrganizationUpdate(SQLModel):
name: str = Field(exclude=True)
city: str | None = None
org = Organization(name="Example Org", city="New York", headquarters="NYC HQ")
org_in = OrganizationUpdate(name="Updated org")
org.sqlmodel_update(
org_in,
update={
"headquarters": "-", # This field is in Organization, but not in OrganizationUpdate
},
exclude_unset=True,
)
# fields that should stay the same
assert org.city == "New York"
# fields that should be updated
assert org.name == "Updated org"
assert org.headquarters == "-"
# test raise value error when passing in updates other than dict or BaseModel
with raises(ValueError):
org.sqlmodel_update(["Boston"])