def testGetFactoryContext(self): """ Tests the _get_factory_context() method """ class DummyClass(object): pass class ChildClass(DummyClass): pass # Assert the field doesn't exist yet self.assertRaises(AttributeError, getattr, DummyClass, constants.IPOPO_FACTORY_CONTEXT) # Convert the parent into a component DummyClass = decorators.ComponentFactory("dummy-factory")( decorators.Requires("field", "req")(DummyClass)) # Get the context class_context = decorators.get_factory_context(DummyClass) self.assertIsNotNone(decorators.get_factory_context(DummyClass), "Invalid factory context") # The child has a copy of the parent context child_context = decorators.get_factory_context(ChildClass) self.assertIsNot(child_context, class_context, "The child must have a copy of the context")
def testComponentFactory(self): """ Tests the @decorators.ComponentFactory decorator """ instance_name = "test" @decorators.Instantiate(instance_name) class DummyClass(object): pass class ChildClass(DummyClass): pass def method(): pass # Invalid target for invalid in (None, method, 123): self.assertRaises(TypeError, decorators.ComponentFactory("test"), invalid) # Transform the class into a component decorators.ComponentFactory()(DummyClass) # No name -> generated one parent_context = decorators.get_factory_context(DummyClass) self.assertEqual(parent_context.name, "DummyClassFactory", "Invalid generated name") # Transform the child class decorators.ComponentFactory()(ChildClass) child_context = decorators.get_factory_context(ChildClass) # Ensure the instantiation was not removed after inheritance self.assertIn(instance_name, parent_context.get_instances(), "Instance disappeared of parent") # Ensure the instantiation was not inherited self.assertNotIn(instance_name, child_context.get_instances(), "Instance kept in child")