summaryrefslogtreecommitdiff
path: root/test/test_initbindings.py
blob: f3228ecbc2c9723c5c16c54ac761c6d53345e70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

from nose import SkipTest

from rdflib.plugins.sparql import prepareQuery


from rdflib import ConjunctiveGraph, URIRef, Literal, Namespace, Variable
g = ConjunctiveGraph()


def testStr():
    a = set(g.query("SELECT (STR(?target) AS ?r) WHERE { }", initBindings={'target': URIRef('example:a')}))
    b = set(g.query("SELECT (STR(?target) AS ?r) WHERE { } VALUES (?target) {(<example:a>)}"))
    assert a==b, "STR: %r != %r"%(a,b)

def testIsIRI():
    a = set(g.query("SELECT (isIRI(?target) AS ?r) WHERE { }", initBindings={'target': URIRef('example:a')}))
    b = set(g.query("SELECT (isIRI(?target) AS ?r) WHERE { } VALUES (?target) {(<example:a>)}"))
    assert a==b, "isIRI: %r != %r"%(a,b)

def testIsBlank():
    a = set(g.query("SELECT (isBlank(?target) AS ?r) WHERE { }", initBindings={'target': URIRef('example:a')}))
    b = set(g.query("SELECT (isBlank(?target) AS ?r) WHERE { } VALUES (?target) {(<example:a>)}"))
    assert a==b, "isBlank: %r != %r"%(a,b)

def testIsLiteral():
    a = set(g.query("SELECT (isLiteral(?target) AS ?r) WHERE { }", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT (isLiteral(?target) AS ?r) WHERE { } VALUES (?target) {('example')}"))
    assert a==b, "isLiteral: %r != %r"%(a,b)

def testUCase():
    a = set(g.query("SELECT (UCASE(?target) AS ?r) WHERE { }", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT (UCASE(?target) AS ?r) WHERE { } VALUES (?target) {('example')}"))
    assert a==b, "UCASE: %r != %r"%(a,b)

def testNoFunc():
    a = set(g.query("SELECT ?target WHERE { }", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } VALUES (?target) {('example')}"))
    assert a==b, "no func: %r != %r"%(a,b)

def testOrderBy():
    a = set(g.query("SELECT ?target WHERE { } ORDER BY ?target", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } ORDER BY ?target VALUES (?target) {('example')}"))
    assert a==b, "orderby: %r != %r"%(a,b)

def testOrderByFunc():
    a = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target VALUES (?target) {('example')} "))
    assert a==b, "orderbyFunc: %r != %r"%(a,b)

def testNoFuncLimit():
    a = set(g.query("SELECT ?target WHERE { } LIMIT 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } LIMIT 1 VALUES (?target) {('example')}"))
    assert a==b, "limit: %r != %r"%(a,b)

def testOrderByLimit():
    a = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1 VALUES (?target) {('example')}"))
    assert a==b, "orderbyLimit: %r != %r"%(a,b)

def testOrderByFuncLimit():
    a = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1 VALUES (?target) {('example')}"))
    assert a==b, "orderbyFuncLimit: %r != %r"%(a,b)

def testNoFuncOffset():
    a = set(g.query("SELECT ?target WHERE { } OFFSET 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } OFFSET 1 VALUES (?target) {('example')}"))
    assert a==b, "offset: %r != %r"%(a,b)

def testNoFuncLimitOffset():
    a = set(g.query("SELECT ?target WHERE { } LIMIT 1 OFFSET 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } LIMIT 1 OFFSET 1 VALUES (?target) {('example')}"))
    assert a==b, "limitOffset: %r != %r"%(a,b)

def testOrderByLimitOffset():
    a = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1 VALUES (?target) {('example')}"))
    assert a==b, "orderbyLimitOffset: %r != %r"%(a,b)

def testOrderByFuncLimitOffset():
    a = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1 VALUES (?target) {('example')}"))
    assert a==b, "orderbyFuncLimitOffset: %r != %r"%(a,b)

def testDistinct():
    a = set(g.query("SELECT DISTINCT ?target WHERE { }", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT DISTINCT ?target WHERE { } VALUES (?target) {('example')}"))
    assert a==b, "distinct: %r != %r"%(a,b)

def testDistinctOrderBy():
    a = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target VALUES (?target) {('example')}"))
    assert a==b, "distinctOrderby: %r != %r"%(a,b)

def testDistinctOrderByLimit():
    a = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target LIMIT 1", initBindings={'target': Literal('example')}))
    b = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target LIMIT 1 VALUES (?target) {('example')}"))
    assert a==b, "distinctOrderbyLimit: %r != %r"%(a,b)

def testPrepare():
    q = prepareQuery('SELECT ?target WHERE { }')
    r = list(g.query(q))
    e = [(None,)] # TODO: https://github.com/RDFLib/rdflib/issues/554
    assert r == e, 'prepare: %r != %r'%(r,e)

    r = list(g.query(q, initBindings={'target': Literal('example')}))
    e = [(Literal('example'),)]
    assert r == e, 'prepare: %r != %r'%(r, e)

    r = list(g.query(q))
    e = [(None,)] # TODO: https://github.com/RDFLib/rdflib/issues/554
    assert r == e, 'prepare: %r != %r'%(r,e)


def testData():
    data = ConjunctiveGraph()
    data += [ ( URIRef('urn:a'), URIRef('urn:p'), Literal('a') ),
              ( URIRef('urn:b'), URIRef('urn:p'), Literal('b') ) ]

    a = set(g.query("SELECT ?target WHERE { ?target <urn:p> ?val }", initBindings={'val': Literal('a')}))
    b = set(g.query("SELECT ?target WHERE { ?target <urn:p> ?val } VALUES (?val) {('a')}"))
    assert a==b, "data: %r != %r"%(a,b)

def testAsk():
    a = set(g.query("ASK { }", initBindings={'target': Literal('example')}))
    b = set(g.query("ASK { } VALUES (?target) {('example')}"))
    assert a==b, "ask: %r != %r"%(a,b)


EX = Namespace("http://example.com/")
g2 = ConjunctiveGraph()
g2.bind('', EX)
g2.add((EX['s1'], EX['p'], EX['o1']))
g2.add((EX['s2'], EX['p'], EX['o2']))

def testStringKey():
    results = list(g2.query("SELECT ?o WHERE { ?s :p ?o }", initBindings={"s": EX['s1']}))
    assert len(results) == 1, results

def testStringKeyWithQuestionMark():
    results = list(g2.query("SELECT ?o WHERE { ?s :p ?o }", initBindings={"?s": EX['s1']}))
    assert len(results) == 1, results

def testVariableKey():
    results = list(g2.query("SELECT ?o WHERE { ?s :p ?o }", initBindings={Variable("s"): EX['s1']}))
    assert len(results) == 1, results

def testVariableKeyWithQuestionMark():
    results = list(g2.query("SELECT ?o WHERE { ?s :p ?o }", initBindings={Variable("?s"): EX['s1']}))
    assert len(results) == 1, results


if __name__ == "__main__":

    import sys
    import nose
    if len(sys.argv)==1:
        nose.main(defaultTest=sys.argv[0])