Skip to content

Commit

Permalink
Extract tests to package with conditional extra tests import for Pyth…
Browse files Browse the repository at this point in the history
…on 3.5+
  • Loading branch information
rudyryk committed Jan 24, 2016
1 parent 8993596 commit 78f6f4f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -20,7 +20,7 @@ sdist/
var/
*.egg-info/
.installed.cfg
*.egg
*.egg*
*.ini

# PyInstaller
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -13,4 +13,3 @@ install: "pip install -e ."
# command to run tests
script:
- python setup.py test
- if [[ $TRAVIS_PYTHON_VERSION != '3.4' ]]; then python tests_3_5.py; fi
10 changes: 6 additions & 4 deletions peewee_async.py
Expand Up @@ -440,13 +440,13 @@ def _begin(self):
def commit(self, begin=True):
yield from _run_sql(self.db, "COMMIT")
if begin:
self._begin()
yield from self._begin()

@asyncio.coroutine
def rollback(self, begin=True):
yield from _run_sql(self.db, "ROLLBACK")
if begin:
self._begin()
yield from self._begin()

@asyncio.coroutine
def __aenter__(self):
Expand Down Expand Up @@ -669,11 +669,13 @@ class UnwantedSyncQueryError(Exception):


@asyncio.coroutine
def _run_sql(db, *args, **kwargs):
def _run_sql(db, operation, *args, **kwargs):
"""Run SQL operation (query or command) against database.
"""
assert db._async_conn, "Error, no async database connection."
cursor = yield from db._async_conn.cursor()
try:
yield from cursor.execute(*args, **kwargs)
yield from cursor.execute(operation, *args, **kwargs)
except Exception as e:
cursor.release()
raise e
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -32,4 +32,5 @@
'Programming Language :: Python :: 3',
],
test_suite='tests',
test_loader='unittest:TestLoader',
)
21 changes: 11 additions & 10 deletions tests.py → tests/__init__.py
Expand Up @@ -16,6 +16,9 @@
import peewee_async
import peewee_asyncext

# import logging
# logging.basicConfig(level=logging.DEBUG)


class ProxyDatabase(object):
"""Proxy database for deferred initialization.
Expand Down Expand Up @@ -113,8 +116,7 @@ class Meta:
database = database



class AsyncPostgresTestCase(unittest.TestCase):
class BaseAsyncPostgresTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls, *args, **kwargs):
# Sync connect
Expand Down Expand Up @@ -149,13 +151,10 @@ def tearDownClass(cls, *args, **kwargs):

def run_until_complete(self, coroutine):
result = self.loop.run_until_complete(coroutine)
self.assertTrue(result)
return result

#
# Test methods
#

class AsyncPostgresTestCase(BaseAsyncPostgresTestCase):
def test_get_obj(self):
# Async get
@asyncio.coroutine
Expand All @@ -165,7 +164,8 @@ def test():
self.assertEqual(obj.text, self.obj.text)
return obj

self.assertTrue(self.run_until_complete(test()))
obj = self.run_until_complete(test())
self.assertTrue(obj is not None)

def test_get_uuid_obj(self):
# Async get
Expand All @@ -176,7 +176,8 @@ def test():
self.assertEqual(obj.text, self.obj.text)
return obj

self.assertTrue(self.run_until_complete(test()))
obj = self.run_until_complete(test())
self.assertTrue(obj is not None)

def test_create_obj(self):
# Async create
Expand Down Expand Up @@ -351,5 +352,5 @@ def test():
self.run_until_complete(test())


if __name__ == '__main__':
unittest.main(argv=sys.argv)
if sys.version_info >= (3, 5):
from .tests_py35 import *
70 changes: 70 additions & 0 deletions tests/tests_py35.py
@@ -0,0 +1,70 @@
"""
peewee-async tests for Python 3.5+
==================================
These tests should be run only for Python 3.5+, older versions
will just fail with `SyntaxError` while importing this module.
"""
import peewee_async
from . import database,\
BaseAsyncPostgresTestCase,\
TestModel,\
UUIDTestModel

# Shortcuts
execute = peewee_async.execute
count = peewee_async.count
scalar = peewee_async.scalar
get_object = peewee_async.get_object
create_object = peewee_async.create_object
delete_object = peewee_async.delete_object
update_object = peewee_async.update_object
sync_unwanted = peewee_async.sync_unwanted


class FakeUpdateError(Exception):
"""Fake error while updating database.
"""
pass


class AsyncPostgresTransactionsTestCase(BaseAsyncPostgresTestCase):
def test_atomic_success(self):
"""Successful update in transaction.
"""
async def test():
with sync_unwanted(database):
obj = await create_object(TestModel, text='FOO')
obj_id = obj.id

async with database.async_atomic():
obj.text = 'BAR'
await update_object(obj)

res = await get_object(TestModel, TestModel.id == obj_id)
self.assertEqual(res.text, 'BAR')

self.run_until_complete(test())

def test_atomic_failed(self):
"""Failed update in transaction.
"""
async def test():
with sync_unwanted(database):
obj = await create_object(TestModel, text='FOO')
obj_id = obj.id

try:
async with database.async_atomic():
obj.text = 'BAR'
await update_object(obj)
raise FakeUpdateError()
except FakeUpdateError as e:
update_error = True
res = await get_object(TestModel, TestModel.id == obj_id)

self.assertTrue(update_error)
self.assertEqual(res.text, 'FOO')

self.run_until_complete(test())
185 changes: 0 additions & 185 deletions tests_3_5.py

This file was deleted.

0 comments on commit 78f6f4f

Please sign in to comment.