[tests] skip SPARQL tests when python-sparqlwrapper is not installed (closes #279604)

authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
changeset65bce2c06a62
branchdefault
phasepublic
hiddenno
parent revision#5730ee266a8e [debian] recommend python-sparqlwrapper (closes #279603)
child revision#afd0c31334e2 fix NameError (s/query/rql) in dataio.rqlquery (closes #279601)
files modified by this revision
test/test_dataio.py
test/test_ner.py
# HG changeset patch
# User Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
# Date 1416819751 -3600
# Mon Nov 24 10:02:31 2014 +0100
# Node ID 65bce2c06a62c543aaa43264c477681a5e96a8a4
# Parent 5730ee266a8e7c1ba17d6115be10247d6985c672
[tests] skip SPARQL tests when python-sparqlwrapper is not installed (closes #279604)

diff --git a/test/test_dataio.py b/test/test_dataio.py
@@ -27,11 +27,11 @@
1  from tempfile import mkdtemp
2 
3  from nazca.utils.dataio import (HTMLPrettyPrint, ValidXHTMLPrettyPrint,
4                                  sparqlquery, sparqljson, _sparqlexecute,
5                                  rqlquery, parsefile,
6 -                                autocast, split_file)
7 +                                autocast, split_file, SPARQL_ENABLED)
8  from nazca.utils.tokenizer import NLTK_AVAILABLE
9  from nazca.ner import NerProcess
10  from nazca.ner.sources import NerSourceLexicon
11 
12  TESTDIR = path.dirname(__file__)
@@ -145,10 +145,11 @@
13                  alllines.extend(lines)
14 
15              with open(file2split) as fobj:
16                  self.assertEqual(alllines, fobj.readlines())
17 
18 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
19      def test_sparql_query(self):
20          results = sparqlquery(u'http://dbpedia.org/sparql',
21                                u'''SELECT DISTINCT ?uri ?designer
22                                    WHERE {
23                                    ?uri rdf:type <http://dbpedia.org/ontology/ProgrammingLanguage> ;
@@ -157,10 +158,11 @@
24                                    FILTER(regex(?label, "^Python"))
25                                    }''')
26          self.assertEqual(results, [['http://dbpedia.org/resource/Python_(programming_language)', 'http://dbpedia.org/resource/Guido_van_Rossum'],
27                                     ['http://dbpedia.org/resource/Python_for_S60', 'http://dbpedia.org/resource/Guido_van_Rossum']])
28 
29 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
30      def test_sparql_execute(self):
31          rawresults = _sparqlexecute(u'http://dbpedia.org/sparql',
32                                      u'''SELECT DISTINCT ?uri ?designer
33                                          WHERE {
34                                          ?uri rdf:type <http://dbpedia.org/ontology/ProgrammingLanguage> ;
@@ -181,27 +183,30 @@
35                                     'uri': {'type': 'uri',
36                                             'value': 'http://dbpedia.org/resource/Python_for_S60'}}],
37                                'distinct': False,
38                                'ordered': True}})
39 
40 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
41      def test_sparql_execute_no_raise_on_error(self):
42          rawresults = _sparqlexecute(u'http://dbpedia.org/sparql',
43                                      u'''SELECT DISTINCT ?uri
44                                      WHERE{
45                                      ?uri faultyrdf
46                                      ?uri rdf:type ?type}''')
47          self.assertEqual(rawresults, [])
48 
49 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
50      def test_sparql_execute_raise_on_error(self):
51          with self.assertRaises(RuntimeError):
52              rawresults = _sparqlexecute(u'http://dbpedia.org/sparql',
53                                          u'''SELECT DISTINCT ?uri
54                                          WHERE{
55                                          ?uri faultyrdf
56                                          ?uri rdf:type ?type}''',
57                                          raise_on_error=True)
58 
59 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
60      def test_sparql_json(self):
61          results = sparqljson(u'http://dbpedia.org/sparql',
62                               u'''SELECT DISTINCT ?uri ?designer
63                                   WHERE {
64                                   ?uri rdf:type <http://dbpedia.org/ontology/ProgrammingLanguage> ;
@@ -212,10 +217,11 @@
65          self.assertEqual(results,
66                           {'designer': set(['http://dbpedia.org/resource/Guido_van_Rossum']),
67                            'uri': set(['http://dbpedia.org/resource/Python_(programming_language)',
68                                        'http://dbpedia.org/resource/Python_for_S60'])})
69 
70 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
71      def test_sparql_autocast(self):
72          alignset = sparqlquery('http://dbpedia.inria.fr/sparql',
73                                   'prefix db-owl: <http://dbpedia.org/ontology/>'
74                                   'prefix db-prop: <http://fr.dbpedia.org/property/>'
75                                   'select ?ville, ?name, ?long, ?lat where {'
@@ -228,10 +234,11 @@
76                                   ' FILTER (?population > 1000)'
77                                   '} LIMIT 100', indexes=[0, 1, (2, 3)])
78          self.assertEqual(len(alignset), 100)
79          self.assertTrue(isinstance(alignset[0][2][0], float))
80 
81 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
82      def test_sparql_no_autocast(self):
83          alignset = sparqlquery('http://dbpedia.inria.fr/sparql',
84                                   'prefix db-owl: <http://dbpedia.org/ontology/>'
85                                   'prefix db-prop: <http://fr.dbpedia.org/property/>'
86                                   'select ?ville, ?name, ?long, ?lat where {'
diff --git a/test/test_ner.py b/test/test_ner.py
@@ -21,15 +21,16 @@
87      import unittest
88  else:
89      import unittest2 as unittest
90 
91  from nazca.ner.sources import (NerSourceLexicon,
92 -                                          NerSourceSparql,
93 -                                          NerSourceRql)
94 +                               NerSourceSparql,
95 +                               NerSourceRql)
96  from nazca.ner import NerProcess
97  from nazca.utils.tokenizer import Token, Sentence, NLTK_AVAILABLE
98  from nazca.ner.preprocessors import NerStopwordsFilterPreprocessor
99 +from nazca.utils.dataio import SPARQL_ENABLED
100 
101 
102  class NerTest(unittest.TestCase):
103      """ Test of Ner """
104 
@@ -52,10 +53,11 @@
105          """ Test rql source """
106          source = NerSourceRql('http://www.cubicweb.org',
107                                'Any U LIMIT 1 WHERE X cwuri U, X name "%(word)s"')
108          self.assertEqual(source.query_word('apycot'), [u'http://www.cubicweb.org/1310453',])
109 
110 +    @unittest.skipUnless(SPARQL_ENABLED, 'python-sparqlwrapper is not installed')
111      def test_sparql_source(self):
112          """ Test sparql source """
113          source = NerSourceSparql(u'http://dbpedia.org/sparql',
114                               u'''SELECT DISTINCT ?uri
115                                   WHERE {