Skip to content

Commit

Permalink
Added test for tortoise.run_async
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Sep 25, 2018
1 parent 1e791e8 commit 7e818c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tortoise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ async def init(
async def close_connections(cls):
for connection in cls._connections.values():
await connection.close()
cls._connections = {}

@classmethod
async def _reset_connections(cls):
await cls.close_connections()
cls._connections = {}

for app in cls.apps.values():
for model in app.values():
Expand Down
40 changes: 40 additions & 0 deletions tortoise/tests/test_run_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest import TestCase

from tortoise import Tortoise, run_async


class TestRunAsync(TestCase):
def setUp(self):
self.somevalue = 1

async def init(self):
await Tortoise.init(
db_url='sqlite://:memory:',
modules={'models': []}
)
self.somevalue = 2
self.assertNotEqual(Tortoise._connections, {})

async def init_raise(self):
await Tortoise.init(
db_url='sqlite://:memory:',
modules={'models': []}
)
self.somevalue = 3
self.assertNotEqual(Tortoise._connections, {})
raise Exception('Some exception')

def test_run_async(self):
self.assertEqual(Tortoise._connections, {})
self.assertEqual(self.somevalue, 1)
run_async(self.init())
self.assertEqual(Tortoise._connections, {})
self.assertEqual(self.somevalue, 2)

def test_run_async_raised(self):
self.assertEqual(Tortoise._connections, {})
self.assertEqual(self.somevalue, 1)
with self.assertRaises(Exception):
run_async(self.init_raise())
self.assertEqual(Tortoise._connections, {})
self.assertEqual(self.somevalue, 3)

0 comments on commit 7e818c4

Please sign in to comment.