def test_func_wrappers_support_flag_queries_but_has_no_flags(self): provider = func_as_provider(func=lambda: 'Lambada!', port='blah') self.assertIsNone(provider.get_provider_flag('blah', 'whatev')) self.assertEqual({}, provider.get_provider_flags('blah')) self.assertRaises(UnknownPort, provider.get_provider_flag, 'not_a_port', 'whatev') self.assertRaises(UnknownPort, provider.get_provider_flags, 'not_a_port')
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_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_service_needs_defined_with_interface_can_be_adapted(self): class MyServiceDeps(NeedsInterface): def some_data(self, data): # type: (str) -> str pass class MyService(Service): deps = MyServiceDeps() @provides def gimme(self, data): # type: (str) -> str return self.deps.some_data(data=data) service = MyService() self.assertRaises(DisconnectedPort, service.gimme, 'xyz') provider = func_as_provider(func=(lambda data: data.upper()), port='some_data') service.set_provider(port_name='some_data', provider=provider) self.assertEqual('YOLO', service.gimme(data='yolo'))
def test_creating_wrapper_with_invalid_port_name(self): msg = '"9_not_va!id_port" does not have required format for port names' with self.assertRaisesRegexp(InvalidPortName, msg): func_as_provider(func=lambda: 'Lambada!', port='9_not_va!id_port')