def test_load_configuration(self):
        """
        Test loading a configuration file
        """
        manager = BindingsManager.load_from_filepath("/Users/aaron/IdeaProjects/flx-injection/tests/test_flxbindings/resources/test_configuration.yaml")

        properties1 = manager.resolve("properties-1")
        component1 = manager.resolve("component-1")
        action1 = manager.resolve("action-1")

        assert properties1 is not None
        assert component1 is not None
        assert action1 is not None
 def setup(self):
     """
     Create new instance of BindingsManager for each test
     """
     self.manager = BindingsManager()
class TestEntityResolution(object):
    """
    Unit-tests for resolving entities
    """

    _logger = dynamiclogger()

    def setup(self):
        """
        Create new instance of BindingsManager for each test
        """
        self.manager = BindingsManager()

    def test_resolve_label(self):
        """
        An entity can be resolved by its label
        """
        entities = [entity_resolution.component1]
        self.__import_entities(entities)

        instance = self.manager.resolve("component1")
        assert instance == GenericFactory1.RESULT

    def test_resolve_instance(self):
        """
        [Not implemented] resolving entities using their instance
        """

    def test_resolve_factory(self):
        """
        An entity can be resolved by its factory
        """
        entities = [entity_resolution.component1]
        self.__import_entities(entities)

        instance = self.manager.resolve("test_flxbindings.fixture.generics.GenericFactory1")
        assert instance == GenericFactory1.RESULT

    def test_no_label_entity(self):
        """
        An entity with factory (but not label) can be resolved
        """
        entities = [entity_resolution.component4]
        self.__import_entities(entities)

        instance = self.manager.resolve("test_flxbindings.fixture.generics.GenericFactory1")
        assert instance == GenericFactory1.RESULT

    def test_no_interface_entity(self):
        """
        [Not implemented] resolving entities with no instance defined
        """

    def test_factory_only_entity(self):
        """
        [NA] convered by test_no_label_entity until interfaces implemented
        """

    @raises(KeyError)
    def test_duplicate_label_mapping(self):
        """
        Ensure there is an error when attempting to map more than one entity
        with the same label.
        """
        entities = [entity_resolution.component5_1, entity_resolution.component5_2]
        self.__import_entities(entities)

        instance = self.manager.resolve("foo5")
        assert instance == GenericFactory1.RESULT

    def test_share_interface_mapping(self):
        """
        [Not implemented] multiple entities with the same interface
        """

    def test_share_factory_mapping(self):
        """
        Ensure that duplicate factories can exist.
        """
        entities = [entity_resolution.component6_1, entity_resolution.component6_2]
        self.__import_entities(entities)

        instance = self.manager.resolve("test_flxbindings.fixture.generics.GenericFactory1")
        assert instance == GenericFactory1.RESULT

    def test_lookup_duplicate_interface(self):
        """
        [Not implemented] duplicate of test_share_interface_mapping?
        """

    def test_lookup_duplicate_factory(self):
        """
        Can resolve entities based on factories even if multiple exist
        """
        entities = [entity_resolution.component8_1, entity_resolution.component8_2]
        self.__import_entities(entities)

        instance = self.manager.resolve("test_flxbindings.fixture.generics.GenericFactory1")
        assert instance == GenericFactory1.RESULT

    def __import_entities(self, entities):
        """
        Imports a sequence of entities into the BindingsManager
        :param entities: list of BaseEntity
        """
        for entity in entities:
            self._logger.info("adding entity: %s" % entity)
            self.manager.add_entity(entity)
 def test_invalid_file(self):
     """
     Test importing a configuration file that isn't YAML
     """
     manager = BindingsManager.load_from_filepath("/Users/aaron/IdeaProjects/flx-injection/tests/test_flxbindings/resources/test_invalid_file.txt")
 def test_missing_filepath(self):
     """
     Test importing a config file that doesn't exist
     """
     BindingsManager.load_from_filepath("/tmp/foo.yaml")
Beispiel #6
0
"""
Unit-tests for the attribute binding mechanism
"""
from flxbindings.domain import Component
from flxbindings.logutil import dynamiclogger
from flxbindings.manager import BindingsManager
from test_flxbindings.fixture.generics import GenericFactory1

manager = BindingsManager()

component1 = Component("component1", "test_flxbindings.fixture.generics.GenericFactory1")
component1._singleton = False
component2 = Component("component2", "test_flxbindings.fixture.generics.GenericFactory2")
component2._singleton = False

manager.add_entity(component1)
manager.add_entity(component2)

class ExampleClient1(object):
    """
    Example class with bound attributes
    """
    binding1 = manager.bind("component1")
    binding2 = manager.bind("component2")

    def test_referencing_binding(self):
        """"""
        for _ in range(3):
            print self.binding1

class ExampleClient2(object):
class TestSingletonHandling(object):
    """
    Unit-tests to ensure singletons are handled correctly
    """
    _logger = dynamiclogger()

    def setup(self):
        """
        Create new instance of BindingsManager for each test
        """
        self.manager = BindingsManager()

    def test_singleton(self):
        """
        Ensure singletons only created once
        """
        num_inits = GenericFactory1.NUM_INITS

        component = Component("component1", "test_flxbindings.fixture.generics.GenericFactory1")
        self.manager.add_entity(component)

        self.manager.resolve("component1")
        self.manager.resolve("component1")
        self.manager.resolve("component1")

        assert GenericFactory1.NUM_INITS == num_inits + 1, GenericFactory1.NUM_INITS

    def test_non_singleton(self):
        """
        Ensure non-singletons created every time
        """
        num_inits = GenericFactory1.NUM_INITS

        component = Component("component2", "test_flxbindings.fixture.generics.GenericFactory1")
        component._singleton = False
        self.manager.add_entity(component)

        self.manager.resolve("component2")
        self.manager.resolve("component2")
        self.manager.resolve("component2")

        assert GenericFactory1.NUM_INITS == num_inits + 3, GenericFactory1.NUM_INITS

    def test_reference_singleton(self):
        """
        Ensure a referenced singleton only created once
        """
        num_inits1 = GenericFactory1.NUM_INITS
        num_inits2 = GenericFactory2.NUM_INITS

        component3_1 = Component("component3_1", "test_flxbindings.fixture.generics.GenericFactory1")
        component3_1._singleton = False
        component3_1._parameters["param1"] = EntityReference("component3_2")
        component3_2 = Component("component3_2", "test_flxbindings.fixture.generics.GenericFactory2")

        self.manager.add_entity(component3_1)
        self.manager.add_entity(component3_2)

        self.manager.resolve("component3_1")
        self.manager.resolve("component3_1")
        self.manager.resolve("component3_1")

        assert GenericFactory1.NUM_INITS == num_inits1 + 3, GenericFactory1.NUM_INITS
        assert GenericFactory2.NUM_INITS == num_inits2 + 1, GenericFactory2.NUM_INITS

    def test_owner_singleton(self):
        """
        Ensure a singleton owners' non-singleton dependencies not recreatred
        """
        num_inits1 = GenericFactory1.NUM_INITS
        num_inits2 = GenericFactory2.NUM_INITS

        component4_1 = Component("component4_1", "test_flxbindings.fixture.generics.GenericFactory1")
        component4_1._parameters["param1"] = EntityReference("component4_2")
        component4_2 = Component("component4_2", "test_flxbindings.fixture.generics.GenericFactory2")
        component4_2._singleton = False

        self.manager.add_entity(component4_1)
        self.manager.add_entity(component4_2)

        self.manager.resolve("component4_1")
        self.manager.resolve("component4_1")
        self.manager.resolve("component4_1")

        assert GenericFactory1.NUM_INITS == num_inits1 + 1, GenericFactory1.NUM_INITS
        assert GenericFactory2.NUM_INITS == num_inits2 + 1, GenericFactory2.NUM_INITS

    def test_non_singleton_dependency(self):
        """
        Ensure non-singleton dependencies are executed every time
        """
        num_inits1 = GenericFactory1.NUM_INITS
        num_inits2 = GenericFactory2.NUM_INITS

        component5_1 = Component("component5_1", "test_flxbindings.fixture.generics.GenericFactory1")
        component5_1._singleton = False
        component5_2 = Component("component5_2", "test_flxbindings.fixture.generics.GenericFactory2")
        component5_2._dependencies = [component5_1]

        self.manager.add_entity(component5_1)
        self.manager.add_entity(component5_2)

        self.manager.resolve("component5_2")
        self.manager.resolve("component5_2")
        self.manager.resolve("component5_2")

        assert GenericFactory1.NUM_INITS == num_inits1 + 1, GenericFactory1.NUM_INITS
        assert GenericFactory2.NUM_INITS == num_inits2 + 1, GenericFactory2.NUM_INITS

    def test_singleton_dependency(self):
        """
        Ensure singleton dependencies are not executed twice
        """
        num_inits1 = GenericFactory1.NUM_INITS
        num_inits2 = GenericFactory2.NUM_INITS

        component5_1 = Component("component5_1", "test_flxbindings.fixture.generics.GenericFactory1")
        component5_2 = Component("component5_2", "test_flxbindings.fixture.generics.GenericFactory2")
        component5_2._dependencies = [component5_1]
        component5_2._singleton = False

        self.manager.add_entity(component5_1)
        self.manager.add_entity(component5_2)

        self.manager.resolve("component5_2")
        self.manager.resolve("component5_2")
        self.manager.resolve("component5_2")

        assert GenericFactory1.NUM_INITS == num_inits1 + 1, GenericFactory1.NUM_INITS
        assert GenericFactory2.NUM_INITS == num_inits2 + 3, GenericFactory2.NUM_INITS

    def test_singleton_parameter_reference(self):
        """
        [Covered by test_reference_singleton?]
        Ensure singleton parameters are not executed twice
        """

    def test_non_singleton_parameter_reference(self):
        """
        Ensure non-singleton parameters are executed every time
        """
        num_inits1 = GenericFactory1.NUM_INITS
        num_inits2 = GenericFactory2.NUM_INITS

        component3_1 = Component("component3_1", "test_flxbindings.fixture.generics.GenericFactory1")
        component3_1._parameters["param1"] = EntityReference("component3_2")
        component3_2 = Component("component3_2", "test_flxbindings.fixture.generics.GenericFactory2")
        component3_2._singleton = False

        self.manager.add_entity(component3_1)
        self.manager.add_entity(component3_2)

        self.manager.resolve("component3_1")
        self.manager.resolve("component3_1")
        self.manager.resolve("component3_1")

        assert GenericFactory1.NUM_INITS == num_inits1 + 1, GenericFactory1.NUM_INITS
        assert GenericFactory2.NUM_INITS == num_inits2 + 1, GenericFactory2.NUM_INITS
class TestEntityErrors(object):
    """
    Unit-tests for various error cases related to entity resolution
    """
    _logger = dynamiclogger()

    def setup(self):
        """
        Create new instance of BindingsManager for each test
        """
        self.manager = BindingsManager()

    @raises(BindingsImportError)
    def test_factory_missing_top_package(self):
        """
        Test resolving an entity whose top-level package doesn't exist
        """
        component = Component("component1", "foo.module.ComponentFactory")
        self.manager.add_entity(component)
        self.manager.resolve("component1")

    @raises(BindingsImportError)
    def test_factory_missing_last_module(self):
        """
        Test resolving an entity whose inner-most module doesn't exist
        """
        component = Component("component2", "test_flxbindings.fixture.foo.GenericFactory1")
        self.manager.add_entity(component)
        self.manager.resolve("component2")

    @raises(BindingsImportError)
    def test_factory_missing_class(self):
        """
        Test resolving an entity whose class doesn't exist
        """
        component = Component("component3", "test_flxbindings.fixture.generics.GenericFactoryFoo")
        self.manager.add_entity(component)
        self.manager.resolve("component3")

    @raises(BindingsInstantiationException)
    def test_exception_factory_init(self):
        """
        Test resolving an entity who throws an exception in __init__
        """
        component = Component("component4", "test_flxbindings.fixture.generics.InitExceptionFactory")
        self.manager.add_entity(component)
        self.manager.resolve("component4")

    @raises(BindingsInstantiationException)
    def test_exception_factory_build(self):
        """
        Test resolving an entity who throws an exception in build
        """
        component = Component("component5", "test_flxbindings.fixture.generics.BuildExceptionFactory")
        self.manager.add_entity(component)
        self.manager.resolve("component5")

    @raises(BindingsInstantiationException)
    def test_exception_action_init(self):
        """
        Test running an action who throws an exception in __init__
        """
        component = Component("component6", "test_flxbindings.fixture.generics.RunExceptionAction")
        self.manager.add_entity(component)
        self.manager.resolve("component6")

    @raises(BindingsInstantiationException)
    def test_exception_action_run(self):
        """
        Test running an action who throws an exception in run
        """
        component = Component("component7", "test_flxbindings.fixture.generics.InitExceptionAction")
        self.manager.add_entity(component)
        self.manager.resolve("component7")

    def test_exception_singleton(self):
        """
        Test resolving a singleton that throws an exception, multiple times
        """
        num_exceptions = InitExceptionFactory.NUM_EXCEPTIONS

        component = Component("component8", "test_flxbindings.fixture.generics.InitExceptionFactory")
        self.manager.add_entity(component)

        for _ in range(3):

            try:
                self.manager.resolve("component8")

            except BindingsInstantiationException, e:
                import traceback; traceback.print_exc()
                pass

        assert InitExceptionFactory.NUM_EXCEPTIONS == num_exceptions + 3, InitExceptionFactory.NUM_EXCEPTIONS