Add async mode API + tests

This commit is contained in:
Roman Mogylatov 2020-12-27 23:37:23 -05:00
parent fb296788e3
commit e09b93c21b
3 changed files with 5031 additions and 4608 deletions

File diff suppressed because it is too large Load Diff

View File

@ -302,6 +302,33 @@ cdef class Provider(object):
"""Return :py:class:`ProvidedInstance` provider.""" """Return :py:class:`ProvidedInstance` provider."""
return ProvidedInstance(self) return ProvidedInstance(self)
def enable_async_mode(self):
"""Enable async mode."""
self.__async_mode = ASYNC_MODE_ENABLED
def disable_async_mode(self):
"""Disable async mode."""
self.__async_mode = ASYNC_MODE_DISABLED
def reset_async_mode(self):
"""Reset async mode.
Provider will automatically define the mode on the next call.
"""
self.__async_mode = ASYNC_MODE_UNDEFINED
def is_async_mode_enabled(self):
"""Check if async mode is enabled."""
return self.__async_mode == ASYNC_MODE_ENABLED
def is_async_mode_disabled(self):
"""Check if async mode is disabled."""
return self.__async_mode == ASYNC_MODE_DISABLED
def is_async_mode_undefined(self):
"""Check if async mode is undefined."""
return self.__async_mode == ASYNC_MODE_UNDEFINED
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Providing strategy implementation. """Providing strategy implementation.

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import random import random
import unittest
from dependency_injector import containers, providers, errors from dependency_injector import containers, providers, errors
@ -570,3 +571,41 @@ class OverrideTests(AsyncTestCase):
self.assertIs(dependency1, dependency) self.assertIs(dependency1, dependency)
self.assertIs(dependency2, dependency) self.assertIs(dependency2, dependency)
class TestAsyncModeApi(unittest.TestCase):
def setUp(self):
self.provider = providers.Provider()
def test_default_mode(self):
self.assertFalse(self.provider.is_async_mode_enabled())
self.assertFalse(self.provider.is_async_mode_disabled())
self.assertTrue(self.provider.is_async_mode_undefined())
def test_enable(self):
self.provider.enable_async_mode()
self.assertTrue(self.provider.is_async_mode_enabled())
self.assertFalse(self.provider.is_async_mode_disabled())
self.assertFalse(self.provider.is_async_mode_undefined())
def test_disable(self):
self.provider.disable_async_mode()
self.assertFalse(self.provider.is_async_mode_enabled())
self.assertTrue(self.provider.is_async_mode_disabled())
self.assertFalse(self.provider.is_async_mode_undefined())
def test_reset(self):
self.provider.enable_async_mode()
self.assertTrue(self.provider.is_async_mode_enabled())
self.assertFalse(self.provider.is_async_mode_disabled())
self.assertFalse(self.provider.is_async_mode_undefined())
self.provider.reset_async_mode()
self.assertFalse(self.provider.is_async_mode_enabled())
self.assertFalse(self.provider.is_async_mode_disabled())
self.assertTrue(self.provider.is_async_mode_undefined())