Exemplo n.º 1
0
 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'
     })
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
 def test_type_instance(self):
     service = Service(Mock())
     self.assertEqual(service.type, 'instance')
Exemplo n.º 4
0
 def test_add_arguments_returns_service(self):
     service = Service(Mock)
     result = service.add_arguments(key1='value', key2='other_value')
     self.assertEqual(service, result)
Exemplo n.º 5
0
 def test_add_argument_returns_service(self):
     service = Service(Mock)
     result = service.add_argument('key', 'value')
     self.assertEqual(service, result)
Exemplo n.º 6
0
 def test_add_argument(self):
     service = Service(Mock)
     service.add_argument('key', 'value')
     self.assertDictEqual(service.arguments, {'key': 'value'})
Exemplo n.º 7
0
 def test_subject_instance(self):
     subject = Mock()
     service = Service(subject)
     self.assertEqual(service.subject, subject)
Exemplo n.º 8
0
 def test_subject_class(self):
     service = Service(Mock)
     self.assertEqual(service.subject, Mock)
Exemplo n.º 9
0
 def test_is_singleton(self):
     service = Service(Mock())
     service.is_singleton = True
     self.assertTrue(service.is_singleton)
Exemplo n.º 10
0
 def test_is_not_singleton(self):
     service = Service(Mock())
     self.assertFalse(service.is_singleton)
Exemplo n.º 11
0
 def test_type_class(self):
     service = Service(Mock)
     self.assertEqual(service.type, 'class')
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
 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)
Exemplo n.º 15
0
 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)
Exemplo n.º 16
0
 def _register_singleton(self):
     fake_service = Service(Mock)
     fake_service.is_singleton = True
     self.injector._services['fake_service'] = fake_service