def test_instance_factory(): class Service(object): foo_proxy = rpc_proxy("foo_service") bar_proxy = rpc_proxy("bar_service") class OtherService(object): pass # simplest case, no overrides instance = instance_factory(Service) assert isinstance(instance, Service) assert isinstance(instance.foo_proxy, Mock) assert isinstance(instance.bar_proxy, Mock) # no injections to replace instance = instance_factory(OtherService) assert isinstance(instance, OtherService) # override specific injection bar_injection = object() instance = instance_factory(Service, bar_proxy=bar_injection) assert isinstance(instance, Service) assert isinstance(instance.foo_proxy, Mock) assert instance.bar_proxy is bar_injection # non-applicable injection instance = instance_factory(Service, nonexist=object()) assert isinstance(instance, Service) assert isinstance(instance.foo_proxy, Mock) assert isinstance(instance.bar_proxy, Mock) assert not hasattr(instance, "nonexist")
def test_service(session): # create instance, providing the real session for the ``db`` injection service = instance_factory(Service, db=session) # verify ``save`` logic by querying the real database service.save("helloworld") assert session.query(Result.value).all() == [("helloworld",)]
def test_conversion_service(): # create instance # dependencies are replaced with mocks unless otherwise given - see # :class:`nameko.testing.services.instance_factory` service = instance_factory(ConversionService) # replace "math" service service.math.multiply.side_effect = lambda x, y: x * y service.math.divide.side_effect = lambda x, y: x / y # test inches_to_cm business logic assert service.inches_to_cm(300) == 762 service.math.multiply.assert_called_once_with(300, 2.54) # test cms_to_inches business logic assert service.cms_to_inches(762) == 300 service.math.divide.assert_called_once_with(762, 2.54)