Skip to content

Commit

Permalink
Merge pull request #66 from xsnippet/decouple
Browse files Browse the repository at this point in the history
Use DI to inject database/conf where needed
  • Loading branch information
malor committed Jan 21, 2018
2 parents cd1c24c + 4d53bb3 commit 6b97bcb
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 237 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def find_packages(namespace):
'motor >= 1.1',
'python-jose >= 1.3.2',
'werkzeug >= 0.11.4',
'picobox >= 1.1.0, < 2',
],
tests_require=[
'pytest >= 2.8.7',
Expand Down
50 changes: 50 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Some pytest fixtures used around the code."""

import picobox
import pkg_resources
import pytest

from xsnippet.api.application import create_app
from xsnippet.api.conf import get_conf
from xsnippet.api.database import create_connection


@pytest.fixture(scope='function')
def testconf():
path = pkg_resources.resource_filename('xsnippet.api', 'default.conf')
conf = get_conf(path)
conf['auth'] = {'secret': 'SWORDFISH'}

# This flag exist to workaround permissions (which we currently lack of)
# and test PUT/PATCH requests to the snippet. Once permissions are
# implemented and the hack is removed from @checkpermissions decorator
# in resources/snippets.py - this silly flag must be thrown away.
conf['test'] = {'sudo': True}
return conf


@pytest.fixture(scope='function')
async def testdatabase(request, loop, testconf):
database = create_connection(testconf)

# Python 3.5 does not support yield statement inside coroutines, hence we
# cannot use yield fixtures here and are forced to use finalizers instead.
request.addfinalizer(lambda: loop.run_until_complete(database.client.drop_database(database)))
return database


@pytest.fixture(scope='function')
async def testapp(request, test_client, testconf, testdatabase):
box = picobox.Box()
box.put('conf', testconf)
box.put('database', testdatabase)

scope = picobox.push(box)
scope.__enter__()

# Python 3.5 does not support yield statement inside coroutines, hence we
# cannot use yield fixtures here and are forced to use finalizers instead.
# This is especially weird as Picobox provides convenient context manager
# and no plain functions, that's why manual triggering is required.
request.addfinalizer(lambda: scope.__exit__(None, None, None))
return await test_client(create_app())
33 changes: 0 additions & 33 deletions tests/resources/conftest.py

This file was deleted.

0 comments on commit 6b97bcb

Please sign in to comment.