def parse_json_results(self, response): graph = rdflib.ConjunctiveGraph() json = json.load(response) if 'boolean' in json: return SparqlResultBool(json['boolean']) vars_ = json['head']['vars'] ResultClass = Result(json['head']['vars']) pb = self.parse_json_binding results = SparqlResultList(vars_) for binding in json['results']['bindings']: results.append(ResultClass(*[pb(binding.get(v), graph) for v in vars_])) return results
def get(self): """ Returns an in-memory object representing the stream. You will either get a SparqlResultsList, a bool, or a ConjunctiveGraph. """ if self._cached_get is None: sparql_results_type = self.get_sparql_results_type() if sparql_results_type == 'resultset': self._cached_get = SparqlResultList(self.get_fields(), self.get_bindings()) elif sparql_results_type == 'boolean': self._cached_get = self.get_boolean() elif sparql_results_type == 'graph': graph = rdflib.ConjunctiveGraph() for prefix, namespace_uri in NS.iteritems(): graph.namespace_manager.bind(prefix, namespace_uri) graph += self.get_triples() self._cached_get = graph else: raise AssertionError( "Unexpected results type: {0}".format(sparql_results_type)) for name in ('query', 'duration'): if hasattr(self, name): setattr(self._cached_get, name, getattr(self, name)) return self._cached_get
import rdflib from humfrey.sparql.results import SparqlResultList, Result from humfrey.utils.namespaces import NS _TEST_BNODE = rdflib.BNode() TEST_RESULTSET_FIELDS = ('one', 'two') TEST_RESULTSET_RESULT = functools.partial(Result, TEST_RESULTSET_FIELDS) TEST_RESULTSET = SparqlResultList( ('one', 'two'), itertools.imap(TEST_RESULTSET_RESULT, [ (rdflib.URIRef('http://example.org/one'), _TEST_BNODE), (rdflib.Literal('hello'), rdflib.Literal('hello', lang='en')), (rdflib.Literal('foo"bar'), rdflib.Literal('foo\nbar')), (rdflib.Literal('foo,bar'), rdflib.Literal('foo;bar')), (rdflib.Literal('foo bar'), rdflib.Literal('foo\tbar')), (rdflib.Literal(1), rdflib.Literal('2011-01-02T12:34:56Z', datatype=NS.xsd.timeDate)), (None, None), (_TEST_BNODE, rdflib.BNode()), (rdflib.URIRef('http://example.org/'), rdflib.URIRef('mailto:[email protected]')), (rdflib.URIRef('urn:isbn:9781449306595'), rdflib.URIRef('tag:[email protected],2011:foo')), ])) del _TEST_BNODE TEST_RESULTSET.fields = ('one', 'two') TEST_RESULTSET.query = 'The query that was run' TEST_RESULTSET.duration = 1
class ResultsParserTestCase(unittest.TestCase): TEST_FIELDS = """uri label altLabel ship_label ship_class occupies_label""".split() TEST_URI_A = rdflib.URIRef('http://example.org/id/malcolm') TEST_URI_B = rdflib.URIRef('http://example.org/id/niska') TEST_RESULTS = SparqlResultList(TEST_FIELDS, [ Result(TEST_FIELDS, {'uri': TEST_URI_A, 'label': 'Malcolm Reynolds'}), Result(TEST_FIELDS, {'uri': TEST_URI_A, 'altLabel': 'Mal'}), Result(TEST_FIELDS, {'uri': TEST_URI_A, 'altLabel': "Cap'n"}), Result(TEST_FIELDS, {'uri': TEST_URI_A, 'ship_label': 'Serenity', 'ship_class': 'Firefly'}), Result(TEST_FIELDS, {'uri': TEST_URI_A, 'appearance_number': 'S01E01', 'appearance_label': 'Serenity'}), Result(TEST_FIELDS, {'uri': TEST_URI_A, 'appearance_number': 'S01E02', 'appearance_label': 'The Train Job'}), Result(TEST_FIELDS, {'uri': TEST_URI_B, 'label': 'Adelei Niska'}), Result(TEST_FIELDS, {'uri': TEST_URI_B, 'altLabel': 'Niska'}), Result(TEST_FIELDS, {'uri': TEST_URI_B, 'appearance_number': 'S01E02', 'appearance_label': 'The Train Job'}), Result(TEST_FIELDS, {'uri': TEST_URI_B, 'appearance_number': 'S01E10', 'appearance_label': 'War Stories'})]) TEST_META = type('', (), {'groups': 'altLabel appearance'})() EXPECTED_RESULT = [{'uri': TEST_URI_A, 'label': 'Malcolm Reynolds', 'altLabel': ["Cap'n", 'Mal'], 'ship': {'label': 'Serenity', 'class': 'Firefly'}, 'appearance': [{'number': 'S01E01', 'label': 'Serenity'}, {'number': 'S01E02', 'label': 'The Train Job'}]}, {'uri': TEST_URI_B, 'label': 'Adelei Niska', 'altLabel': ['Niska'], 'appearance': [{'number': 'S01E02', 'label': 'The Train Job'}, {'number': 'S01E10', 'label': 'War Stories'}]}] def testDictify(self): dictify = update.IndexUpdater.dictify groups = (('d',),) original = {'uri': 'foo', 'a': 1, 'b': 2, 'c_a': 3, 'c_b': 4, 'd_id': 'x', 'd_a': 'c'} expected = {'foo': {'uri': 'foo', 'a': 1, 'b': 2, 'c': {'a': 3, 'b': 4}, 'd': {'x': {'a': 'c'}}}} self.assertEqual(dictify(groups, original), expected) def testMergeResults(self): one = {'foo': {'a': 1, 'b': {'a': 2, 'b': 3}, 'c': {'x': {'a': 4}, 'y': {'a': 5}}}} two = {'foo': {'a': 11, 'c': {'y': {'a': 12}}}} expected = {'foo': {'a': 11, 'b': {'a': 2, 'b': 3}, 'c': {'x': {'a': 4}, 'y': {'a': 12}}}} groups = (('c',),) self.assertEqual(update.IndexUpdater.merge_dicts(groups, one, two), expected) @mock.patch('redis.client.Redis', lambda **kwargs: None) @mock.patch('django.conf.settings.REDIS_PARAMS', {}, create=True) def testResultParsing(self): index_updater = update.IndexUpdater() actual = list(index_updater.parse_results(self.TEST_META, self.TEST_RESULTS)) expected = copy.deepcopy(self.EXPECTED_RESULT) def sort_recursive(value): if isinstance(value, list): for subvalue in value: sort_recursive(subvalue) value.sort() elif isinstance(value, dict): for subvalue in value.itervalues(): sort_recursive(subvalue) sort_recursive(actual) sort_recursive(expected) self.assertEqual(actual, expected)
import functools import itertools import rdflib from humfrey.sparql.results import SparqlResultList, Result from humfrey.utils.namespaces import NS _TEST_BNODE = rdflib.BNode() TEST_RESULTSET_FIELDS = ('one', 'two') TEST_RESULTSET_RESULT = functools.partial(Result, TEST_RESULTSET_FIELDS) TEST_RESULTSET = SparqlResultList(('one', 'two'), itertools.imap(TEST_RESULTSET_RESULT, [ (rdflib.URIRef('http://example.org/one'), _TEST_BNODE), (rdflib.Literal('hello'), rdflib.Literal('hello', lang='en')), (rdflib.Literal('foo"bar'), rdflib.Literal('foo\nbar')), (rdflib.Literal('foo,bar'), rdflib.Literal('foo;bar')), (rdflib.Literal('foo bar'), rdflib.Literal('foo\tbar')), (rdflib.Literal(1), rdflib.Literal('2011-01-02T12:34:56Z', datatype=NS.xsd.timeDate)), (None, None), (_TEST_BNODE, rdflib.BNode()), (rdflib.URIRef('http://example.org/'), rdflib.URIRef('mailto:[email protected]')), (rdflib.URIRef('urn:isbn:9781449306595'), rdflib.URIRef('tag:[email protected],2011:foo')), ])) del _TEST_BNODE TEST_RESULTSET.fields = ('one', 'two') TEST_RESULTSET.query = 'The query that was run' TEST_RESULTSET.duration = 1