def test_add_arguments(self): service = Service(Mock) service.add_arguments(key1='value', key2='other_value') self.assertDictEqual(service._arguments, { 'key1': 'value', 'key2': 'other_value' })
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_type_instance(self): service = Service(Mock()) self.assertEqual(service.type, 'instance')
def test_add_arguments_returns_service(self): service = Service(Mock) result = service.add_arguments(key1='value', key2='other_value') self.assertEqual(service, result)
def test_add_argument_returns_service(self): service = Service(Mock) result = service.add_argument('key', 'value') self.assertEqual(service, result)
def test_add_argument(self): service = Service(Mock) service.add_argument('key', 'value') self.assertDictEqual(service.arguments, {'key': 'value'})
def test_subject_instance(self): subject = Mock() service = Service(subject) self.assertEqual(service.subject, subject)
def test_subject_class(self): service = Service(Mock) self.assertEqual(service.subject, Mock)
def test_is_singleton(self): service = Service(Mock()) service.is_singleton = True self.assertTrue(service.is_singleton)
def test_is_not_singleton(self): service = Service(Mock()) self.assertFalse(service.is_singleton)
def test_type_class(self): service = Service(Mock) self.assertEqual(service.type, 'class')
def test_get_instance(self): subject = Mock() fake_service = Service(subject) self.injector._services['fake_service'] = fake_service result = self.injector.get('fake_service') self.assertEqual(subject, result)
def test_get_class_identifier(self): fake_service = Service(Mock) self.injector._services['mock'] = fake_service subject1 = self.injector.get(Mock) self.assertIsInstance(subject1, Mock)
def test_get_uninstantiated(self): mock = Mock() fake_service = Service(mock) self.injector._services['fake_service'] = fake_service subject1 = self.injector.get_uninstantiated('fake_service') self.assertIs(subject1, mock)
def test_get_class(self): fake_service = Service(Mock()) self.injector._services['fake_service'] = fake_service subject1 = self.injector.get('fake_service') self.assertIsInstance(subject1, Mock)
def _register_singleton(self): fake_service = Service(Mock) fake_service.is_singleton = True self.injector._services['fake_service'] = fake_service