summaryrefslogtreecommitdiff
path: root/tests/functional-tests/07-graph.py
blob: 455bde2207cd883481859afe669e7d6bb9f396ed (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
#!/usr/bin/python
#
# Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
"""
Tests graphs in Sparql. Only requires the store.
"""
import unittest
import random

from common.utils import configuration as cfg
import unittest as ut
#import unittest as ut
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest

class TestGraphs (CommonTrackerStoreTest):
    """
    Insert triplets in different graphs and check the query results asking in
    one specific graph, in all of them and so on.
    """

    def test_graph_filter (self):
        """
        1. Insert a contact with different phone numbers from different sources
        2. Query phone numbers of a single graph
           EXPECTED: Only return the phone number from the specified source
        3. Remove the created resources
        """
        insert_sparql = """
        INSERT {
            <tel:+1234567890> a nco:PhoneNumber ;
                nco:phoneNumber '+1234567890' .
            <tel:+1234567891> a nco:PhoneNumber ;
                nco:phoneNumber '+1234567891' .
            <tel:+1234567892> a nco:PhoneNumber ;
                nco:phoneNumber '+1234567892' .
            <contact://test/graph/1> a nco:PersonContact .
            GRAPH <graph://test/graph/0> {
                <contact://test/graph/1> nco:hasPhoneNumber <tel:+1234567890>
            }
            GRAPH <graph://test/graph/1> {
                <contact://test/graph/1> nco:hasPhoneNumber <tel:+1234567891>
            }
            GRAPH <graph://test/graph/2> {
                <contact://test/graph/1> nco:hasPhoneNumber <tel:+1234567892>
            }
        }
        """
        self.tracker.update (insert_sparql)

        query = """
        SELECT ?contact ?number WHERE {
            ?contact a nco:PersonContact
            GRAPH <graph://test/graph/1> {
                ?contact nco:hasPhoneNumber ?number
            }
        } ORDER BY DESC (fts:rank(?contact))
        """
        results = self.tracker.query (query)

        self.assertEquals (len(results), 1)
        self.assertEquals (results[0][0], "contact://test/graph/1")
        self.assertEquals (results[0][1], "tel:+1234567891")

        delete_sparql = """
        DELETE {
            <tel:+1234567890> a rdf:Resource .
            <tel:+1234567891> a rdf:Resource .
            <tel:+1234567892> a rdf:Resource .
            <contact://test/graph/1> a rdf:Resource .
        }
        """

    def test_graph_insert_multiple (self):
        """
        1. Insert a contact with the same phone number from different sources
        2. Query graph uri of hasPhoneNumber statement
           EXPECTED: The uri of the first graph that inserted the phone number
        3. Remove the created resources
        """
        insert_sparql = """
        INSERT {
            <tel:+1234567890> a nco:PhoneNumber ;
                nco:phoneNumber '+1234567890' .
            <contact://test/graph/1> a nco:PersonContact .
            GRAPH <graph://test/graph/0> {
                <contact://test/graph/1> nco:hasPhoneNumber <tel:+1234567890>
            }
            GRAPH <graph://test/graph/1> {
                <contact://test/graph/1> nco:hasPhoneNumber <tel:+1234567890>
            }
            GRAPH <graph://test/graph/2> {
                <contact://test/graph/1> nco:hasPhoneNumber <tel:+1234567890>
            }
        }
        """
        self.tracker.update (insert_sparql)

        query = """
        SELECT ?contact ?g WHERE {
            ?contact a nco:PersonContact
            GRAPH ?g {
                ?contact nco:hasPhoneNumber <tel:+1234567890>
            }
        }
        """
        results = self.tracker.query (query)
        self.assertEquals (len(results), 1)
        self.assertEquals (results[0][0], "contact://test/graph/1")
        self.assertEquals (results[0][1], "graph://test/graph/0")

        delete_sparql = """
        DELETE {
            <tel:+1234567890> a rdf:Resource .
            <contact://test/graph/1> a rdf:Resource .
        }
        """



if __name__ == '__main__':
    ut.main()