Skip to content

Commit 62d9388

Browse files
committed
Fixed tests for SQLAlchemy 2.1 [skip ci]
1 parent 5275164 commit 62d9388

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

‎tests/test_sqlalchemy.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
from typing import Any
1313
from .conftest import numpy as np
1414

15+
try:
16+
from sqlalchemy.dialects.postgresql import BitString
17+
sqlalchemy_version = 2.1
18+
except ImportError:
19+
sqlalchemy_version = 2
20+
1521
psycopg2_engine = create_engine('postgresql+psycopg2://localhost/pgvector_python_test')
1622
psycopg2_type_engine = create_engine('postgresql+psycopg2://localhost/pgvector_python_test')
1723

@@ -420,7 +426,7 @@ def test_select(self, engine: Engine) -> None:
420426
def test_select_orm(self, engine: Engine) -> None:
421427
with Session(engine) as session:
422428
session.add(Item(embedding=[2, 3, 3]))
423-
items = session.scalars(select(Item.embedding.l2_distance([1, 1, 1]))).all()
429+
items = session.scalars(select(Item.embedding.l2_distance([1, 1, 1]))).all() # type: ignore
424430
assert items == [3]
425431

426432
def test_avg(self, engine: Engine) -> None:
@@ -634,9 +640,15 @@ async def test_bit(self, engine: AsyncEngine) -> None:
634640

635641
async with async_session() as session:
636642
async with session.begin():
637-
# typing issue
638-
# https://github.com/MagicStack/py-pgproto/pull/32
639-
embedding = asyncpg.BitString('101') if engine == asyncpg_engine else '101' # type: ignore
643+
embedding: Any
644+
if sqlalchemy_version >= 2.1:
645+
embedding = BitString('101')
646+
elif engine == asyncpg_engine:
647+
# typing issue
648+
# https://github.com/MagicStack/py-pgproto/pull/32
649+
embedding = asyncpg.BitString('101') # type: ignore
650+
else:
651+
embedding = '101'
640652
session.add(Item(id=1, binary_embedding=embedding))
641653
item = await session.get_one(Item, 1)
642654
assert item.binary_embedding == embedding

0 commit comments

Comments
 (0)