Skip to content

Add UUID field converter #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
run: "scripts/build"
- name: "Run tests with SQLite"
env:
TEST_DATABASE_URI_SYNC: "sqlite:///test.db"
TEST_DATABASE_URI_ASYNC: "sqlite+aiosqlite:///test.db"
TEST_DATABASE_URI_SYNC: "sqlite:///test.db?check_same_thread=False"
TEST_DATABASE_URI_ASYNC: "sqlite+aiosqlite:///test.db?check_same_thread=False"
run: "scripts/test"
- name: "Run tests with PostgreSQL"
env:
Expand Down
10 changes: 5 additions & 5 deletions sqladmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ def handle_decimal_types(
# field_args["validators"].append(validators.MacAddress())
# return StringField(**field_args)

# @converts("dialects.postgresql.base.UUID")
# def conv_PGUuid(self, field_args: Dict, **kwargs: Any) -> Field:
# field_args.setdefault("label", "UUID")
# field_args["validators"].append(validators.UUID())
# return StringField(**field_args)
@converts("UUID")
def conv_uuid(self, field_args: Dict, **kwargs: Any) -> Field:
field_args.setdefault("label", "UUID")
field_args["validators"].append(validators.UUID())
return StringField(**field_args)

@converts("JSON")
def convert_JSON(self, field_args: dict, **extra: Any) -> Field:
Expand Down
15 changes: 6 additions & 9 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import create_async_engine

test_database_uri_sync = os.environ.get("TEST_DATABASE_URI_SYNC", "sqlite:///test.db")
test_database_uri_async = os.environ.get(
"TEST_DATABASE_URI_ASYNC", "sqlite+aiosqlite:///test.db"
test_database_uri_sync = os.environ.get(
"TEST_DATABASE_URI_SYNC", "sqlite:///test.db?check_same_thread=False"
)


connect_args = (
{"check_same_thread": False} if "sqlite" in test_database_uri_sync else {}
test_database_uri_async = os.environ.get(
"TEST_DATABASE_URI_ASYNC", "sqlite+aiosqlite:///test.db?check_same_thread=False"
)

sync_engine = create_engine(test_database_uri_sync, connect_args=connect_args)
async_engine = create_async_engine(test_database_uri_async, connect_args=connect_args)
sync_engine = create_engine(test_database_uri_sync)
async_engine = create_async_engine(test_database_uri_async)


class DummyData(dict): # pragma: no cover
Expand Down
13 changes: 13 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Text,
TypeDecorator,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
Expand Down Expand Up @@ -107,3 +108,15 @@ async def test_model_form_only(client: AsyncClient) -> None:
async def test_model_form_exclude(client: AsyncClient) -> None:
Form = await get_model_form(model=User, engine=engine, exclude=["status"])
assert len(Form()._fields) == 8


@pytest.mark.skipif(engine.name != "postgresql", reason="PostgreSQL only")
async def test_model_form_postgresql(client: AsyncClient) -> None:
class PostgresModel(Base):
__tablename__ = "postgres_model"

id = Column(Integer, primary_key=True)
uuid = Column(UUID)

Form = await get_model_form(model=PostgresModel, engine=engine)
assert len(Form()._fields) == 1