def attach_mock_provider(consumer, ports): assert isinstance(consumer, INeed) assert isinstance(ports, (list, dict)) invalid_ports = set(ports).difference(consumer.get_needs()) if invalid_ports: raise UnknownPort('Invalid ports for {} - {}'.format( consumer, ', '.join(invalid_ports))) # create the underlying object that will hold all the mock functions for the specified ports template_funcs = get_template_funcs(service=consumer) template_func_subset = {port: template_funcs[port] for port in ports} template_class = type('Mock' + consumer.__class__.__name__, (), template_func_subset) provider_impl = mock.create_autospec(spec=template_class, spec_set=True, instance=True) # supply return_value if provider if isinstance(ports, dict): for port, value in ports.iteritems(): getattr(provider_impl, port).return_value = value provider = object_as_provider(provider=provider_impl, ports=ports) auto_wire(components=[consumer, provider]) return provider_impl
def test_wiring_domain_to_a_service(self): x = 1234 y = 98716 expected_values = self._get_expected_values_of_D_domain(x=x, y=y) _, _, _, D = self._get_service_and_domain_classes() domain = D() provider = self._get_provider_service(x, y)() auto_wire([domain, provider]) self.assertEqual(expected_values['a1'], domain.a1()) self.assertEqual(expected_values['b2'], domain.b2())
def test_connecting_service_to_a_func_converted_to_a_provider(self): def some_func(): return 'yolo' service = MyTestService() dep_provider = func_as_provider(func=some_func, port='external_dep') # before wiring self.assertRaises(DisconnectedPort, service.call_me_maybe) # after wiring auto_wire([service, dep_provider]) self.assertEqual('yolo', service.call_me_maybe())
def test_connecting_service_to_a_lambda_converted_to_a_provider(self): service = MyTestService() dep_provider = func_as_provider(func=lambda: 'Lambada!', port='external_dep') # before wiring self.assertRaises(DisconnectedPort, service.call_me_maybe) # after wiring auto_wire([service, dep_provider]) self.assertEqual('Lambada!', service.call_me_maybe()) self.assertRaises(UnknownPort, dep_provider.get_provider_func, 'not_a_port')
def test_auto_wiring_of_ports_from_a_collection_of_component_instances( self): A, B, C = self.get_services() a = A() b = B() c = C() auto_wire([a, b, c]) self.assertEqual(b, a.get_provider('b1')) self.assertEqual(c, b.get_provider('c1')) self.assertRaises(DisconnectedPort, a.get_provider, 'x') self.assertRaises(DisconnectedPort, b.get_provider, 'y')
def test_connecting_service_to_a_classmethod_converted_to_a_provider(self): class Deppy(object): @classmethod def wowza(cls): return cls.__name__ service = MyTestService() dep_provider = func_as_provider(func=Deppy.wowza, port='external_dep') # before wiring self.assertRaises(DisconnectedPort, service.call_me_maybe) # after wiring auto_wire([service, dep_provider]) self.assertEqual('Deppy', service.call_me_maybe())
def test_connecting_service_to_class_with_staticmethod_converted_to_a_provider( self): class Deppy(object): @staticmethod def external_dep(): return 'Sassy' service = MyTestService() dep_provider = object_as_provider(provider=Deppy, ports=['external_dep']) # before wiring self.assertRaises(DisconnectedPort, service.call_me_maybe) # after wiring auto_wire([service, dep_provider]) self.assertEqual('Sassy', service.call_me_maybe())
def test_wiring_domain_to_a_domain(self): x = 1234 y = 98716 expected_values = self._get_expected_values_of_D_domain(x=x, y=y) _, _, _, D = self._get_service_and_domain_classes() domain = D() ProviderService = self._get_provider_service(x, y) class ProviderDomain(Domain): __services__ = [ProviderService] __provides__ = ['x', 'y'] auto_wire([domain, ProviderDomain()]) self.assertEqual(expected_values['a1'], domain.a1()) self.assertEqual(expected_values['b2'], domain.b2())
def test_connecting_service_to_an_obj_converted_to_a_provider(self): class Regurgitator(object): def __init__(self, value): self.value = value def external_dep(self): return self.value service = MyTestService() provider = Regurgitator('kapow!') dep_provider = object_as_provider(provider=provider, ports=['external_dep']) # before wiring self.assertRaises(DisconnectedPort, service.call_me_maybe) # after wiring auto_wire([service, dep_provider]) self.assertEqual('kapow!', service.call_me_maybe())
def test_wiring_service_to_a_domain(self): x = 1234 y = 98716 expected_values = self._get_expected_values_of_D_domain(x=x, y=y) _, _, _, D = self._get_service_and_domain_classes() class MultiplierService(Service): deps = Needs(['a1', 'b2']) @provides def multiplied(self): return self.deps.a1() * self.deps.b2() domain = D() provider = self._get_provider_service(x, y)() multiplier_service = MultiplierService() auto_wire([domain, provider, multiplier_service]) self.assertEqual(expected_values['a1'], domain.a1()) self.assertEqual(expected_values['b2'], domain.b2()) self.assertEqual(expected_values['a1'] * expected_values['b2'], multiplier_service.multiplied())
def test_instances_of_same_domain_are_isolated(self): x = 1234 y = 98716 expected_values = self._get_expected_values_of_D_domain(x=x, y=y) _, _, _, D = self._get_service_and_domain_classes() domain = D() another_domain = D() provider = self._get_provider_service(x, y)() auto_wire([domain, provider]) # first instance wired self.assertEqual(expected_values['a1'], domain.a1()) self.assertEqual(expected_values['b2'], domain.b2()) # other instance remains disconnected with self.assertRaisesRegexp(DisconnectedPort, 'Port .+ has not been connected'): another_domain.a1() with self.assertRaisesRegexp(DisconnectedPort, 'Port .+ has not been connected'): another_domain.b2()