Move container async resource tests to a separate module for Python 3.6+

This commit is contained in:
Roman Mogylatov 2020-12-07 19:53:20 -05:00
parent 45cd887a37
commit 86d2f655de
2 changed files with 71 additions and 67 deletions

View File

@ -0,0 +1,71 @@
"""Dependency injector dynamic container unit tests."""
import unittest2 as unittest
# Runtime import to get asyncutils module
import os
_TOP_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
'../',
)),
)
import sys
sys.path.append(_TOP_DIR)
from asyncutils import AsyncTestCase
from dependency_injector import (
containers,
providers,
)
class AsyncResourcesInitializationTest(AsyncTestCase):
@unittest.skipIf(sys.version_info[:2] <= (3, 5), 'Async test')
def test_async_init_resources(self):
async def _init1():
_init1.init_counter += 1
yield
_init1.shutdown_counter += 1
_init1.init_counter = 0
_init1.shutdown_counter = 0
async def _init2():
_init2.init_counter += 1
yield
_init2.shutdown_counter += 1
_init2.init_counter = 0
_init2.shutdown_counter = 0
class Container(containers.DeclarativeContainer):
resource1 = providers.Resource(_init1)
resource2 = providers.Resource(_init2)
container = Container()
self.assertEqual(_init1.init_counter, 0)
self.assertEqual(_init1.shutdown_counter, 0)
self.assertEqual(_init2.init_counter, 0)
self.assertEqual(_init2.shutdown_counter, 0)
self._run(container.init_resources())
self.assertEqual(_init1.init_counter, 1)
self.assertEqual(_init1.shutdown_counter, 0)
self.assertEqual(_init2.init_counter, 1)
self.assertEqual(_init2.shutdown_counter, 0)
self._run(container.shutdown_resources())
self.assertEqual(_init1.init_counter, 1)
self.assertEqual(_init1.shutdown_counter, 1)
self.assertEqual(_init2.init_counter, 1)
self.assertEqual(_init2.shutdown_counter, 1)
self._run(container.init_resources())
self._run(container.shutdown_resources())
self.assertEqual(_init1.init_counter, 2)
self.assertEqual(_init1.shutdown_counter, 2)
self.assertEqual(_init2.init_counter, 2)
self.assertEqual(_init2.shutdown_counter, 2)

View File

@ -2,19 +2,6 @@
import unittest2 as unittest import unittest2 as unittest
# Runtime import to get asyncutils module
import os
_TOP_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
'../',
)),
)
import sys
sys.path.append(_TOP_DIR)
from asyncutils import AsyncTestCase
from dependency_injector import ( from dependency_injector import (
containers, containers,
providers, providers,
@ -244,57 +231,3 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
self.assertEqual(_init1.shutdown_counter, 2) self.assertEqual(_init1.shutdown_counter, 2)
self.assertEqual(_init2.init_counter, 2) self.assertEqual(_init2.init_counter, 2)
self.assertEqual(_init2.shutdown_counter, 2) self.assertEqual(_init2.shutdown_counter, 2)
class AsyncResourcesInitializationTest(AsyncTestCase):
@unittest.skipIf(sys.version_info[:2] <= (3, 5), 'Async test')
def test_async_init_resources(self):
async def _init1():
_init1.init_counter += 1
yield
_init1.shutdown_counter += 1
_init1.init_counter = 0
_init1.shutdown_counter = 0
async def _init2():
_init2.init_counter += 1
yield
_init2.shutdown_counter += 1
_init2.init_counter = 0
_init2.shutdown_counter = 0
class Container(containers.DeclarativeContainer):
resource1 = providers.Resource(_init1)
resource2 = providers.Resource(_init2)
container = Container()
self.assertEqual(_init1.init_counter, 0)
self.assertEqual(_init1.shutdown_counter, 0)
self.assertEqual(_init2.init_counter, 0)
self.assertEqual(_init2.shutdown_counter, 0)
self._run(container.init_resources())
self.assertEqual(_init1.init_counter, 1)
self.assertEqual(_init1.shutdown_counter, 0)
self.assertEqual(_init2.init_counter, 1)
self.assertEqual(_init2.shutdown_counter, 0)
self._run(container.shutdown_resources())
self.assertEqual(_init1.init_counter, 1)
self.assertEqual(_init1.shutdown_counter, 1)
self.assertEqual(_init2.init_counter, 1)
self.assertEqual(_init2.shutdown_counter, 1)
self._run(container.init_resources())
self._run(container.shutdown_resources())
self.assertEqual(_init1.init_counter, 2)
self.assertEqual(_init1.shutdown_counter, 2)
self.assertEqual(_init2.init_counter, 2)
self.assertEqual(_init2.shutdown_counter, 2)
if __name__ == '__main__':
unittest.main()