Skip to content

Commit

Permalink
Merge branch 'release/0.14.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Jun 16, 2021
2 parents 8880537 + 4bbde37 commit 9a5682a
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Copy and paste this code in a file and run:
async with db_session.begin():
db_session.add_all([MyModel()])
result = await db_session.execute(sa.select(MyModel))
stmt = sa.select(MyModel)
result = await db_session.execute(stmt)
items = result.scalars().all()
data = {}
Expand Down
13 changes: 11 additions & 2 deletions aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from typing import cast, TYPE_CHECKING
import warnings

from aiohttp_sqlalchemy.constants import DEFAULT_KEY, SA_DEFAULT_KEY
from aiohttp_sqlalchemy.constants import SA_DEFAULT_KEY
from aiohttp_sqlalchemy.decorators import sa_decorator
from aiohttp_sqlalchemy.exceptions import DuplicateAppKeyError, \
DuplicateRequestKeyError
Expand All @@ -21,7 +22,7 @@
TSABinding = Tuple[TSessionFactory, str, bool]


__version__ = '0.14.0'
__version__ = '0.14.1'

__all__ = ['DuplicateAppKeyError', 'DuplicateRequestKeyError',
'SAAbstractView', 'SABaseView', 'SA_DEFAULT_KEY', 'sa_bind',
Expand Down Expand Up @@ -68,3 +69,11 @@ def setup(app: 'Application', bindings: 'Iterable[TSABinding]') -> None:

if middleware:
app.middlewares.append(sa_middleware(key))


def __getattr__(name):
if name == 'DEFAULT_KEY':
msg = "'DEFAULT_KEY' has been deprecated, use 'SA_DEFAULT_KEY'"
warnings.warn(msg, UserWarning, stacklevel=2)
return SA_DEFAULT_KEY
raise AttributeError(f"module {__name__} has no attribute {name}")
12 changes: 11 additions & 1 deletion aiohttp_sqlalchemy/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import warnings


SA_DEFAULT_KEY = 'sa_main'
DEFAULT_KEY = SA_DEFAULT_KEY


def __getattr__(name):
if name == 'DEFAULT_KEY':
msg = "'DEFAULT_KEY' has been deprecated, use 'SA_DEFAULT_KEY'"
warnings.warn(msg, UserWarning, stacklevel=2)
return SA_DEFAULT_KEY
raise AttributeError(f"module {__name__} has no attribute {name}")
4 changes: 2 additions & 2 deletions aiohttp_sqlalchemy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aiohttp.web import View
from typing import TYPE_CHECKING

from aiohttp_sqlalchemy.constants import DEFAULT_KEY
from aiohttp_sqlalchemy.constants import SA_DEFAULT_KEY

if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession
Expand All @@ -18,7 +18,7 @@ class SAAbstractView(AbstractView, metaclass=ABCMeta):
Suitable for a specific usage with multiple models.
"""
sa_session_key: 'str' = DEFAULT_KEY
sa_session_key: 'str' = SA_DEFAULT_KEY

def sa_session(self, key: 'Optional[str]' = None) -> 'AsyncSession':
return self.request[key or self.sa_session_key]
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Ruslan Ilyasovich Gilfanov'

# The full version, including alpha/beta/rc tags
release = '0.14.0'
release = '0.14.1'


# -- General configuration ---------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Copy and paste this code in a file and run:
async with db_session.begin():
db_session.add_all([MyModel()])
result = await db_session.execute(sa.select(MyModel))
stmt = sa.select(MyModel)
result = await db_session.execute(stmt)
items = result.scalars().all()
data = {}
Expand Down
3 changes: 2 additions & 1 deletion examples/single_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ async def main(request):

async with db_session.begin():
db_session.add_all([MyModel()])
result = await db_session.execute(sa.select(MyModel))
stmt = sa.select(MyModel)
result = await db_session.execute(stmt)
items = result.scalars().all()

data = {}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aiohttp-sqlalchemy"
version = "0.14.0"
version = "0.14.1"
description = "SQLAlchemy 1.4 / 2.0 support for aiohttp."
authors = [
"Ruslan Ilyasovich Gilfanov <ri.gilfanov@yandex.ru>",
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine

import aiohttp_sqlalchemy
from aiohttp_sqlalchemy import DEFAULT_KEY, sa_bind, sa_middleware
from aiohttp_sqlalchemy import SA_DEFAULT_KEY, sa_bind, sa_middleware


pytest_plugins = 'aiohttp.pytest_plugin'
Expand All @@ -29,7 +29,7 @@ def orm_session(orm_session_factory):

@pytest.fixture
def sa_main_middleware():
return sa_middleware(DEFAULT_KEY)
return sa_middleware(SA_DEFAULT_KEY)


@pytest.fixture
Expand Down
20 changes: 10 additions & 10 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import pytest
from sqlalchemy.ext.asyncio import AsyncSession

from aiohttp_sqlalchemy import DEFAULT_KEY, DuplicateRequestKeyError, sa_decorator
from aiohttp_sqlalchemy import SA_DEFAULT_KEY, DuplicateRequestKeyError, sa_decorator
from tests.conftest import ClassBasedView, ClassHandler, function_handler


async def test_duplicate_request_key_error(mocked_request, orm_session):
assert mocked_request.get(DEFAULT_KEY) is None
mocked_request[DEFAULT_KEY] = orm_session
assert mocked_request.get(DEFAULT_KEY) is orm_session
assert mocked_request.get(SA_DEFAULT_KEY) is None
mocked_request[SA_DEFAULT_KEY] = orm_session
assert mocked_request.get(SA_DEFAULT_KEY) is orm_session
with pytest.raises(DuplicateRequestKeyError):
await sa_decorator()(function_handler)(mocked_request)


async def test_decorated_class_based_view(mocked_request):
assert mocked_request.get(DEFAULT_KEY) is None
assert mocked_request.get(SA_DEFAULT_KEY) is None
await sa_decorator()(ClassBasedView.get)(mocked_request)
assert isinstance(mocked_request.get(DEFAULT_KEY), AsyncSession)
assert isinstance(mocked_request.get(SA_DEFAULT_KEY), AsyncSession)


async def test_decorated_class_handler(mocked_request):
assert mocked_request.get(DEFAULT_KEY) is None
assert mocked_request.get(SA_DEFAULT_KEY) is None
await sa_decorator()(ClassHandler.get)(mocked_request)
assert isinstance(mocked_request.get(DEFAULT_KEY), AsyncSession)
assert isinstance(mocked_request.get(SA_DEFAULT_KEY), AsyncSession)


async def test_decorated_function_handler(mocked_request):
assert mocked_request.get(DEFAULT_KEY) is None
assert mocked_request.get(SA_DEFAULT_KEY) is None
await sa_decorator()(function_handler)(mocked_request)
assert isinstance(mocked_request.get(DEFAULT_KEY), AsyncSession)
assert isinstance(mocked_request.get(SA_DEFAULT_KEY), AsyncSession)
12 changes: 6 additions & 6 deletions tests/test_middlewares.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import pytest
from sqlalchemy.ext.asyncio import AsyncSession

from aiohttp_sqlalchemy import DEFAULT_KEY, DuplicateRequestKeyError
from aiohttp_sqlalchemy import SA_DEFAULT_KEY, DuplicateRequestKeyError
from tests.conftest import function_handler


async def test_duplicate_request_key_error(sa_main_middleware, mocked_request, orm_session):
assert mocked_request.get(DEFAULT_KEY) is None
mocked_request[DEFAULT_KEY] = orm_session
assert mocked_request.get(DEFAULT_KEY) is orm_session
assert mocked_request.get(SA_DEFAULT_KEY) is None
mocked_request[SA_DEFAULT_KEY] = orm_session
assert mocked_request.get(SA_DEFAULT_KEY) is orm_session

with pytest.raises(DuplicateRequestKeyError):
await sa_main_middleware(mocked_request, function_handler)


async def test_sa_middleware(sa_main_middleware, mocked_request):
assert mocked_request.get(DEFAULT_KEY) is None
assert mocked_request.get(SA_DEFAULT_KEY) is None
await sa_main_middleware(mocked_request, function_handler)
assert isinstance(mocked_request.get(DEFAULT_KEY), AsyncSession)
assert isinstance(mocked_request.get(SA_DEFAULT_KEY), AsyncSession)
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy.sql.schema import MetaData
from aiohttp_sqlalchemy import DEFAULT_KEY, sa_init_db, sa_session
from aiohttp_sqlalchemy import SA_DEFAULT_KEY, sa_init_db, sa_session
import sqlalchemy as sa


Expand All @@ -9,5 +9,5 @@ async def test_sa_db_init(middlewared_app):


def test_sa_session(mocked_request, orm_session):
mocked_request[DEFAULT_KEY] = orm_session
mocked_request[SA_DEFAULT_KEY] = orm_session
assert sa_session(mocked_request) is orm_session
4 changes: 2 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from aiohttp_sqlalchemy import DEFAULT_KEY, SABaseView
from aiohttp_sqlalchemy import SA_DEFAULT_KEY, SABaseView


def test_sa_session(mocked_request, orm_session):
mocked_request[DEFAULT_KEY] = orm_session
mocked_request[SA_DEFAULT_KEY] = orm_session
view = SABaseView(mocked_request)
assert view.sa_session() is orm_session

0 comments on commit 9a5682a

Please sign in to comment.