mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +03:00
Move container async resource tests to a separate module for Python 3.6+
This commit is contained in:
parent
45cd887a37
commit
86d2f655de
71
tests/unit/containers/test_dynamic_async_resources_py36.py
Normal file
71
tests/unit/containers/test_dynamic_async_resources_py36.py
Normal 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)
|
|
@ -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()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user