Skip to content

Commit 13a8228

Browse files
committed
add more tests for pg
1 parent 05f98d0 commit 13a8228

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

tests/fixtures/db_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ async def async_session_plain(async_engine):
5151

5252
@async_fixture(scope="class")
5353
async def async_session(async_session_plain):
54-
async with async_session_plain() as session:
54+
async with async_session_plain() as session: # type: AsyncSession
5555
yield session
56+
await session.rollback()

tests/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sqlalchemy.orm import declared_attr, relationship
77
from sqlalchemy.types import CHAR, TypeDecorator
88

9-
from tests.common import sqla_uri
9+
from tests.common import is_postgres_tests, sqla_uri
1010

1111

1212
class Base:
@@ -271,9 +271,9 @@ def python_type(self):
271271

272272

273273
db_uri = sqla_uri()
274-
if "postgres" in db_uri:
274+
if is_postgres_tests():
275275
# noinspection PyPep8Naming
276-
from sqlalchemy.dialects.postgresql import UUID as UUIDType
276+
from sqlalchemy.dialects.postgresql.asyncpg import AsyncpgUUID as UUIDType
277277
elif "sqlite" in db_uri:
278278
UUIDType = CustomUUIDType
279279
else:
@@ -283,10 +283,10 @@ def python_type(self):
283283

284284
class CustomUUIDItem(Base):
285285
__tablename__ = "custom_uuid_item"
286-
id = Column(UUIDType, primary_key=True)
286+
id = Column(UUIDType(as_uuid=True), primary_key=True)
287287

288288
extra_id = Column(
289-
UUIDType,
289+
UUIDType(as_uuid=True),
290290
nullable=True,
291291
unique=True,
292292
)

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,36 @@ async def test_join_by_relationships_does_not_duplicating_response_entities(
23802380
"meta": {"count": 1, "totalPages": 1},
23812381
}
23822382

2383+
async def test_sqla_filters_by_uuid_type(
2384+
self,
2385+
async_session: AsyncSession,
2386+
):
2387+
"""
2388+
This test checks if UUID fields allow filtering by UUID object
2389+
2390+
Make sure your UUID field allows native UUID filtering: `UUID(as_uuid=True)`
2391+
2392+
:param async_session:
2393+
:return:
2394+
"""
2395+
new_id = uuid4()
2396+
extra_id = uuid4()
2397+
item = CustomUUIDItem(
2398+
id=new_id,
2399+
extra_id=extra_id,
2400+
)
2401+
async_session.add(item)
2402+
await async_session.commit()
2403+
2404+
# noinspection PyTypeChecker
2405+
stmt = select(CustomUUIDItem)
2406+
# works because we set `as_uuid=True`
2407+
i = await async_session.scalar(stmt.where(CustomUUIDItem.id == new_id))
2408+
assert i
2409+
# works because we set `as_uuid=True`
2410+
i = await async_session.scalar(stmt.where(CustomUUIDItem.extra_id == extra_id))
2411+
assert i
2412+
23832413
@pytest.mark.parametrize("filter_kind", ["small", "full"])
23842414
async def test_filter_by_field_of_uuid_type(
23852415
self,

0 commit comments

Comments
 (0)