class TestSimple(TestCase): def setUp(self): self._container = DependencyInjector() self._container.register(InnerClass) self._container.register(OuterClass) def test_get_outer_class(self): outer = self._container.get("outer_class") self.assertIsInstance(outer, OuterClass) def test_get_inner_class(self): outer = self._container.get("outer_class") self.assertIsInstance(outer.inner_class, InnerClass) def test_get_by_class(self): outer = self._container.get(OuterClass) self.assertIsInstance(outer, OuterClass) def test_has_service_true(self): result = self._container.has_service("outer_class") self.assertTrue(result) def test_has_service_false(self): result = self._container.has_service("unknown") self.assertFalse(result) def test_has_service_by_class(self): result = self._container.has_service(OuterClass) self.assertTrue(result) def test_not_singleton(self): outer1 = self._container.get("outer_class") outer2 = self._container.get("outer_class") self.assertIsNot(outer1, outer2)
class TestSingleton(TestCase): def setUp(self): self._container = DependencyInjector() self._container.register_singleton(OuterClass) def test_get_outer_class(self): outer = self._container.get("outer_class") self.assertIsInstance(outer, OuterClass) def test_is_singleton(self): outer1 = self._container.get("outer_class") outer2 = self._container.get("outer_class") self.assertIs(outer1, outer2)
class TestError(TestCase): def setUp(self): self._container = DependencyInjector() self._container.register(OuterClass) def test_unknown_parameter(self): with self.assertRaises(ArgumentNotFoundError): self._container.get("outer_class") def test_unknown_service(self): with self.assertRaises(ServiceNotFoundError): self._container.get("unknown_service")
def setUp(self): self.injector = DependencyInjector()
class TestDependencyInjector(TestCase): def setUp(self): self.injector = DependencyInjector() def test_register(self): result = self.injector.register(Mock, 'identifier') self.assertIsInstance(result, Service) def test_register_without_identifier(self): result = self.injector.register(Mock) self.assertIsInstance(result, Service) def test_register_singleton_returns_service(self): result = self.injector.register_singleton(Mock, 'identifier') self.assertIsInstance(result, Service) def test_register_singleton(self): result = self.injector.register_singleton(Mock, 'identifier') self.assertTrue(result.is_singleton) def test_register_singleton_without_identifier(self): result = self.injector.register_singleton(Mock) self.assertTrue(result.is_singleton) def test_has_service_returns_false(self): success = self.injector.has_service('no_service') self.assertFalse(success) def test_has_service_returns_true(self): self.injector._services['fake_service'] = None success = self.injector.has_service('fake_service') self.assertTrue(success) def test_has_service_with_class(self): self.injector._services['mock'] = None success = self.injector.has_service(Mock) self.assertTrue(success) def test_get_error(self): with self.assertRaises(Exception): self.injector.get('no_service') def test_get_singleton_class(self): self._register_singleton() subject1 = self.injector.get('fake_service') self.assertIsInstance(subject1, Mock) def test_get_singleton_same_instance(self): self._register_singleton() subject1 = self.injector.get('fake_service') subject2 = self.injector.get('fake_service') self.assertEqual(subject1, subject2) def _register_singleton(self): fake_service = Service(Mock) fake_service.is_singleton = True self.injector._services['fake_service'] = fake_service 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 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_identifier(self): fake_service = Service(Mock) self.injector._services['mock'] = fake_service subject1 = self.injector.get(Mock) self.assertIsInstance(subject1, Mock) 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 setUp(self): self._container = DependencyInjector() self._container.register_singleton(OuterClass)
def setUp(self): self._container = DependencyInjector() self._container.register(InnerClass) self._container.register(OuterClass)