add test for pydantic.AnyURL

This commit is contained in:
honglei 2023-08-16 23:18:55 +08:00
parent fa8955c700
commit 045f9bcc8a
2 changed files with 7 additions and 3 deletions

View File

@ -666,15 +666,14 @@ def _is_field_noneable(field: FieldInfo) -> bool:
return field.nullable
if not field.is_required():
default = getattr(field, "original_default", field.default)
if default is PydanticUndefined:
return False
if field.annotation is None or field.annotation is NoneType:
return True
if _is_optional_or_union(field.annotation):
for base in get_args(field.annotation):
if base is NoneType:
return True
if default is PydanticUndefined:
return False
return False
return False

View File

@ -3,6 +3,9 @@ from typing import Optional
import pytest
from sqlalchemy.exc import IntegrityError
from sqlmodel import Field, Session, SQLModel, create_engine
from typing_extensions import Annotated
from pydantic import AnyUrl, UrlConstraints
MoveSharedUrl = Annotated[AnyUrl, UrlConstraints(max_length=512, allowed_schemes=['smb', 'ftp','file'])]
def test_nullable_fields(clear_sqlmodel, caplog):
@ -49,6 +52,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="")
engine = create_engine("sqlite://", echo=True)
SQLModel.metadata.create_all(engine)
@ -77,6 +81,7 @@ def test_nullable_fields(clear_sqlmodel, caplog):
assert "str_default_str_nullable VARCHAR," in create_table_log
assert "str_default_ellipsis_non_nullable VARCHAR NOT NULL," in create_table_log
assert "str_default_ellipsis_nullable VARCHAR," in create_table_log
assert "annotated_any_url VARCHAR(512)," in create_table_log
# Test for regression in https://github.com/tiangolo/sqlmodel/issues/420