def test_RegisterAndGetInstance(self): appScope = scope.Scope() childScope = scope.Scope(parent=appScope) childScope1 = scope.Scope(parent=appScope) appScope.runServices() self.assertEqual(2, len(appScope.children)) appScope.remove() self.assertEqual(0, len(appScope.children)) self.assertFalse(appScope.servicesStarted)
def test_find_instances(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency5), Bean(Dependency6)) res = appScope.getInstances(Dependency4) assert len(res) == 2 assert isinstance(res[0], Dependency5) assert isinstance(res[1], Dependency6)
def test_Inheritance_multi(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency5), Bean(Dependency7)) dep3 = appScope.getInstance(Dependency3) self.assertTrue(dep3, Dependency7) dep4 = appScope.getInstance(Dependency5) self.assertTrue(dep3, Dependency7)
def test_RegisterAndGetInstanceError(self): appScope = scope.Scope() appScope.registerBean(Dependency1, Dependency4, Dependency5, Dependency7) try: instance = appScope.getInstance(Dependency5) self.fail("Exception expected") except ScopetonException as e: pass
def test_RegisterAndGetInstanceCustomNames(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency1, name=Dependency3), Bean(Dependency4, name="aaa")) dep2 = appScope.getInstance(Dependency3) # type: Dependency2 dep3 = appScope.getInstance("aaa") self.assertNotEqual(dep2, dep3) self.assertEqual(Dependency1, dep2.__class__)
def test_Inheritance_err(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency5), Bean(Dependency7)) try: dep4 = appScope.getInstance(Dependency4) ok = False except: ok = True self.assertTrue(ok)
def test_RegisterAndGetInstance(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency2), Bean(Dependency3)) dep2 = appScope.getInstance(Dependency2) # type: Dependency2 dep3 = appScope.getInstance(Dependency3) dep3_single = appScope.getInstance(Dependency3) appScope.runServices() self.assertTrue(isinstance(dep3, Dependency3)) self.assertEqual(dep3, dep3_single) self.assertEqual(dep2.dep3, dep3_single) self.assertTrue(isinstance(dep2, Dependency2)) self.assertTrue(dep2.called) self.assertFalse(dep2.preDestroyCalled) appScope.stopServices() self.assertTrue(dep2.preDestroyCalled)
def test_same(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency7), Bean(Dependency7)) appScope.getInstance(Dependency7)
def test_mock(self): appScope = scope.Scope() appScope.registerBean(Bean(Mock(Dependency7), name=Dependency7)) appScope = scope.Scope() appScope.registerInstance(Dependency7, Mock(Dependency7))
def test_register_instance(self): appScope = scope.Scope() appScope.registerBean(Bean(Dependency7)) a = appScope.getInstance(Dependency7) b = appScope.getInstance(Dependency4) self.assertEqual(a, b)
def test_RegisterAndGetInstance(self): appScope = scope.Scope() appScope.registerBean(Dependency1, Dependency4, Dependency5, Dependency7) instance = appScope.getInstance(Dependency7) self.assertTrue(instance.called, "Constructor is not called!")