test/unittest_spa2rql.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 22 Jul 2009 19:42:44 +0200
changeset 2428 a3e10e6a2461
parent 2427 3e0ef847a546
child 2431 93c061eac647
permissions -rw-r--r--
use space as separator, dot may be found in xml ns
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
from logilab.common.testlib import TestCase, unittest_main
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
from cubicweb.devtools import TestServerConfiguration
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
from cubicweb.xy import xy
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
from cubicweb.spa2rql import Sparql2rqlTranslator
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
xy.add_equivalence('Project', 'doap:Project')
2428
a3e10e6a2461 use space as separator, dot may be found in xml ns
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2427
diff changeset
     7
xy.add_equivalence('Project creation_date', 'doap:Project doap:created')
a3e10e6a2461 use space as separator, dot may be found in xml ns
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2427
diff changeset
     8
xy.add_equivalence('Project name', 'doap:Project doap:name')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
config = TestServerConfiguration('data')
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
config.bootstrap_cubes()
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
schema = config.load_schema()
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
class XYTC(TestCase):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
    def setUp(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
        self.tr = Sparql2rqlTranslator(schema)
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
    def _test(self, sparql, rql):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
        qi = self.tr.translate(sparql)
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
        self.assertEquals(qi.finalize(), rql)
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
    def XXX_test_base_01(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
        self._test('SELECT * WHERE { }', 'Any X')
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
    def test_base_is(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
        self._test('''
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
    PREFIX doap: <http://usefulinc.com/ns/doap#>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
    SELECT ?project
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
    WHERE  {
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
      ?project a doap:Project;
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
    }''', 'Any PROJECT WHERE PROJECT is Project')
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
    def test_base_attr_sel(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
        self._test('''
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
    PREFIX doap: <http://usefulinc.com/ns/doap#>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
    SELECT ?created
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
    WHERE  {
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
      ?project a doap:Project;
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
              doap:created ?created.
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
    }''', 'Any CREATED WHERE PROJECT creation_date CREATED, PROJECT is Project')
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
2427
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    45
    def test_base_attr_sel_distinct(self):
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    46
        self._test('''
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    47
    PREFIX doap: <http://usefulinc.com/ns/doap#>
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    48
    SELECT DISTINCT ?name
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    49
    WHERE  {
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    50
      ?project a doap:Project;
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    51
              doap:name ?name.
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    52
    }''', 'DISTINCT Any NAME WHERE PROJECT name NAME, PROJECT is Project')
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    53
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    54
    def test_base_attr_sel_reduced(self):
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    55
        self._test('''
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    56
    PREFIX doap: <http://usefulinc.com/ns/doap#>
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    57
    SELECT REDUCED ?name
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    58
    WHERE  {
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    59
      ?project a doap:Project;
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    60
              doap:name ?name.
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    61
    }''', 'Any NAME WHERE PROJECT name NAME, PROJECT is Project')
3e0ef847a546 distinct / reduced support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2422
diff changeset
    62
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
    def test_base_any_attr_sel(self):
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
        self._test('''
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
    SELECT ?x ?cd
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
    WHERE  {
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
      ?x dc:date ?cd;
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
    }''', 'Any X, CD WHERE X creation_date CD')
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
    def test_base_any_attr_sel_amb(self):
2428
a3e10e6a2461 use space as separator, dot may be found in xml ns
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2427
diff changeset
    73
        xy.add_equivalence('Version publication_date', 'doap:Version dc:date')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
        try:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
            self._test('''
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
    SELECT ?x ?cd
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
    WHERE  {
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
      ?x dc:date ?cd;
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
    }''', '(Any X, CD WHERE , X creation_date CD) UNION (Any X, CD WHERE , X publication_date CD, X is Version)')
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
        finally:
2428
a3e10e6a2461 use space as separator, dot may be found in xml ns
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2427
diff changeset
    82
            xy.remove_equivalence('Version publication_date', 'doap:Version dc:date')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
# # Two elements in the group
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
# PREFIX :  <http://example.org/ns#>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
# SELECT *
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
# { :p :q :r  OPTIONAL { :a :b :c }
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
#   :p :q :r  OPTIONAL { :a :b :c }
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
# }
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
# PREFIX : <http://example.org/ns#>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
# SELECT *
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
# {
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
#   { ?s ?p ?o } UNION { ?a ?b ?c }
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
# }
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    97
# PREFIX dob: <http://placetime.com/interval/gregorian/1977-01-18T04:00:00Z/P>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    98
# PREFIX time: <http://www.ai.sri.com/daml/ontologies/time/Time.daml#>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
# PREFIX dc: <http://purl.org/dc/elements/1.1/>
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
# SELECT ?desc
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
# WHERE  {
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
#   dob:1D a time:ProperInterval;
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
#          dc:description ?desc.
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
# }
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   106
if __name__ == '__main__':
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
    unittest_main()