2016-11-02 13:50:13 +03:00
|
|
|
"""Dependency injector injections unit tests."""
|
2016-11-02 12:25:39 +03:00
|
|
|
|
2021-03-06 04:17:28 +03:00
|
|
|
import unittest
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
|
|
|
|
|
|
class PositionalInjectionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_isinstance(self):
|
2016-11-04 14:06:47 +03:00
|
|
|
injection = providers.PositionalInjection(1)
|
|
|
|
self.assertIsInstance(injection, providers.Injection)
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
def test_get_value_with_not_provider(self):
|
2016-11-04 14:06:47 +03:00
|
|
|
injection = providers.PositionalInjection(123)
|
2018-11-08 23:55:01 +03:00
|
|
|
self.assertEqual(injection.get_value(), 123)
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
def test_get_value_with_factory(self):
|
2016-11-04 14:06:47 +03:00
|
|
|
injection = providers.PositionalInjection(providers.Factory(object))
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
obj1 = injection.get_value()
|
|
|
|
obj2 = injection.get_value()
|
|
|
|
|
|
|
|
self.assertIs(type(obj1), object)
|
|
|
|
self.assertIs(type(obj2), object)
|
|
|
|
self.assertIsNot(obj1, obj2)
|
|
|
|
|
2016-11-04 19:35:53 +03:00
|
|
|
def test_get_original_value(self):
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = providers.PositionalInjection(provider)
|
|
|
|
self.assertIs(injection.get_original_value(), provider)
|
|
|
|
|
2016-11-10 22:00:23 +03:00
|
|
|
def test_deepcopy(self):
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = providers.PositionalInjection(provider)
|
|
|
|
|
|
|
|
injection_copy = providers.deepcopy(injection)
|
|
|
|
|
|
|
|
self.assertIsNot(injection_copy, injection)
|
|
|
|
self.assertIsNot(injection_copy.get_original_value(),
|
|
|
|
injection.get_original_value())
|
|
|
|
|
|
|
|
def test_deepcopy_memo(self):
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = providers.PositionalInjection(provider)
|
|
|
|
injection_copy_orig = providers.PositionalInjection(provider)
|
|
|
|
|
|
|
|
injection_copy = providers.deepcopy(
|
|
|
|
injection, {id(injection): injection_copy_orig})
|
|
|
|
|
|
|
|
self.assertIs(injection_copy, injection_copy_orig)
|
|
|
|
self.assertIs(injection_copy.get_original_value(),
|
|
|
|
injection.get_original_value())
|
|
|
|
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
class NamedInjectionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_isinstance(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name", 1)
|
2016-11-04 14:06:47 +03:00
|
|
|
self.assertIsInstance(injection, providers.Injection)
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
def test_get_name(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name", 123)
|
|
|
|
self.assertEqual(injection.get_name(), "name")
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
def test_get_value_with_not_provider(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name", 123)
|
2018-11-08 23:55:01 +03:00
|
|
|
self.assertEqual(injection.get_value(), 123)
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
def test_get_value_with_factory(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name",
|
2016-11-04 14:06:47 +03:00
|
|
|
providers.Factory(object))
|
2016-11-02 12:25:39 +03:00
|
|
|
|
|
|
|
obj1 = injection.get_value()
|
|
|
|
obj2 = injection.get_value()
|
|
|
|
|
|
|
|
self.assertIs(type(obj1), object)
|
|
|
|
self.assertIs(type(obj2), object)
|
|
|
|
self.assertIsNot(obj1, obj2)
|
2016-11-04 19:35:53 +03:00
|
|
|
|
|
|
|
def test_get_original_value(self):
|
|
|
|
provider = providers.Factory(object)
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name", provider)
|
2016-11-04 19:35:53 +03:00
|
|
|
self.assertIs(injection.get_original_value(), provider)
|
2016-11-10 22:00:23 +03:00
|
|
|
|
|
|
|
def test_deepcopy(self):
|
|
|
|
provider = providers.Factory(object)
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name", provider)
|
2016-11-10 22:00:23 +03:00
|
|
|
|
|
|
|
injection_copy = providers.deepcopy(injection)
|
|
|
|
|
|
|
|
self.assertIsNot(injection_copy, injection)
|
|
|
|
self.assertIsNot(injection_copy.get_original_value(),
|
|
|
|
injection.get_original_value())
|
|
|
|
|
|
|
|
def test_deepcopy_memo(self):
|
|
|
|
provider = providers.Factory(object)
|
2021-10-01 03:09:42 +03:00
|
|
|
injection = providers.NamedInjection("name", provider)
|
|
|
|
injection_copy_orig = providers.NamedInjection("name", provider)
|
2016-11-10 22:00:23 +03:00
|
|
|
|
|
|
|
injection_copy = providers.deepcopy(
|
|
|
|
injection, {id(injection): injection_copy_orig})
|
|
|
|
|
|
|
|
self.assertIs(injection_copy, injection_copy_orig)
|
|
|
|
self.assertIs(injection_copy.get_original_value(),
|
|
|
|
injection.get_original_value())
|