def test_missing_dependency_reference(self):
     """
     Test resolving an entity whose dependency doesn't exist
     """
     component = Component("component10", "test_flxbindings.fixture.generics.GenericFactory1")
     component._dependencies = [EntityReference("foo-entity")]
     self.manager.add_entity(component)
     self.manager.resolve("component10")
 def test_missing_parameter_reference(self):
     """
     Test resolving an entities whose parameter reference doesn't exist
     """
     component = Component("component9", "test_flxbindings.fixture.generics.GenericFactory1")
     component._parameters["param1"] = EntityReference("foo-entity")
     self.manager.add_entity(component)
     self.manager.resolve("component9")
    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_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_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_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_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
Beispiel #8
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):