forked from AntonDeMeester/sqlmodel
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from honglei/main
get_column_from_field support functional sa_column
- Loading branch information
Showing
5 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import datetime | ||
import sys | ||
|
||
import pytest | ||
from pydantic import AnyUrl, UrlConstraints | ||
from sqlmodel import ( | ||
BigInteger, | ||
Column, | ||
DateTime, | ||
Field, | ||
Integer, | ||
SQLModel, | ||
String, | ||
create_engine, | ||
) | ||
from typing_extensions import Annotated | ||
|
||
MoveSharedUrl = Annotated[ | ||
AnyUrl, UrlConstraints(max_length=512, allowed_schemes=["smb", "ftp", "file"]) | ||
] | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher") | ||
def test_field_resuse(): | ||
class BasicFileLog(SQLModel): | ||
resourceID: int = Field( | ||
sa_column=lambda: Column(Integer, index=True), description=""" """ | ||
) | ||
transportID: Annotated[int | None, Field(description=" for ")] = None | ||
fileName: str = Field( | ||
sa_column=lambda: Column(String, index=True), description=""" """ | ||
) | ||
fileSize: int | None = Field( | ||
sa_column=lambda: Column(BigInteger), ge=0, description=""" """ | ||
) | ||
beginTime: datetime.datetime | None = Field( | ||
sa_column=lambda: Column( | ||
DateTime(timezone=True), | ||
index=True, | ||
), | ||
description="", | ||
) | ||
|
||
class SendFileLog(BasicFileLog, table=True): | ||
id: int | None = Field( | ||
sa_column=Column(Integer, primary_key=True, autoincrement=True), | ||
description=""" """, | ||
) | ||
sendUser: str | ||
dstUrl: MoveSharedUrl | None | ||
|
||
class RecvFileLog(BasicFileLog, table=True): | ||
id: int | None = Field( | ||
sa_column=Column(Integer, primary_key=True, autoincrement=True), | ||
description=""" """, | ||
) | ||
recvUser: str | ||
|
||
sqlite_file_name = "database.db" | ||
sqlite_url = f"sqlite:///{sqlite_file_name}" | ||
|
||
engine = create_engine(sqlite_url, echo=True) | ||
SQLModel.metadata.drop_all(engine) | ||
SQLModel.metadata.create_all(engine) | ||
SendFileLog( | ||
sendUser="j", | ||
resourceID=1, | ||
fileName="a.txt", | ||
fileSize=3234, | ||
beginTime=datetime.datetime.now(), | ||
) | ||
RecvFileLog( | ||
sendUser="j", | ||
resourceID=1, | ||
fileName="a.txt", | ||
fileSize=3234, | ||
beginTime=datetime.datetime.now(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import Optional | ||
|
||
from sqlmodel import Field, Session, SQLModel, create_engine | ||
|
||
|
||
def test_model_copy(clear_sqlmodel): | ||
"""Test validation of implicit and explict None values. | ||
# For consistency with pydantic, validators are not to be called on | ||
# arguments that are not explicitly provided. | ||
https://github.com/tiangolo/sqlmodel/issues/230 | ||
https://github.com/samuelcolvin/pydantic/issues/1223 | ||
""" | ||
|
||
class Hero(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
secret_name: str | ||
age: Optional[int] = None | ||
|
||
hero = Hero(name="Deadpond", secret_name="Dive Wilson", age=25) | ||
|
||
engine = create_engine("sqlite://") | ||
|
||
SQLModel.metadata.create_all(engine) | ||
|
||
with Session(engine) as session: | ||
session.add(hero) | ||
session.commit() | ||
session.refresh(hero) | ||
|
||
model_copy = hero.model_copy(update={"name": "Deadpond Copy"}) | ||
|
||
assert ( | ||
model_copy.name == "Deadpond Copy" | ||
and model_copy.secret_name == "Dive Wilson" | ||
and model_copy.age == 25 | ||
) | ||
|
||
db_hero = session.get(Hero, hero.id) | ||
|
||
db_copy = db_hero.model_copy(update={"name": "Deadpond Copy"}) | ||
|
||
assert ( | ||
db_copy.name == "Deadpond Copy" | ||
and db_copy.secret_name == "Dive Wilson" | ||
and db_copy.age == 25 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters