Add tests for .provided & .call()

This commit is contained in:
Roman Mogylatov 2021-02-05 08:48:25 -05:00
parent 288284aa9c
commit c9ab7d540d
2 changed files with 27 additions and 1 deletions

View File

@ -10,6 +10,7 @@ follows `Semantic versioning`_
Development version Development version
------------------- -------------------
- Add tests for ``Selector`` provider. - Add tests for ``Selector`` provider.
- Add tests for ``ProvidedInstance`` and ``MethodCaller`` providers.
- Update Makefile to make Python 3 tests to be a default test command: ``make test``. - Update Makefile to make Python 3 tests to be a default test command: ``make test``.
4.17.0 4.17.0

View File

@ -10,12 +10,20 @@ class Service:
self.value = value self.value = value
self.values = [self.value] self.values = [self.value]
def get_value(self): def __call__(self):
return self.value return self.value
def __getitem__(self, item): def __getitem__(self, item):
return self.values[item] return self.values[item]
def get_value(self):
return self.value
def get_closure(self):
def closure():
return self.value
return closure
class Client: class Client:
def __init__(self, value): def __init__(self, value):
@ -45,6 +53,15 @@ class Container(containers.DeclarativeContainer):
Client, Client,
value=service.provided.get_value.call(), value=service.provided.get_value.call(),
) )
client_method_closure_call = providers.Factory(
Client,
value=service.provided.get_closure.call().call(),
)
client_provided_call = providers.Factory(
Client,
value=service.provided.call(),
)
class ProvidedInstanceTests(unittest.TestCase): class ProvidedInstanceTests(unittest.TestCase):
@ -71,6 +88,14 @@ class ProvidedInstanceTests(unittest.TestCase):
client = self.container.client_method_call() client = self.container.client_method_call()
self.assertEqual(client.value, 'foo') self.assertEqual(client.value, 'foo')
def test_method_closure_call(self):
client = self.container.client_method_closure_call()
self.assertEqual(client.value, 'foo')
def test_provided_call(self):
client = self.container.client_provided_call()
self.assertEqual(client.value, 'foo')
def test_call_overridden(self): def test_call_overridden(self):
value = 'bar' value = 'bar'
with self.container.service.override(Service(value)): with self.container.service.override(Service(value)):