def init(cls): """ init Pylaas """ current_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) PylaasCore._init("{}/conf/definitions.yml".format(current_path))
def setup_method(self, method): if not self._service_id: raise RuntimeError('Attribute _service_id cannot be empty') # clear service instance PylaasCore.get_container().clear(self._service_id) self._service = PylaasCore.get_service(self._service_id)
def test_set_adapter_success(self): self.init_container() dummy = PylaasCore.get_service('dummy') assert not dummy.has_adapter() dummy.set_adapter(DummyAdapterAdapter) assert dummy.has_adapter() assert issubclass(dummy.get_adapter(), DummyAdapterAdapter)
def get_service(service_id) -> ServiceInterface: """ Get service Args: service_id (string): service id to instantiate from container Returns: ServiceInterface """ return PylaasCore.get_service(service_id)
def __init__(self) -> None: """ Init service """ module_name = self.__module__ filename = sys.modules[module_name].__file__ if path.exists(filename.replace('.py', '_adapter.py')): module_name += "_adapter" class_name = "{}Adapter".format(self.__class__.__name__) __import__(module_name) adapter = getattr(sys.modules[module_name], class_name) adapter = adapter() self._adapter = adapter else: self._adapter = None list_services = PylaasCore.get_container().get_definitions( )['services'] for service in list_services: setattr(self, '{}_service'.format(service), self._make_service_method(service))
def setup_method(self, method) -> None: """ setup any state tied to the execution of the given method in a class. setup_method is invoked for every test method of a class. Args: method (str): test method name Returns: None """ if not self._service_id: raise RuntimeError('Attribute _service_id cannot be empty') self._service = PylaasCore.get_service(self._service_id) # init adapter as a mock if necessary if self._service.has_adapter(): self._service.set_adapter(ServiceMock())
def get_service(): return PylaasCore.get_service(service_id)
def test__init_success(self): PylaasCore._init({'services': {'a service': 'data'}}) expected = {'configurations': {}, 'services': {'a service': 'data'}} assert expected == PylaasCore._container._definitions
def test_get_adapter_with_no_adapter_return_none(self): self.init_container() assert PylaasCore.get_service('dummy').get_adapter() is None
def test_has_adapter_with_adapter_return_true(self): self.init_container() dummy_adapter = PylaasCore.get_service('dummy_adapter') assert dummy_adapter.has_adapter()
def test_has_adapter_with_no_adapter_return_false(self): self.init_container() dummy = PylaasCore.get_service('dummy') assert not dummy.has_adapter()
def test_get_attr_success_return_service(self): PylaasCore._init(self.datasets_path + '/container/definitions.yml') dummy = PylaasCore.get_service('dummy') dummyC = dummy.test_magic_service_injection() assert isinstance(dummyC, DummyConfigurable)
def test_get_service(self): PylaasCore._init(self.datasets_path + '/container/definitions.yml') assert isinstance(PylaasCore.get_service('dummy'), Dummy)
def test_get_container(self): PylaasCore._init({'services': {'a service': 'data'}}) assert isinstance(PylaasCore.get_container(), Container)
def test_get_adapter_with_adapter_return_adapter(self): self.init_container() assert isinstance( PylaasCore.get_service('dummy_adapter').get_adapter(), DummyAdapterAdapter)
def init_container(self): PylaasCore._init(self.datasets_path + '/container/definitions.yml')
def test_new_raise_TypeError(self): """Test unique instance creation""" with pytest.raises(TypeError): PylaasCore()