diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..718ff26 --- /dev/null +++ b/.github/workflows/test.yml @@ -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 diff --git a/joey/cli/__init__.py b/joey/cli/__init__.py index 6f2c8b9..feb5662 100644 --- a/joey/cli/__init__.py +++ b/joey/cli/__init__.py @@ -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']) diff --git a/joey/db.py b/joey/db.py index 90f45a4..ef36b4a 100644 --- a/joey/db.py +++ b/joey/db.py @@ -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) diff --git a/setup.py b/setup.py index bd78350..7aeff99 100644 --- a/setup.py +++ b/setup.py @@ -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, diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..a015fe3 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1 @@ +SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db' diff --git a/tests/test_auto_id.py b/tests/test_auto_id.py new file mode 100644 index 0000000..b55d7e9 --- /dev/null +++ b/tests/test_auto_id.py @@ -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 == ''