Exemplo n.º 1
0
class TestDynamicResourceInRegistry(object):
    def setup_method(self, method):
        from rctk.resourceregistry import ResourceRegistry
        self.rr = ResourceRegistry()

    from rctk.resourceregistry import DynamicResource
    class DummyDynamicResource(DynamicResource):
        def __call__(self, elements):
            return elements[-1]

    def test_equality(self):
        """
           two dynamic resources with the same name get different registrations
        """
        r1 = self.DummyDynamicResource("unique")
        r2 = self.DummyDynamicResource("unique")
        n1 = self.rr.add(r1)
        n2 = self.rr.add(r2)
        assert n1 != n2

    def test_dynamics(self):
        r = self.DummyDynamicResource("name")
        n = self.rr.add(r)
        assert self.rr.get_resource(n, ['a', 'b', 'c']) == 'c'
        assert self.rr.get_resource(n, ['a', 'b', ]) == 'b'
Exemplo n.º 2
0
class TestResourceRegistry(object):
    def setup_method(self, method):
        from rctk.resourceregistry import ResourceRegistry
        self.rr = ResourceRegistry()

    def test_order(self):
        """ assert that add-order is preserved """
        from rctk.resourceregistry import BaseResource
        r1 = BaseResource("3", name="c")
        r2 = BaseResource("2", name="a")
        r3 = BaseResource("1", name="b")

        self.rr.add(r1)
        self.rr.add(r2)
        self.rr.add(r3)

        assert self.rr.names() == ["c", "a", "b"]

    def test_duplicates(self):
        """ duplicates are not added """
        from rctk.resourceregistry import BaseResource
        r1 = BaseResource("1", name="foo")
        r2 = BaseResource("1", name="foo")

        self.rr.add(r1)
        self.rr.add(r2)

        assert len(self.rr.names()) == 1

    def test_duplicate_named_resource(self):
        from rctk.resourceregistry import BaseResource
        r1 = BaseResource("x", name="foo")
        r2 = BaseResource("y", name="foo")

        n1 = self.rr.add(r1)
        n2 = self.rr.add(r2)

        assert n1 != n2 

    def test_retrieve_resource(self):
        from rctk.resourceregistry import BaseResource
        r1 = BaseResource("x", name="foo")
        r2 = BaseResource("y", name="foo")

        n1 = self.rr.add(r1)
        n2 = self.rr.add(r2)

        assert self.rr.get_resource(n1).data == r1.data
        assert self.rr.get_resource(n2).data == r2.data

    def test_singleton(self):
        from rctk.resourceregistry import getResourceRegistry

        rr1 = getResourceRegistry()
        rr2 = getResourceRegistry()

        assert rr1 is rr2