mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +03:00
Add async mode API + tests
This commit is contained in:
parent
fb296788e3
commit
e09b93c21b
File diff suppressed because it is too large
Load Diff
|
@ -302,6 +302,33 @@ cdef class Provider(object):
|
|||
"""Return :py:class:`ProvidedInstance` provider."""
|
||||
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):
|
||||
"""Providing strategy implementation.
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import asyncio
|
||||
import random
|
||||
import unittest
|
||||
|
||||
from dependency_injector import containers, providers, errors
|
||||
|
||||
|
@ -570,3 +571,41 @@ class OverrideTests(AsyncTestCase):
|
|||
|
||||
self.assertIs(dependency1, 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())
|
||||
|
|
Loading…
Reference in New Issue
Block a user