Skip to content

Commit

Permalink
fixes for some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed Dec 23, 2024
1 parent e631a49 commit 0a21c0f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
36 changes: 33 additions & 3 deletions tests/sqlalchemy/core/test_uuid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from uuid import UUID, uuid4

from sqlalchemy import Column, String
from sqlalchemy import Column, String, Text
from sqlalchemy.dialects.postgresql import UUID as PostgresUUID
from sqlalchemy.types import TypeDecorator
from fastapi import FastAPI
Expand All @@ -12,16 +12,46 @@

from ..conftest import Base

class UUIDType(TypeDecorator):
"""Platform-independent UUID type.
Uses PostgreSQL's UUID type, otherwise CHAR(36)
"""
impl = String
cache_ok = True

def __init__(self):
super().__init__(36)

def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
return dialect.type_descriptor(PostgresUUID(as_uuid=True))
else:
return dialect.type_descriptor(String(36))

def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == 'postgresql':
return value
else:
return str(value)

def process_result_value(self, value, dialect):
if value is None:
return value
if not isinstance(value, UUID):
return UUID(value)
return value


class UUIDModel(Base):
__tablename__ = "uuid_test"
id = Column(PostgresUUID(as_uuid=True), primary_key=True, default=uuid4)
id = Column(UUIDType(), primary_key=True, default=uuid4)
name = Column(String(255))


class CustomUUID(TypeDecorator):
"""Custom UUID type for testing."""

impl = String
cache_ok = True

Expand Down
39 changes: 35 additions & 4 deletions tests/sqlmodel/core/test_uuid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from uuid import UUID, uuid4

from sqlalchemy import Column, String
from sqlalchemy import Column, String, Text
from sqlalchemy.dialects.postgresql import UUID as PostgresUUID
from sqlalchemy.types import TypeDecorator
from fastapi import FastAPI
Expand All @@ -11,19 +11,49 @@
from fastcrud import crud_router, FastCRUD
from pydantic import ConfigDict

class UUIDType(TypeDecorator):
"""Platform-independent UUID type.
Uses PostgreSQL's UUID type, otherwise CHAR(36)
"""
impl = String
cache_ok = True

def __init__(self):
super().__init__(36)

def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
return dialect.type_descriptor(PostgresUUID(as_uuid=True))
else:
return dialect.type_descriptor(String(36))

def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == 'postgresql':
return value
else:
return str(value)

def process_result_value(self, value, dialect):
if value is None:
return value
if not isinstance(value, UUID):
return UUID(value)
return value


class UUIDModel(SQLModel, table=True):
__tablename__ = "uuid_test"
id: UUID = Field(
default_factory=uuid4,
sa_column=Column(PostgresUUID(as_uuid=True), primary_key=True),
sa_column=Column(UUIDType(), primary_key=True)
)
name: str = Field(sa_column=Column(String(255)))


class CustomUUID(TypeDecorator):
"""Custom UUID type for testing."""

impl = String
cache_ok = True

Expand All @@ -45,7 +75,8 @@ def process_result_value(self, value, dialect):
class CustomUUIDModel(SQLModel, table=True):
__tablename__ = "custom_uuid_test"
id: UUID = Field(
default_factory=uuid4, sa_column=Column(CustomUUID(), primary_key=True)
default_factory=uuid4,
sa_column=Column(CustomUUID(), primary_key=True)
)
name: str = Field(sa_column=Column(String(255)))

Expand Down

0 comments on commit 0a21c0f

Please sign in to comment.