Exemplo n.º 1
0
 def test_from_obj_with_sequence_unpacks_object_as_args(self):
     args = (Variable('x'), FOAF.name, "Alice")
     triple = Triple.from_obj(args)
     assert triple.subject == Variable('x')
     assert triple.predicate == FOAF.name
     assert triple.object == "Alice"
     assert_raises(TypeError, Triple.from_obj, 1)
Exemplo n.º 2
0
 def test_from_obj_with_collection_pattern(self):
     args = (Variable('x'), FOAF.name, ("Alice", Variable('restaurant')))
     triple = Triple.from_obj(args)
     assert triple.subject == Variable('x')
     assert triple.predicate == FOAF.name
     assert triple.object == ("Alice", Variable('restaurant'))
     assert isinstance(triple.object, CollectionPattern)
Exemplo n.º 3
0
 def test_method_args_replace_projected_variables(self):
     select = self.select.project(v.foo, v.bar)
     assert Variable('foo') in select.projection
     assert Variable('bar') in select.projection
     select = self.select.project(v.baz)
     assert Variable('baz') in select.projection
     assert Variable('foo') not in select.projection
     assert Variable('bar') not in select.projection
Exemplo n.º 4
0
 def test_order_by_method(self):
     a, b = Variable('a'), Variable('b')
     select = self.select.order_by(a)
     assert len(select._order_by) == 1
     assert a in select._order_by
     select = self.select.order_by(a, b)
     assert len(select._order_by) == 2
     assert b in select._order_by
Exemplo n.º 5
0
 def test_nested_collection_patterns(self):
     args = (Variable('x'), FOAF.name, ("Alice", (Variable('y'),
                                                  Variable('z'))))
     triple = Triple.from_obj(args)
     assert isinstance(triple.object, CollectionPattern)
     assert triple.object[0] == "Alice"
     assert isinstance(triple.object[1], CollectionPattern)
     assert triple.object[1][0] == Variable('y')
     assert triple.object[1][1] == Variable('z')
Exemplo n.º 6
0
 def add_property(self, key, descriptor):
     assert isinstance(key, basestring)
     assert isinstance(descriptor, Term), (descriptor, type(descriptor))
     key = Variable(key)
     self.names[descriptor] = key
     self.properties[key] = descriptor
     setattr(self.class_, key, descriptor)
Exemplo n.º 7
0
class TestCallingBuiltinOperators:
    ARG_LIST_FIXTURES = {
        'bound': [Variable('x')],
        'isIRI': [URIRef('mailto:[email protected]')],
        'isBlank': [Variable('x')],
        'isLiteral': ["test"],
        'str': [URIRef('mailto:[email protected]')],
        'lang': ["test"],
        'datatype': ["test"],
        'logical-or': [True, False],
        'logical-and': [True, False],
        'RDFTerm-equal': [Variable('x'), Variable('a')],
        'sameTerm': [Variable('x'), Variable('x')],
        'langMatches': ['en', 'FR'],
        'regex': [Variable('x'), "^bob", 'i']
    }

    def setup(self):
        self.op = BuiltinOperatorConstructor()

    def test_getattr_gets_callable(self):
        for op in helpers.BUILTIN_OPERATORS:
            operator = getattr(self.op, op.replace('-', '_'), None)
            assert callable(operator)

    def test_getitem_gets_callable(self):
        for op in helpers.BUILTIN_OPERATORS:
            operator = self.op[op]
            assert callable(operator)

    def test_operator_returns_expression(self):
        for op, arg_list in self.ARG_LIST_FIXTURES.iteritems():
            operator = self.op[op]
            value = operator(*arg_list)
            assert isinstance(value, Expression)
Exemplo n.º 8
0
 def execute(self, graph=None):
     if graph is None:
         graph = self.session.graph
     # manager = self.class_._manager
     mapper = self.class_._mapper
     variables = map(Variable, self.class_._manager.properties)
     triples = []
     for name, property in self.class_._manager:
         triples.extend(property.triples(mapper.identifier, Variable(name)))
     select = self.select.project(variables, append=True).where(*triples)
     results = select.execute(graph)
     return mapper.bind_results(graph, select, results)
Exemplo n.º 9
0
    def __init__(self,
                 class_,
                 type_or_select,
                 identifier=None,
                 properties=None):
        self.class_ = class_

        if identifier is None:
            identifier = Variable(class_.__name__)
        self.identifier = identifier

        if isinstance(type_or_select, Select):
            select = type_or_select
        else:
            rdf_type = type_or_select
            select = Select([identifier], [(identifier, is_a, rdf_type)])

        if identifier not in select.projection:
            raise InvalidRequestError("Select must include identifier.")

        self.select = select
        self.setup_class(properties or {})
Exemplo n.º 10
0
 def test_variables_arg_adds_variables(self):
     select = Select([Variable('foo')])
     assert Variable('foo') in select.projection
Exemplo n.º 11
0
 def test_default_identifier_is_class_name(self):
     assert self.mapper.identifier == Variable('Person')
Exemplo n.º 12
0
 def test_repr_args_print_with_repr(self):
     args = (Variable('x'), FOAF.name, "Alice")
     triple = Triple(*args)
     assert repr(triple) == "Triple(%r, %r, %r)" % args
Exemplo n.º 13
0
 def test_args_set_subject_predicate_object(self):
     triple = Triple(Variable('x'), FOAF.name, "Alice")
     assert triple.subject == Variable('x')
     assert triple.predicate == FOAF.name
     assert triple.object == "Alice"
Exemplo n.º 14
0
 def setup(self):
     self.triple = Triple(Variable('x'), FOAF.name, "Alice")
Exemplo n.º 15
0
 def test_iteration_yields_subject_predicate_object(self):
     items = list(self.triple)
     assert len(items) == 3
     assert items[0] == Variable('x')
     assert items[1] == FOAF.name
     assert items[2] == "Alice"
Exemplo n.º 16
0
 def test_variables_can_be_expressions(self):
     select = self.select.project(v.foo)
     assert Variable('foo') in select.projection
Exemplo n.º 17
0
 def test_from_obj_with_triple_returns_same_object(self):
     triple_a = Triple(Variable('x'), FOAF.name, "Alice")
     triple_b = Triple.from_obj(triple_a)
     assert triple_a is triple_b
Exemplo n.º 18
0
 def test_variables_can_be_rdflib_variables(self):
     select = self.select.project(Variable('foo'))
     assert Variable('foo') in select.projection
Exemplo n.º 19
0
 def test_variables_can_be_strings(self):
     select = self.select.project('foo')
     assert Variable('foo') in select.projection
Exemplo n.º 20
0
 def test_method_is_generative(self):
     a = Variable('a')
     select = self.select.order_by(a)
     assert select is not self.select