Skip to content

Commit

Permalink
add more tests for pg
Browse files Browse the repository at this point in the history
  • Loading branch information
mahenzon committed Dec 21, 2023
1 parent 05f98d0 commit 13a8228
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion tests/fixtures/db_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ async def async_session_plain(async_engine):

@async_fixture(scope="class")
async def async_session(async_session_plain):
async with async_session_plain() as session:
async with async_session_plain() as session: # type: AsyncSession
yield session
await session.rollback()
10 changes: 5 additions & 5 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.orm import declared_attr, relationship
from sqlalchemy.types import CHAR, TypeDecorator

from tests.common import sqla_uri
from tests.common import is_postgres_tests, sqla_uri


class Base:
Expand Down Expand Up @@ -271,9 +271,9 @@ def python_type(self):


db_uri = sqla_uri()
if "postgres" in db_uri:
if is_postgres_tests():
# noinspection PyPep8Naming
from sqlalchemy.dialects.postgresql import UUID as UUIDType
from sqlalchemy.dialects.postgresql.asyncpg import AsyncpgUUID as UUIDType
elif "sqlite" in db_uri:
UUIDType = CustomUUIDType
else:
Expand All @@ -283,10 +283,10 @@ def python_type(self):

class CustomUUIDItem(Base):
__tablename__ = "custom_uuid_item"
id = Column(UUIDType, primary_key=True)
id = Column(UUIDType(as_uuid=True), primary_key=True)

extra_id = Column(
UUIDType,
UUIDType(as_uuid=True),
nullable=True,
unique=True,
)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_api/test_api_sqla_with_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,36 @@ async def test_join_by_relationships_does_not_duplicating_response_entities(
"meta": {"count": 1, "totalPages": 1},
}

async def test_sqla_filters_by_uuid_type(
self,
async_session: AsyncSession,
):
"""
This test checks if UUID fields allow filtering by UUID object
Make sure your UUID field allows native UUID filtering: `UUID(as_uuid=True)`
:param async_session:
:return:
"""
new_id = uuid4()
extra_id = uuid4()
item = CustomUUIDItem(
id=new_id,
extra_id=extra_id,
)
async_session.add(item)
await async_session.commit()

# noinspection PyTypeChecker
stmt = select(CustomUUIDItem)
# works because we set `as_uuid=True`
i = await async_session.scalar(stmt.where(CustomUUIDItem.id == new_id))
assert i
# works because we set `as_uuid=True`
i = await async_session.scalar(stmt.where(CustomUUIDItem.extra_id == extra_id))
assert i

@pytest.mark.parametrize("filter_kind", ["small", "full"])
async def test_filter_by_field_of_uuid_type(
self,
Expand Down

0 comments on commit 13a8228

Please sign in to comment.