diff --git a/tests/test_class_hierarchy.py b/tests/test_class_hierarchy.py new file mode 100644 index 0000000..0f0ec1f --- /dev/null +++ b/tests/test_class_hierarchy.py @@ -0,0 +1,79 @@ +import datetime +import sys + +import pytest +from pydantic import AnyUrl, UrlConstraints +from typing_extensions import Annotated + +from sqlmodel import ( + BigInteger, + Column, + DateTime, + Field, + Integer, + SQLModel, + String, + create_engine, +) + +MoveSharedUrl = Annotated[ + AnyUrl, UrlConstraints(max_length=512, allowed_schemes=["smb", "ftp", "file"]) +] + + +@pytest.mark.skipif(sys.version_info < (3, 10)) +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(), + ) diff --git a/tests/test_nullable.py b/tests/test_nullable.py index c21d311..b4fa1bb 100644 --- a/tests/test_nullable.py +++ b/tests/test_nullable.py @@ -19,6 +19,8 @@ def test_nullable_fields(clear_sqlmodel, caplog): ) required_value: str optional_default_ellipsis: Optional[str] = Field(default=...) + optional_no_field: Optional[str] + optional_no_field_default: Optional[str] = Field(description="no default") optional_default_none: Optional[str] = Field(default=None) optional_non_nullable: Optional[str] = Field( nullable=False, @@ -55,7 +57,7 @@ def test_nullable_fields(clear_sqlmodel, caplog): str_default_str_nullable: str = Field(default="default", nullable=True) str_default_ellipsis_non_nullable: str = Field(default=..., nullable=False) str_default_ellipsis_nullable: str = Field(default=..., nullable=True) - annotated_any_url: MoveSharedUrl | None = Field(description="") + annotated_any_url: Optional[MoveSharedUrl] = Field(description="") engine = create_engine("sqlite://", echo=True) SQLModel.metadata.create_all(engine) @@ -66,6 +68,8 @@ def test_nullable_fields(clear_sqlmodel, caplog): assert "primary_key INTEGER NOT NULL," in create_table_log assert "required_value VARCHAR NOT NULL," in create_table_log assert "optional_default_ellipsis VARCHAR NOT NULL," in create_table_log + assert "optional_no_field VARCHAR," in create_table_log + assert "optional_no_field_default VARCHAR NOT NULL," in create_table_log assert "optional_default_none VARCHAR," in create_table_log assert "optional_non_nullable VARCHAR NOT NULL," in create_table_log assert "optional_nullable VARCHAR," in create_table_log