Skip to content

Commit

Permalink
Update examples and test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Sep 16, 2024
1 parent 5ec449a commit 6dad6f0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
28 changes: 14 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,43 +78,43 @@ Copy and paste this code in a file and run:
import aiohttp_sqlalchemy as ahsa
metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
class Base(orm.DeclarativeBase): ...
class MyModel(Base):
__tablename__ = 'my_table'
__tablename__ = "my_table"
pk = sa.Column(sa.Integer, primary_key=True)
timestamp = sa.Column(sa.DateTime(), default=datetime.now)
async def main(request):
sa_session = ahsa.get_session(request)
sa_session = aiohttp_sqlalchemy.get_session(request)
async with sa_session.begin():
sa_session.add(MyModel())
result = await sa_session.execute(sa.select(MyModel))
result = result.scalars()
data = {
instance.pk: instance.timestamp.isoformat()
for instance in result
}
data = {instance.pk: instance.timestamp.isoformat() for instance in result}
return web.json_response(data)
async def app_factory():
app = web.Application()
ahsa.setup(app, [
ahsa.bind('sqlite+aiosqlite:///'),
])
await ahsa.init_db(app, metadata)
aiohttp_sqlalchemy.setup(
app,
[
aiohttp_sqlalchemy.bind("sqlite+aiosqlite:///"),
],
)
await aiohttp_sqlalchemy.init_db(app, Base.metadata)
app.add_routes([web.get('/', main)])
app.add_routes([web.get("/", main)])
return app
if __name__ == '__main__':
if __name__ == "__main__":
web.run_app(app_factory(), port=8087)
28 changes: 14 additions & 14 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,45 @@ Copy and paste this code in a file and run:
import aiohttp_sqlalchemy as ahsa
metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)
class Base(orm.DeclarativeBase): ...
class MyModel(Base):
__tablename__ = 'my_table'
__tablename__ = "my_table"
pk = sa.Column(sa.Integer, primary_key=True)
timestamp = sa.Column(sa.DateTime(), default=datetime.now)
async def main(request):
sa_session = ahsa.get_session(request)
sa_session = aiohttp_sqlalchemy.get_session(request)
async with sa_session.begin():
sa_session.add(MyModel())
result = await sa_session.execute(sa.select(MyModel))
result = result.scalars()
data = {
instance.pk: instance.timestamp.isoformat()
for instance in result
}
data = {instance.pk: instance.timestamp.isoformat() for instance in result}
return web.json_response(data)
async def app_factory():
app = web.Application()
ahsa.setup(app, [
ahsa.bind('sqlite+aiosqlite:///'),
])
await ahsa.init_db(app, metadata)
aiohttp_sqlalchemy.setup(
app,
[
aiohttp_sqlalchemy.bind("sqlite+aiosqlite:///"),
],
)
await aiohttp_sqlalchemy.init_db(app, Base.metadata)
app.add_routes([web.get('/', main)])
app.add_routes([web.get("/", main)])
return app
if __name__ == '__main__':
if __name__ == "__main__":
web.run_app(app_factory(), port=8087)
Expand Down
6 changes: 3 additions & 3 deletions examples/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import aiohttp_sqlalchemy

metadata = sa.MetaData()
Base = orm.declarative_base(metadata=metadata)

class Base(orm.DeclarativeBase): ...


class MyModel(Base):
Expand Down Expand Up @@ -38,7 +38,7 @@ async def app_factory():
aiohttp_sqlalchemy.bind("sqlite+aiosqlite:///"),
],
)
await aiohttp_sqlalchemy.init_db(app, metadata)
await aiohttp_sqlalchemy.init_db(app, Base.metadata)

app.add_routes([web.get("/", main)])
return app
Expand Down
8 changes: 4 additions & 4 deletions examples/test_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime
from secrets import choice
from typing import Any

import sqlalchemy as sa
from aiohttp import web
Expand All @@ -14,11 +13,12 @@
sa_session,
)

metadata = sa.MetaData()
Base: Any = orm.declarative_base(metadata=metadata)
DB_URL = "sqlite+aiosqlite:///"


class Base(orm.DeclarativeBase): ...


class MyModel(Base):
__tablename__ = "my_table"
pk = sa.Column(sa.Integer, primary_key=True)
Expand Down Expand Up @@ -87,7 +87,7 @@ async def app_factory():
],
)
for key in KEY_LIST:
await aiohttp_sqlalchemy.init_db(app, metadata, key)
await aiohttp_sqlalchemy.init_db(app, Base.metadata, key)

app.add_routes([web.get("/handler_a", function_handler)])
app.add_routes([web.get("/handler_b", ClassOrganizedHandler().get)])
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

import pytest
import sqlalchemy as sa
from aiohttp import web
from aiohttp.hdrs import METH_GET
from aiohttp.test_utils import make_mocked_request
Expand Down Expand Up @@ -34,9 +33,10 @@ def wrong_key() -> str:


@pytest.fixture
def base_model() -> orm.Mapper[Any]:
metadata = sa.MetaData()
return orm.declarative_base(metadata=metadata) # type: ignore
def base_model() -> type[orm.DeclarativeBase]:
class BaseModel(orm.DeclarativeBase): ...

return BaseModel


@pytest.fixture
Expand Down

0 comments on commit 6dad6f0

Please sign in to comment.