python-dependency-injector/tests/unit/providers/test_utils_py2_py3.py

57 lines
1.6 KiB
Python
Raw Normal View History

2016-11-04 16:48:26 +03:00
"""Dependency injector provider utils unit tests."""
import unittest
2016-11-04 16:48:26 +03:00
2016-11-04 17:13:21 +03:00
from dependency_injector import (
providers,
errors,
)
2016-11-04 16:48:26 +03:00
class IsProviderTests(unittest.TestCase):
def test_with_instance(self):
self.assertTrue(providers.is_provider(providers.Provider()))
def test_with_class(self):
self.assertFalse(providers.is_provider(providers.Provider))
def test_with_string(self):
2021-10-01 03:09:42 +03:00
self.assertFalse(providers.is_provider("some_string"))
2016-11-04 16:48:26 +03:00
def test_with_object(self):
self.assertFalse(providers.is_provider(object()))
def test_with_subclass_instance(self):
class SomeProvider(providers.Provider):
pass
self.assertTrue(providers.is_provider(SomeProvider()))
def test_with_class_with_getattr(self):
class SomeClass(object):
def __getattr__(self, _):
return False
self.assertFalse(providers.is_provider(SomeClass()))
class EnsureIsProviderTests(unittest.TestCase):
def test_with_instance(self):
provider = providers.Provider()
self.assertIs(providers.ensure_is_provider(provider), provider)
def test_with_class(self):
self.assertRaises(errors.Error,
providers.ensure_is_provider,
providers.Provider)
def test_with_string(self):
self.assertRaises(errors.Error,
providers.ensure_is_provider,
2021-10-01 03:09:42 +03:00
"some_string")
2016-11-04 16:48:26 +03:00
def test_with_object(self):
self.assertRaises(errors.Error, providers.ensure_is_provider, object())