-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Added support for asyncpg as an optional engine #125
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"project_name": "app", | ||
"use_postgres": "y", | ||
"use_postgres": ["aiopg", "asyncpg", "n"], | ||
"use_redis": "n" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import pytest | ||
{%- if cookiecutter.use_postgres == 'y' %} | ||
{%- if cookiecutter.use_postgres == 'aiopg' %} | ||
from sqlalchemy import create_engine | ||
import aiopg.sa | ||
{%- endif %} | ||
|
||
from {{ cookiecutter.project_name }}.utils.common import PATH, get_config | ||
from {{ cookiecutter.project_name }}.app import init_app | ||
{%- if cookiecutter.use_postgres == 'y' %} | ||
from {{ cookiecutter.project_name }}.migrations import metadata | ||
from {{ cookiecutter.project_name }}.users.tables import users | ||
from {{cookiecutter.project_name}}.utils.common import PATH, get_config | ||
from {{cookiecutter.project_name}}.app import init_app | ||
{%- if cookiecutter.use_postgres != 'n' %} | ||
from {{cookiecutter.project_name}}.migrations import metadata | ||
from {{cookiecutter.project_name}}.users.tables import users | ||
{%- endif %} | ||
|
||
|
||
|
@@ -20,10 +20,9 @@ | |
test_config = get_config(['-c', TEST_CONFIG_PATH.as_posix()]) | ||
|
||
|
||
{%- if cookiecutter.use_postgres == 'y' %} | ||
{%- if cookiecutter.use_postgres != 'n' %} | ||
# helpers | ||
|
||
|
||
def get_db_url(config: dict) -> str: | ||
''' | ||
Generate a url for db connection from the config. | ||
|
@@ -58,7 +57,7 @@ def init_sample_data(engine) -> None: | |
'username': f'test#{idx}', | ||
'email': f'test#{idx}', | ||
'password': f'{idx}'} for idx in range(10) | ||
]) | ||
]) | ||
|
||
conn.execute(query) | ||
|
||
|
@@ -138,11 +137,20 @@ async def sa_engine(loop): | |
''' | ||
|
||
return await aiopg.sa.create_engine(**test_config['postgres']) | ||
|
||
|
||
@pytest.fixture | ||
async def postgres_engine(loop): | ||
{%- if cookiecutter.use_postgres == 'aiopg' %} | ||
import asyncpg | ||
return await asyncpg.connect(**test_config['postgres']) | ||
{%- endif %} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that for compatibiliti with tests u should use asyncpgsa and just rewrite sa_engine some like that: from sqlalchemy import create_engine
...
return await create_engine(dsn, echo=True, module=asyncpgsa) what do u think? |
||
{%- endif %} | ||
|
||
|
||
@pytest.fixture | ||
async def client(aiohttp_client{% if cookiecutter.use_postgres == 'y' %}, tables{% endif %}): | ||
async def client(aiohttp_client {% if cookiecutter.use_postgres != 'n' %} , tables{% endif %}): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did u add indent here? this code generate async def client(aiohttp_client , tables):
... right? |
||
''' | ||
The fixture for the initialize client. | ||
''' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[flake8] | ||
max-line-length = 79 | ||
exclude = env/* {% if cookiecutter.use_postgres == 'y' %}migrations/*{% endif %} | ||
exclude = env/* {% if cookiecutter.use_postgres != 'n' %}migrations/*{% endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{%- if cookiecutter.use_postgres == 'y' %} | ||
{%- if cookiecutter.use_postgres == 'aiopg' %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Types are important, so u need create types for |
||
from typing import List | ||
|
||
from aiopg.sa.result import RowProxy | ||
|
||
|
||
RowsProxy = List[RowProxy] | ||
{% endif %} | ||
{%- endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
{%- if cookiecutter.use_postgres == 'asyncpg' %} | ||
import asyncpg | ||
{%- endif %} | ||
{%- if cookiecutter.use_postgres == 'aiopg' %} | ||
from aiopg.sa import SAConnection | ||
from aiopg.sa.result import RowProxy | ||
{%- endif %} | ||
from {{cookiecutter.project_name}}.users.tables import users{%- if cookiecutter.use_postgres == 'asyncpg' %}, dialect {%- endif %} | ||
|
||
from {{ cookiecutter.project_name }}.users.tables import users | ||
__all__ = ['select_user_by_id', 'select_user_by_id_query'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
|
||
__all__ = ['select_user_by_id', ] | ||
|
||
|
||
async def select_user_by_id(conn: SAConnection, key: int) -> RowProxy: | ||
def select_user_by_id_query(key): | ||
query = users\ | ||
.select()\ | ||
.where(users.c.id == key)\ | ||
.order_by(users.c.id) | ||
cursor = await conn.execute(query) | ||
|
||
return query | ||
|
||
|
||
{%- if cookiecutter.use_postgres == 'asyncpg' %} | ||
async def select_user_by_id(conn, key): | ||
query = str(select_user_by_id_query(key).compile(dialect=dialect)) | ||
result = await conn.fetchrow(query) | ||
return result | ||
{%- endif %} | ||
{%- if cookiecutter.use_postgres == 'aiopg' %} | ||
async def select_user_by_id(conn: SAConnection, key: int) -> RowProxy: | ||
cursor = await conn.execute(select_user_by_id_query(key)) | ||
return await cursor.fetchone() | ||
{%- endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
from {{ cookiecutter.project_name }}.migrations import metadata | ||
from {{ cookiecutter.project_name }}.users.enums import UserGender | ||
from {{cookiecutter.project_name}}.migrations import metadata | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indents |
||
from {{cookiecutter.project_name}}.users.enums import UserGender | ||
|
||
|
||
__all__ = ['users', 'gender_enum', ] | ||
__all__ = ['users', 'gender_enum', 'dialect', ] | ||
|
||
|
||
gender_enum = postgresql.ENUM(UserGender) | ||
|
||
dialect = postgresql.dialect() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this part need only for asyncpg, so use if statement |
||
|
||
users = sa.Table( | ||
'users', metadata, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the whole project uses
{{ cookiecutter.project_name }}
with indent, so don't change that, please.In jinja2 documentation also use indents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry autoformatting, my bad.