Ejemplo n.º 1
0
    def test_get_one_exceptions(self):
        """ Test RessourceValue.one. """
        def values_source():
            return ["1st_obj", "2nd_obj"], ["1st_uriref", "2nd_uriref"]

        instance = ResourceValue(values_source, MockResource(), "some_name")
        self.assertRaises(surf.exc.CardinalityException, instance.get_one)
Ejemplo n.º 2
0
 def setUp(self):
     def values_source():
         return ["1st_obj", "2nd_obj"], ["1st_uriref", "2nd_uriref"]
     
     self.store = MockStore()
     self.value = ResourceValue(values_source, MockResource(self.store),
                                "some_name")
Ejemplo n.º 3
0
    def test_contains(self):
        """ Test ResourceValue.__contains__. """
        def values_source():
            return ["value_as_surf_object"], ["value_as_uriref"]

        instance = ResourceValue(values_source, MockResource(), "some_name")
        # Test basic membership check.
        self.assertTrue("value_as_surf_object" in instance)

        # Test that "to_rdf" is tried.
        self.assertTrue("value_as_uriref" in instance)
Ejemplo n.º 4
0
class TestResultValueQuery(unittest.TestCase):

    def setUp(self):
        def values_source():
            return ["1st_obj", "2nd_obj"], ["1st_uriref", "2nd_uriref"]
        
        self.store = MockStore()
        self.value = ResourceValue(values_source, MockResource(self.store),
                                   "some_name")

    def test_limit_offset(self):
        """ Test limit, offset. """
        
        self.store.expect_args({"limit" : 10, "offset" : 5})
        list(self.value.limit(10).offset(5))

    def test_full(self):
        """ Test full(). """
        
        self.store.expect_args({"full" : True, "only_direct" : True})
        list(self.value.full(only_direct = True))


    def test_order_desc(self):
        """ Test order, desc. """
        
        self.store.expect_args({"order" : "some_attr", "desc" : True})
        list(self.value.order("some_attr").desc())

    def test_get_by(self):
        """ Test get_by. """
        
        expected = [(surf.ns.FOAF["name"], "Jane", True)]
        self.store.expect_args({"get_by" : expected})
        list(self.value.get_by(foaf_name = "Jane"))

    def test_context(self):
        """ Test context. """
        
        self.store.expect_args({"context" : "my_context"})
        list(self.value.context("my_context"))

    def test_filter(self):
        """ Test filter. """
        
        self.store.expect_args({"filter" : [(surf.ns.FOAF["name"], "f", True)]})
        list(self.value.filter(foaf_name = "f"))

    def test_get_by_resource(self):
        """ Test that get_by accepts Resources as values. """
        
        resource = MockResource()
        expected = [(surf.ns.FOAF["knows"], resource.subject, True)]
        self.store.expect_args({"get_by" : expected})
        list(self.value.get_by(foaf_knows = resource))
Ejemplo n.º 5
0
    def __setattr__(self, name, value):
        """
        The `set` method - responsible for *caching* the `value` to the
        corresponding object attribute given by `name`.

        .. note: This method sets the state of the resource to *dirty*
        (the `resource` will be persisted if the `commit` `session` method
        is called).

        """
        def make_values_source(values, rdf_values):
            """ Return callable that returns stored values for this attr. """
            def setattr_values_source():
                """ Return stored values for this attribute. """

                return values, rdf_values

            return setattr_values_source

        predicate, direct = attr2rdf(name)
        if predicate:
            rdf_dict = direct and self.__rdf_direct or self.__rdf_inverse
            if not isinstance(value, list):
                value = [value]
            rdf_dict[predicate] = []
            rdf_dict[predicate].extend([self.to_rdf(val) for val in value])
            self.dirty = True

            if type(value) is ResourceValue:
                pass
            else:
                if type(value) not in [list, tuple]:
                    value = [value]

                value = map(value_to_rdf, value)
                values_source = make_values_source(value, rdf_dict[predicate])
                value = ResourceValue(values_source, self, name)

        object.__setattr__(self, name, value)
Ejemplo n.º 6
0
    def __getattr__(self, attr_name):
        """ Retrieve and cache attribute values.

        If attribute name is not in the "ns_predicate" form, an
        `AttributeError` will be raised.


        This method has no impact on the *dirty* state of the object.

        """

        predicate, direct = attr2rdf(attr_name)

        if not predicate:
            raise AttributeError('Not a predicate: %s' % attr_name)

        # Closure for lazy execution.
        def make_values_source(resource, predicate, direct, do_query):
            """ Return callable that loads and returns values. """
            def getattr_values_source():
                """ Load and return values for this attribute. """

                if do_query:
                    store = resource.session[resource.store_key]
                    # Request to triple store
                    values = store.get(resource, predicate, direct)
                    # Instantiate SuRF objects
                    surf_values = resource._lazy(values)
                else:
                    surf_values = []

                # Select triple dictionary for synchronization
                if direct:
                    rdf_dict = resource.__rdf_direct
                else:
                    rdf_dict = resource.__rdf_inverse

                # Save query_contexts
                for i in range(len(surf_values)):
                    if isinstance(surf_values[i], Resource):
                        surf_values[i].query_contexts = self.query_contexts

                # Initial synchronization
                rdf_dict[predicate] = [
                    resource.to_rdf(value) for value in surf_values
                ]

                return surf_values, rdf_dict[predicate]

            return getattr_values_source

        # If resource is fully loaded and still we're here
        # at __getattr__, this must be an empty attribute, so
        # no point querying triple store.
        do_query = not self.__full
        values_source = make_values_source(self, predicate, direct, do_query)

        attr_value = ResourceValue(values_source, self, attr_name)

        # Not using self.__setattr__, that would trigger loading of attributes
        object.__setattr__(self, attr_name, attr_value)
        self.dirty = False

        return attr_value