Fix initBindings handling. Fixes #294 · RDFLib/rdflib@6200934 (original) (raw)

1

1

``

2

2

`from nose import SkipTest

`

3

3

``

``

4

`+

from rdflib.plugins.sparql import prepareQuery

`

``

5

+

4

6

``

5

7

`from rdflib import ConjunctiveGraph, URIRef, Literal, Namespace, Variable

`

6

8

`g = ConjunctiveGraph()

`

7

9

``

8

10

``

9

11

`def testStr():

`

10

``

`-

raise SkipTest('skipped - pending fix for #294')

`

11

12

`a = set(g.query("SELECT (STR(?target) AS ?r) WHERE { }", initBindings={'target': URIRef('example:a')}))

`

12

13

`b = set(g.query("SELECT (STR(?target) AS ?r) WHERE { } VALUES (?target) {(example:a)}"))

`

13

14

`assert a==b, "STR: %r != %r"%(a,b)

`

14

15

``

15

16

`def testIsIRI():

`

16

``

`-

raise SkipTest('skipped - pending fix for #294')

`

17

17

`a = set(g.query("SELECT (isIRI(?target) AS ?r) WHERE { }", initBindings={'target': URIRef('example:a')}))

`

18

18

`b = set(g.query("SELECT (isIRI(?target) AS ?r) WHERE { } VALUES (?target) {(example:a)}"))

`

19

19

`assert a==b, "isIRI: %r != %r"%(a,b)

`

20

20

``

21

21

`def testIsBlank():

`

22

``

`-

raise SkipTest('skipped - pending fix for #294')

`

23

22

`a = set(g.query("SELECT (isBlank(?target) AS ?r) WHERE { }", initBindings={'target': URIRef('example:a')}))

`

24

23

`b = set(g.query("SELECT (isBlank(?target) AS ?r) WHERE { } VALUES (?target) {(example:a)}"))

`

25

24

`assert a==b, "isBlank: %r != %r"%(a,b)

`

26

25

``

27

``

`-

def testIsLiteral():

`

28

``

`-

raise SkipTest('skipped - pending fix for #294')

`

``

26

`+

def testIsLiteral():

`

29

27

`a = set(g.query("SELECT (isLiteral(?target) AS ?r) WHERE { }", initBindings={'target': Literal('example')}))

`

30

28

`b = set(g.query("SELECT (isLiteral(?target) AS ?r) WHERE { } VALUES (?target) {('example')}"))

`

31

29

`assert a==b, "isLiteral: %r != %r"%(a,b)

`

32

30

``

33

``

`-

def testUCase():

`

34

``

`-

raise SkipTest('skipped - pending fix for #294')

`

``

31

`+

def testUCase():

`

35

32

`a = set(g.query("SELECT (UCASE(?target) AS ?r) WHERE { }", initBindings={'target': Literal('example')}))

`

36

33

`b = set(g.query("SELECT (UCASE(?target) AS ?r) WHERE { } VALUES (?target) {('example')}"))

`

37

34

`assert a==b, "UCASE: %r != %r"%(a,b)

`

`@@ -41,13 +38,102 @@ def testNoFunc():

`

41

38

`b = set(g.query("SELECT ?target WHERE { } VALUES (?target) {('example')}"))

`

42

39

`assert a==b, "no func: %r != %r"%(a,b)

`

43

40

``

``

41

`+

def testOrderBy():

`

``

42

`+

a = set(g.query("SELECT ?target WHERE { } ORDER BY ?target", initBindings={'target': Literal('example')}))

`

``

43

`+

b = set(g.query("SELECT ?target WHERE { } ORDER BY ?target VALUES (?target) {('example')}"))

`

``

44

`+

assert a==b, "orderby: %r != %r"%(a,b)

`

``

45

+

``

46

`+

def testOrderByFunc():

`

``

47

`+

a = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target", initBindings={'target': Literal('example')}))

`

``

48

`+

b = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target VALUES (?target) {('example')} "))

`

``

49

`+

assert a==b, "orderbyFunc: %r != %r"%(a,b)

`

``

50

+

``

51

`+

def testNoFuncLimit():

`

``

52

`+

a = set(g.query("SELECT ?target WHERE { } LIMIT 1", initBindings={'target': Literal('example')}))

`

``

53

`+

b = set(g.query("SELECT ?target WHERE { } LIMIT 1 VALUES (?target) {('example')}"))

`

``

54

`+

assert a==b, "limit: %r != %r"%(a,b)

`

``

55

+

``

56

`+

def testOrderByLimit():

`

``

57

`+

a = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1", initBindings={'target': Literal('example')}))

`

``

58

`+

b = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1 VALUES (?target) {('example')}"))

`

``

59

`+

assert a==b, "orderbyLimit: %r != %r"%(a,b)

`

``

60

+

``

61

`+

def testOrderByFuncLimit():

`

``

62

`+

a = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1", initBindings={'target': Literal('example')}))

`

``

63

`+

b = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1 VALUES (?target) {('example')}"))

`

``

64

`+

assert a==b, "orderbyFuncLimit: %r != %r"%(a,b)

`

``

65

+

``

66

`+

def testNoFuncOffset():

`

``

67

`+

a = set(g.query("SELECT ?target WHERE { } OFFSET 1", initBindings={'target': Literal('example')}))

`

``

68

`+

b = set(g.query("SELECT ?target WHERE { } OFFSET 1 VALUES (?target) {('example')}"))

`

``

69

`+

assert a==b, "offset: %r != %r"%(a,b)

`

``

70

+

``

71

`+

def testNoFuncLimitOffset():

`

``

72

`+

a = set(g.query("SELECT ?target WHERE { } LIMIT 1 OFFSET 1", initBindings={'target': Literal('example')}))

`

``

73

`+

b = set(g.query("SELECT ?target WHERE { } LIMIT 1 OFFSET 1 VALUES (?target) {('example')}"))

`

``

74

`+

assert a==b, "limitOffset: %r != %r"%(a,b)

`

``

75

+

``

76

`+

def testOrderByLimitOffset():

`

``

77

`+

a = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1", initBindings={'target': Literal('example')}))

`

``

78

`+

b = set(g.query("SELECT ?target WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1 VALUES (?target) {('example')}"))

`

``

79

`+

assert a==b, "orderbyLimitOffset: %r != %r"%(a,b)

`

``

80

+

``

81

`+

def testOrderByFuncLimitOffset():

`

``

82

`+

a = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1", initBindings={'target': Literal('example')}))

`

``

83

`+

b = set(g.query("SELECT (UCASE(?target) as ?r) WHERE { } ORDER BY ?target LIMIT 1 OFFSET 1 VALUES (?target) {('example')}"))

`

``

84

`+

assert a==b, "orderbyFuncLimitOffset: %r != %r"%(a,b)

`

``

85

+

``

86

`+

def testDistinct():

`

``

87

`+

a = set(g.query("SELECT DISTINCT ?target WHERE { }", initBindings={'target': Literal('example')}))

`

``

88

`+

b = set(g.query("SELECT DISTINCT ?target WHERE { } VALUES (?target) {('example')}"))

`

``

89

`+

assert a==b, "distinct: %r != %r"%(a,b)

`

``

90

+

``

91

`+

def testDistinctOrderBy():

`

``

92

`+

a = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target", initBindings={'target': Literal('example')}))

`

``

93

`+

b = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target VALUES (?target) {('example')}"))

`

``

94

`+

assert a==b, "distinctOrderby: %r != %r"%(a,b)

`

``

95

+

``

96

`+

def testDistinctOrderByLimit():

`

``

97

`+

a = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target LIMIT 1", initBindings={'target': Literal('example')}))

`

``

98

`+

b = set(g.query("SELECT DISTINCT ?target WHERE { } ORDER BY ?target LIMIT 1 VALUES (?target) {('example')}"))

`

``

99

`+

assert a==b, "distinctOrderbyLimit: %r != %r"%(a,b)

`

``

100

+

``

101

`+

def testPrepare():

`

``

102

`+

q = prepareQuery('SELECT ?target WHERE { }')

`

``

103

`+

r = list(g.query(q))

`

``

104

`+

e = [(None,)] # TODO: https://github.com/RDFLib/rdflib/issues/554

`

``

105

`+

assert r == e, 'prepare: %r != %r'%(r,e)

`

``

106

+

``

107

`+

r = list(g.query(q, initBindings={'target': Literal('example')}))

`

``

108

`+

e = [(Literal('example'),)]

`

``

109

`+

assert r == e, 'prepare: %r != %r'%(r, e)

`

``

110

+

``

111

`+

r = list(g.query(q))

`

``

112

`+

e = [(None,)] # TODO: https://github.com/RDFLib/rdflib/issues/554

`

``

113

`+

assert r == e, 'prepare: %r != %r'%(r,e)

`

``

114

+

``

115

+

``

116

`+

def testData():

`

``

117

`+

data = ConjunctiveGraph()

`

``

118

`+

data += [ ( URIRef('urn:a'), URIRef('urn:p'), Literal('a') ),

`

``

119

`+

( URIRef('urn:b'), URIRef('urn:p'), Literal('b') ) ]

`

``

120

+

``

121

`+

a = set(g.query("SELECT ?target WHERE { ?target urn:p ?val }", initBindings={'val': Literal('a')}))

`

``

122

`+

b = set(g.query("SELECT ?target WHERE { ?target urn:p ?val } VALUES (?val) {('a')}"))

`

``

123

`+

assert a==b, "data: %r != %r"%(a,b)

`

``

124

+

``

125

`+

def testAsk():

`

``

126

`+

a = set(g.query("ASK { }", initBindings={'target': Literal('example')}))

`

``

127

`+

b = set(g.query("ASK { } VALUES (?target) {('example')}"))

`

``

128

`+

assert a==b, "ask: %r != %r"%(a,b)

`

``

129

+

44

130

``

45

131

`EX = Namespace("http://example.com/")

`

46

132

`g2 = ConjunctiveGraph()

`

47

133

`g2.bind('', EX)

`

48

134

`g2.add((EX['s1'], EX['p'], EX['o1']))

`

49

135

`g2.add((EX['s2'], EX['p'], EX['o2']))

`

50

``

-

``

136

+

51

137

`def testStringKey():

`

52

138

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

`

53

139

`assert len(results) == 1, results

`

`@@ -63,11 +149,11 @@ def testVariableKey():

`

63

149

`def testVariableKeyWithQuestionMark():

`

64

150

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

`

65

151

`assert len(results) == 1, results

`

66

``

-

67

``

-

``

152

+

``

153

+

68

154

`if name == "main":

`

69

155

``

70

156

`import sys

`

71

157

`import nose

`

72

``

`-

if len(sys.argv)==1:

`

``

158

`+

if len(sys.argv)==1:

`

73

159

`nose.main(defaultTest=sys.argv[0])

`