Skip to content

Commit

Permalink
Merge pull request #14 from pinecrew/feature-auto-id
Browse files Browse the repository at this point in the history
Automatic add id to all models
  • Loading branch information
citrux authored Oct 19, 2020
2 parents d88b8a5 + 3ae173e commit f7fa638
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install .[dev]
- name: Test with pytest
run: |
pytest
4 changes: 2 additions & 2 deletions joey/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def run():


@cli.command()
@click.argument('label', required=False)
@click.argument('label', required=False, default='auto')
@verbose
def revise(label='auto'):
def revise(label):
'''Create migration'''
subprocess.call(['alembic', 'revision', '-m', label, '--autogenerate'])

Expand Down
22 changes: 14 additions & 8 deletions joey/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,23 @@ def __new__(cls: type, name: str, bases: typing.Sequence[type], attrs: dict) ->
class Model(orm.Model, metaclass=BaseMetaclass):
__abstract__ = True

id = orm.Integer(primary_key=True)

DoesNotExist = NoMatch
MultipleObjectsReturned = MultipleMatches


def init(settings):
import databases
import sqlalchemy
class DB:
def __init__(self, settings):
import databases
import sqlalchemy

database = databases.Database(settings.SQLALCHEMY_DATABASE_URI)
metadata = sqlalchemy.MetaData()
engine = sqlalchemy.create_engine(str(database.url))
self.database = databases.Database(settings.SQLALCHEMY_DATABASE_URI)
self.metadata = sqlalchemy.MetaData()
self.engine = sqlalchemy.create_engine(str(self.database.url))

Model.__metadata__ = metadata
Model.__database__ = database
Model.__metadata__ = self.metadata
Model.__database__ = self.database

def init(settings):
return DB(settings)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"click==7.1.2",
"pyyaml==5.3.1",
],
extras_require={"dev": ["black==19.10b0", "pylint",], "sessions": ["pyjwt==1.7.1", "cryptography==3.0"]},
extras_require={"dev": ["black==19.10b0", "pylint", "pytest", "aiosqlite"], "sessions": ["pyjwt==1.7.1", "cryptography==3.0"]},
entry_points={'console_scripts': ['joey=joey.cli:main'],},
dependency_links=[],
include_package_data=True,
Expand Down
Empty file added tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'
46 changes: 46 additions & 0 deletions tests/test_auto_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import asyncio
import functools

import pytest
import orm
import databases
import sqlalchemy

import tests.settings as settings

from joey import db

db_context = db.init(settings)

@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = db_context.engine
metadata = db_context.metadata
metadata.create_all(engine)
yield
metadata.drop_all(engine)

class Example(db.Model):
description = orm.Text(allow_blank=True)

def async_adapter(wrapped_func):
"""
Decorator used to run async test cases.
"""

@functools.wraps(wrapped_func)
def run_sync(*args, **kwargs):
loop = asyncio.get_event_loop()
task = wrapped_func(*args, **kwargs)
return loop.run_until_complete(task)

return run_sync


@async_adapter
async def test_auti_id():
await Example.objects.create()

example = await Example.objects.get()
assert example.id == 1
assert example.description == ''

0 comments on commit f7fa638

Please sign in to comment.