summaryrefslogtreecommitdiff
path: root/tests/functional-tests/04-group-concat.py
blob: 5d9bcd1169973f17e9971093444e21a0a2f07f43 (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
#!/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.
#
"""
Test the GROUP_CONCAT function in Sparql. Only requires the store.
"""
import unittest
import random

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

class TestGroupConcat (CommonTrackerStoreTest):
    """
    Insert a multivalued property and request the results in GROUP_CONCAT
    """
    def test_group_concat (self):
        """
        1. Insert 3 capabilities for a test contact
        2. Retrieve contact/capabilites without group_contact (3 results)
        2. TEST: contact with group_concat capabilities (1 result)
        3. Remove the test contact inserted
        """
        
        uri = "contact://test_group_concat"
        
        insert = """
        INSERT { <%s> a nco:IMAddress;
                      nco:imID \"test_group_concat\";
                      nco:imCapability nco:im-capability-text-chat ;
                      nco:imCapability nco:im-capability-media-calls ;
                      nco:imCapability nco:im-capability-file-transfers .
         }
        """ % (uri)
        self.tracker.update (insert)

        query = """
        SELECT ?c ?capability WHERE {
           ?c a nco:IMAddress ;
              nco:imID \"test_group_concat\";
              nco:imCapability ?capability .
        }
        """ 
        results = self.tracker.query (query)

        assert len (results) == 3
        group_concat_query = """
        SELECT ?c GROUP_CONCAT (?capability, '|') AS ?cap WHERE {
           ?c a nco:IMAddress ;
              nco:imID \"test_group_concat\";
              nco:imCapability ?capability .
        } GROUP BY (?c)
        """ 
        results = self.tracker.query (group_concat_query)
        assert len (results) == 1
        
        instances = results[0][1].split ('|')
        assert len (instances) == 3
        
        TEXT_CHAT = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#im-capability-text-chat"
        MEDIA_CALLS = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#im-capability-media-calls"
        FILE_TRANSFERS = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#im-capability-file-transfers"
        assert TEXT_CHAT in instances
        assert MEDIA_CALLS in instances
        assert FILE_TRANSFERS in instances

        
        #self.assertEquals (str(results[0][0]), "test_insertion_1")

        delete = """
        DELETE { <%s> a rdfs:Resource. }
        """ % (uri)
        self.tracker.update (delete)
        

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