Skip to content

Commit bf75e07

Browse files
authored
Run tests against sqlalchemy 1.4 as well. (#44)
* Run tests against sqlalchemy 1.4 as well. * Run tests when noxfile changed. * Update test without async_sessionmaker in 1.4 * Fix tests to run on 1.4
1 parent 6886283 commit bf75e07

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515
- "pyproject.toml"
1616
- "poetry.lock"
1717
- ".github/workflows/test.yml"
18+
- "noxfile.py"
1819

1920
jobs:
2021
generate-jobs-tests:

noxfile.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
]
1919

2020

21-
@session(python=PYTHON_VERSIONS, name="Tests", tags=["tests"])
22-
def tests(session: Session) -> None:
21+
@session(python=PYTHON_VERSIONS, name="SQLAlchemy 2.0 Tests", tags=["tests"])
22+
def tests_sqlalchemy_latest(session: Session) -> None:
2323
session.run_always(
2424
"poetry",
2525
"run",
@@ -39,6 +39,29 @@ def tests(session: Session) -> None:
3939
)
4040

4141

42+
# No need for now to run 1.4 against all 5 python versions.
43+
@session(python="3.11", name="Sqlalchemy 1.4 Tests", tags=["tests"])
44+
def tests_sqlalchemy_1_4(session: Session) -> None:
45+
session.run_always(
46+
"poetry",
47+
"run",
48+
"pip",
49+
"install",
50+
"--upgrade",
51+
"pip",
52+
"setuptools",
53+
"wheel",
54+
external=True,
55+
)
56+
session.run_always("poetry", "install", external=True)
57+
session._session.install("sqlalchemy~=1.4")
58+
59+
session.run(
60+
"pytest",
61+
*COMMON_PYTEST_OPTIONS,
62+
)
63+
64+
4265
@session(name="Mypy", tags=["lint"])
4366
def mypy(session: Session) -> None:
4467
session.run_always("poetry", "install", external=True)

tests/conftest.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from sqlalchemy import orm
1919
from sqlalchemy.engine import Engine
2020
from sqlalchemy.ext import asyncio
21-
from sqlalchemy.ext.asyncio import AsyncAttrs, create_async_engine
21+
from sqlalchemy.ext.asyncio import create_async_engine
2222
from sqlalchemy.ext.asyncio.engine import AsyncEngine
2323
from testing.postgresql import Postgresql, PostgresqlFactory
2424

@@ -97,13 +97,13 @@ def async_engine(request) -> AsyncEngine:
9797

9898

9999
@pytest.fixture
100-
def async_sessionmaker(async_engine) -> asyncio.async_sessionmaker:
101-
return asyncio.async_sessionmaker(async_engine)
100+
def async_sessionmaker(async_engine):
101+
if SQLA2:
102+
return asyncio.async_sessionmaker(async_engine)
103+
else:
104+
return lambda **kwargs: asyncio.AsyncSession(async_engine, **kwargs)
102105

103106

104107
@pytest.fixture
105108
def base():
106-
class Base(AsyncAttrs, orm.DeclarativeBase):
107-
pass
108-
109-
return Base
109+
return orm.declarative_base()

tests/test_loader.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class Employee(base):
1414
name = Column(String, nullable=False)
1515
department_id = Column(Integer, ForeignKey("department.id"))
1616
department = relationship("Department", back_populates="employees")
17+
__mapper_args__ = {"eager_defaults": True}
1718

1819
class Department(base):
1920
__tablename__ = "department"
2021
id = Column(Integer, autoincrement=True, primary_key=True)
2122
name = Column(String, nullable=False)
2223
employees = relationship("Employee", back_populates="department")
24+
__mapper_args__ = {"eager_defaults": True}
2325

2426
return Employee, Department
2527

@@ -112,7 +114,7 @@ async def test_loader_with_async_session(
112114
async with async_engine.begin() as conn:
113115
await conn.run_sync(base.metadata.create_all)
114116

115-
async with async_sessionmaker() as session:
117+
async with async_sessionmaker(expire_on_commit=False) as session:
116118
e1 = Employee(name="e1")
117119
e2 = Employee(name="e2")
118120
d1 = Department(name="d1")
@@ -126,10 +128,10 @@ async def test_loader_with_async_session(
126128
e1.department = d2
127129
e2.department = d1
128130
await session.commit()
129-
d2_id = await d2.awaitable_attrs.id
131+
d2_id = d2.id
130132
department_loader_key = tuple(
131133
[
132-
await getattr(e1.awaitable_attrs, local.key)
134+
getattr(e1, local.key)
133135
for local, _ in Employee.department.property.local_remote_pairs
134136
]
135137
)

0 commit comments

Comments
 (0)