def test_unicode(self): """ Check that returned query string is unicode. """ statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) result = SparulTranslator(query).translate() self.assertTrue(isinstance(result, unicode))
def _add_many(self, triples, context=None): debug("ADD several triples") query = insert() if context: query.into(context) for s, p, o in triples: query.template((s, p, o)) query_str = str(query) try: debug(query_str) self._sparql_wrapper.setQuery(query_str) self._sparql_wrapper.query().convert() return True except EndPointNotFound as _: raise_(SparqlWriterException, "Endpoint not found", sys.exc_info()[2]) except QueryBadFormed as _: raise_(SparqlWriterException, "Bad query: %s" % query_str, sys.exc_info()[2]) except Exception as e: raise_(SparqlWriterException, "Exception: %s" % e, sys.exc_info()[2])
def test_insert(self): """ Try to produce INSERT ..." query. """ expected = canonical(u"INSERT { <http://a> <http://b> <http://c> }") statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) result = canonical(SparulTranslator(query).translate()) self.assertEqual(expected, result)
def test_insert_data_into(self): """ INSERT DATA INTO ... { ... } """ expected = canonical(u"INSERT DATA INTO <g> { <a> <b> <c>. <a> <b> <d> }") st1 = URIRef("a"), URIRef("b"), URIRef("c") st2 = URIRef("a"), URIRef("b"), URIRef("d") query = insert(data = True).into(URIRef("g")) query.template(st1, st2) result = canonical(SparulTranslator(query).translate()) self.assertEqual(expected, result)
def test_str(self): """ Test that __str__ translates query to string. """ expected = canonical(u"INSERT { <http://a> <http://b> <http://c> }") statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) # test str() self.assertEqual(expected, canonical(unicode(str(query)))) # test unicode() self.assertEqual(expected, canonical(unicode(query)))
def _prepare_add_many_query(resources, context=None): query = insert() if context: query = insert() query.into(context) else: query = insert(data=True) for resource in resources: s = resource.subject for p, objs in list(resource.rdf_direct.items()): for o in objs: if isinstance(o, Literal) and isinstance( o.value, str) and ("'" in o.value or '"' in o.value or '\\'): o = Literal(_escape_string(o.value), datatype=o.datatype) query.template((s, p, o)) return query
def test_insert_data_into(self): """ INSERT DATA INTO ... { ... } """ expected = canonical( u"INSERT DATA INTO <g> { <a> <b> <c>. <a> <b> <d> }") st1 = URIRef("a"), URIRef("b"), URIRef("c") st2 = URIRef("a"), URIRef("b"), URIRef("d") query = insert(data=True).into(URIRef("g")) query.template(st1, st2) result = canonical(SparulTranslator(query).translate()) self.assertEqual(expected, result)
def _prepare_add_many_query(resources, context=None): query = insert() if context: query.into(context) for resource in resources: s = resource.subject for p, objs in list(resource.rdf_direct.items()): for o in objs: query.template((s, p, o)) return query
def __prepare_add_many_query(self, resources, context = None): query = insert() if context: query.into(context) for resource in resources: s = resource.subject for p, objs in resource.rdf_direct.items(): for o in objs: query.template((s, p, o)) return query
def test_str(): """ Test that __str__ translates query to string. """ expected = canonical(u"INSERT { <http://a> <http://b> <http://c> }") statement = URIRef("http://a"), URIRef("http://b"), URIRef("http://c") query = insert().template(statement) # test str() assert expected == canonical(unicode(str(query))) # test unicode() assert expected == canonical(unicode(query))
def __prepare_add_many_queries(self, resources, context = None): queries = [] query = insert() if context: query.into(context) i = 0 for resource in resources: s = resource.subject for p, objs in resource.rdf_direct.items(): for o in objs: query.template((s, p, o)) i += 1 if self.__max_statements > 0 and i == self.__max_statements: queries.append(query) query = insert() if context: query.into(context) i = 0 queries.append(query) return queries
def _add_many(self, triples, context=None): debug("ADD several triples") query = insert() if context: query.into(context) for s, p, o in triples: query.template((s, p, o)) query_str = unicode(query) try: debug(query_str) self._sparql_wrapper.setQuery(query_str) self._sparql_wrapper.query().convert() return True except EndPointNotFound, _: raise SparqlWriterException("Endpoint not found"), None, sys.exc_info()[2]