def testQuerySelectFull(self, mock_method): """Test SELECT query with full data.""" mock_method.return_value = Container( SQL_RESPONSE_CONTAINER % '{}, {}'.format(ITEM_Q498787, ITEM_Q677525)) with skipping(pywikibot.exceptions.TimeoutError): q = sparql.SparqlQuery() res = q.select('SELECT * WHERE { ?x ?y ?z }', full_data=True) self.assertIsInstance(res, list, 'Result is not a list') self.assertLength(res, 2) self.assertIsInstance(res[0]['cat'], sparql.URI, 'Wrong type for URI') self.assertEqual(repr(res[0]['cat']), '<http://www.wikidata.org/entity/Q498787>', 'Wrong URI representation') self.assertEqual(res[0]['cat'].getID(), 'Q498787', 'Wrong URI ID') self.assertIsInstance(res[0]['catLabel'], sparql.Literal, 'Wrong type for Literal') self.assertEqual(repr(res[0]['catLabel']), 'Muezza@en', 'Wrong literal representation') self.assertIsInstance(res[0]['d'], sparql.Literal, 'Wrong type for Literal') self.assertEqual( repr(res[0]['d']), '1955-01-01T00:00:00Z^^http://www.w3.org/2001/XMLSchema#dateTime', 'Wrong URI representation')
def setUpClass(cls): """Setup tests.""" super().setUpClass() with skipping(OSError, msg='djvulibre library not installed.'): dp = subprocess.Popen(['djvudump'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) dp.communicate()
def _get_param_values(self, site, module, parameter): """Perform check that a parameter matches the expected list.""" with skipping( ValueError, msg='Paraminfo for {} could not be loaded'.format(module)): param = site._paraminfo.parameter(module, parameter) if not param or 'type' not in param: raise unittest.SkipTest('No defined values for {}.{}'.format( module, parameter)) return param['type']
def testQueryAsk(self, mock_method): """Test ASK query.""" mock_method.return_value = Container(RESPONSE_TRUE) with skipping(pywikibot.exceptions.TimeoutError): q = sparql.SparqlQuery() res = q.ask('ASK { ?x ?y ?z }') self.assertTrue(res) mock_method.return_value = Container(RESPONSE_FALSE) res = q.ask('ASK { ?x ?y ?z }') self.assertFalse(res)
def testGetItems(self, mock_method): """Test item list retrieval via SPARQL.""" mock_method.return_value = Container( SQL_RESPONSE_CONTAINER % '{0}, {1}, {1}'.format(ITEM_Q498787, ITEM_Q677525)) with skipping(pywikibot.exceptions.TimeoutError): q = sparql.SparqlQuery() res = q.get_items('SELECT * WHERE { ?x ?y ?z }', 'cat') self.assertSetEqual(res, {'Q498787', 'Q677525'}) res = q.get_items('SELECT * WHERE { ?x ?y ?z }', 'cat', result_type=list) self.assertEqual(res, ['Q498787', 'Q677525', 'Q677525'])
def testQuerySelect(self, mock_method): """Test SELECT query.""" mock_method.return_value = Container( SQL_RESPONSE_CONTAINER % '{}, {}'.format(ITEM_Q498787, ITEM_Q677525)) with skipping(pywikibot.exceptions.TimeoutError): q = sparql.SparqlQuery() res = q.select('SELECT * WHERE { ?x ?y ?z }') self.assertIsInstance(res, list, 'Result is not a list') self.assertLength(res, 2) self.assertDictEqual( res[0], { 'cat': 'http://www.wikidata.org/entity/Q498787', 'catLabel': 'Muezza', 'd': '1955-01-01T00:00:00Z' }, 'Bad result') self.assertDictEqual( res[1], { 'cat': 'http://www.wikidata.org/entity/Q677525', 'catLabel': 'Orangey', 'd': '2015-06-22T00:00:00Z' }, 'Bad result')