def register_singleton(self, service_subject, identifier=None): """ Register a new singleton service in in the dependency injector This service can be : * A class that will be instantiated when called * An already instantiated instance that will be returned If no identifier is passed, it will be the class name in snake_case :param service_subject: The class or instance :type service_subject: mixed :param identifier: The identifier used to later retrieve a service singleton :type identifier: string :return: Return the newly created dependency entry :rtype: Service """ if identifier is None: identifier = get_service_subject_identifier(service_subject) service = Service(service_subject) service.is_singleton = True self._services[identifier] = service self._logger.debug( "Class %s registered as singleton with identifier %s", str(service_subject), identifier) return service
def test_is_singleton(self): service = Service(Mock()) service.is_singleton = True self.assertTrue(service.is_singleton)
def _register_singleton(self): fake_service = Service(Mock) fake_service.is_singleton = True self.injector._services['fake_service'] = fake_service