Add tests for Dependency provider

This commit is contained in:
Roman Mogylatov 2021-02-12 08:29:02 -05:00
parent 99e404650f
commit b376836150
5 changed files with 6653 additions and 6463 deletions

File diff suppressed because it is too large Load Diff

View File

@ -311,7 +311,10 @@ class DynamicContainer(Container):
if self.parent: if self.parent:
return self.parent.parent_name return self.parent.parent_name
return self.declarative_parent.__name__ if self.declarative_parent:
return self.declarative_parent.__name__
return None
def assign_parent(self, parent): def assign_parent(self, parent):
"""Assign parent.""" """Assign parent."""

File diff suppressed because it is too large Load Diff

View File

@ -735,7 +735,13 @@ cdef class Dependency(Provider):
"""Return parent name.""" """Return parent name."""
if not self.__parent: if not self.__parent:
return None return None
return f'{self.__parent.parent_name}.{self.__parent.resolve_provider_name(self)}'
name = ''
if self.__parent.parent_name:
name += f'{self.__parent.parent_name}.'
name += f'{self.__parent.resolve_provider_name(self)}'
return name
def assign_parent(self, parent): def assign_parent(self, parent):
"""Assign parent.""" """Assign parent."""

View File

@ -360,8 +360,118 @@ class DependencyTests(unittest.TestCase):
self.provider.provided_by(providers.Factory(dict)) self.provider.provided_by(providers.Factory(dict))
self.assertRaises(errors.Error, self.provider) self.assertRaises(errors.Error, self.provider)
def test_call_not_overridden(self): def test_call_undefined(self):
self.assertRaises(errors.Error, self.provider) with self.assertRaises(errors.Error) as context:
self.provider()
self.assertEqual(str(context.exception), 'Dependency is not defined')
def test_call_undefined_error_message_with_container_instance_parent(self):
class UserService:
def __init__(self, database):
self.database = database
class Container(containers.DeclarativeContainer):
database = providers.Dependency()
user_service = providers.Factory(
UserService,
database=database, # <---- missing dependency
)
container = Container()
with self.assertRaises(errors.Error) as context:
container.user_service()
self.assertEqual(str(context.exception), 'Dependency "Container.database" is not defined')
def test_call_undefined_error_message_with_container_provider_parent_deep(self):
class Database:
pass
class UserService:
def __init__(self, db):
self.db = db
class Gateways(containers.DeclarativeContainer):
database_client = providers.Singleton(Database)
class Services(containers.DeclarativeContainer):
gateways = providers.DependenciesContainer()
user = providers.Factory(
UserService,
db=gateways.database_client,
)
class Container(containers.DeclarativeContainer):
gateways = providers.Container(Gateways)
services = providers.Container(
Services,
# gateways=gateways, # <---- missing dependency
)
container = Container()
with self.assertRaises(errors.Error) as context:
container.services().user()
self.assertEqual(
str(context.exception),
'Dependency "Container.services.gateways.database_client" is not defined',
)
def test_call_undefined_error_message_with_dependenciescontainer_provider_parent(self):
class UserService:
def __init__(self, db):
self.db = db
class Services(containers.DeclarativeContainer):
gateways = providers.DependenciesContainer()
user = providers.Factory(
UserService,
db=gateways.database_client, # <---- missing dependency
)
services = Services()
with self.assertRaises(errors.Error) as context:
services.user()
self.assertEqual(
str(context.exception),
'Dependency "Services.gateways.database_client" is not defined',
)
def test_assign_parent(self):
parent = providers.DependenciesContainer()
provider = providers.Dependency()
provider.assign_parent(parent)
self.assertIs(provider.parent, parent)
def test_parent_name(self):
container = containers.DynamicContainer()
provider = providers.Dependency()
container.name = provider
self.assertEqual(provider.parent_name, 'name')
def test_deepcopy_parent(self):
container = containers.DynamicContainer()
provider = providers.Dependency()
container.name = provider
copied = providers.deepcopy(container)
self.assertIs(container.name.parent, container)
self.assertIs(copied.name.parent, copied)
self.assertIsNot(container, copied)
self.assertIsNot(container.name, copied.name)
self.assertIsNot(container.name.parent, copied.name.parent)
def test_deepcopy(self): def test_deepcopy(self):
provider = providers.Dependency(int) provider = providers.Dependency(int)