summaryrefslogtreecommitdiff
path: root/test/test_sparql_service.py
blob: 45cb84a07855122ea84fcb34a2defd1d450a9528 (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
from rdflib import Graph, URIRef, Literal, Variable
from rdflib.plugins.sparql import prepareQuery
from rdflib.compare import isomorphic


def test_service():
    g = Graph()
    q = '''select ?dbpHypernym ?dbpComment
    where
    { service <http://DBpedia.org/sparql>
    { select ?dbpHypernym ?dbpComment
    where
    {
    <http://dbpedia.org/resource/John_Lilburne>
        <http://purl.org/linguistics/gold/hypernym> ?dbpHypernym ;
        <http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment .

    } }  } limit 2'''
    results = g.query(q)
    assert len(results) == 2

    for r in results:
        assert len(r) == 2


def test_service_with_bind():
    g = Graph()
    q = '''select ?dbpHypernym ?dbpComment ?dbpDeathPlace
    where
    { bind (<http://dbpedia.org/resource/Eltham> as ?dbpDeathPlace)
      service <http://DBpedia.org/sparql>
    { select ?dbpHypernym ?dbpComment ?dbpDeathPlace
    where
    {
    <http://dbpedia.org/resource/John_Lilburne>
        <http://purl.org/linguistics/gold/hypernym> ?dbpHypernym ;
        <http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment ;
        <http://dbpedia.org/ontology/deathPlace> ?dbpDeathPlace .

    } }  } limit 2'''
    results = g.query(q)
    assert len(results) == 2

    for r in results:
        assert len(r) == 3


def test_service_with_values():
    g = Graph()
    q = '''select ?dbpHypernym ?dbpComment ?dbpDeathPlace
    where
    { values (?dbpHypernym ?dbpDeathPlace) {(<http://dbpedia.org/resource/Leveller> <http://dbpedia.org/resource/London>) (<http://dbpedia.org/resource/Leveller> <http://dbpedia.org/resource/Eltham>)}
      service <http://DBpedia.org/sparql>
    { select ?dbpHypernym ?dbpComment ?dbpDeathPlace
    where
    {
    <http://dbpedia.org/resource/John_Lilburne>
        <http://purl.org/linguistics/gold/hypernym> ?dbpHypernym ;
        <http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment ;
        <http://dbpedia.org/ontology/deathPlace> ?dbpDeathPlace .

    } }  } limit 2'''
    results = g.query(q)
    assert len(results) == 2

    for r in results:
        assert len(r) == 3


def test_service_with_implicit_select():
    g = Graph()
    q = '''select ?s ?p ?o
    where
    {
      service <http://DBpedia.org/sparql>
    {
      values (?s ?p ?o) {(<http://example.org/a> <http://example.org/b> 1) (<http://example.org/a> <http://example.org/b> 2)}
    }} limit 2'''
    results = g.query(q)
    assert len(results) == 2

    for r in results:
        assert len(r) == 3


def test_service_with_implicit_select_and_prefix():
    g = Graph()
    q = '''prefix ex:<http://example.org/>
    select ?s ?p ?o
    where
    {
      service <http://DBpedia.org/sparql>
    {
      values (?s ?p ?o) {(ex:a ex:b 1) (<http://example.org/a> <http://example.org/b> 2)}
    }} limit 2'''
    results = g.query(q)
    assert len(results) == 2

    for r in results:
        assert len(r) == 3

def test_service_with_implicit_select_and_base():
    g = Graph()
    q = '''base <http://example.org/>
    select ?s ?p ?o
    where
    {
      service <http://DBpedia.org/sparql>
    {
      values (?s ?p ?o) {(<a> <b> 1) (<a> <b> 2)}
    }} limit 2'''
    results = g.query(q)
    assert len(results) == 2

    for r in results:
        assert len(r) == 3


#def test_with_fixture(httpserver):
#    httpserver.expect_request("/sparql/?query=SELECT * WHERE ?s ?p ?o").respond_with_json({"vars": ["s","p","o"], "bindings":[]})
#    test_server = httpserver.url_for('/sparql')
#    g = Graph()
#    q = 'SELECT * WHERE {SERVICE <'+test_server+'>{?s ?p ?o} . ?s ?p ?o .}'
#    results = g.query(q)
#   assert len(results) == 0


if __name__ == '__main__':
    # import nose
    # nose.main(defaultTest=__name__)
    test_service()
    test_service_with_bind()
    test_service_with_values()
    test_service_with_implicit_select()
    test_service_with_implicit_select_and_prefix()
    test_service_with_implicit_select_and_base()