summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_batch_add.py89
-rw-r--r--test/test_sparqlstore.py78
2 files changed, 166 insertions, 1 deletions
diff --git a/test/test_batch_add.py b/test/test_batch_add.py
new file mode 100644
index 00000000..1747100c
--- /dev/null
+++ b/test/test_batch_add.py
@@ -0,0 +1,89 @@
+import unittest
+from rdflib.graph import Graph, BatchAddGraph
+from rdflib.term import URIRef
+
+
+class TestBatchAddGraph(unittest.TestCase):
+ def test_batch_size_zero_denied(self):
+ with self.assertRaises(ValueError):
+ BatchAddGraph(Graph(), batch_size=0)
+
+ def test_batch_size_none_denied(self):
+ with self.assertRaises(ValueError):
+ BatchAddGraph(Graph(), batch_size=None)
+
+ def test_batch_size_one_denied(self):
+ with self.assertRaises(ValueError):
+ BatchAddGraph(Graph(), batch_size=1)
+
+ def test_batch_size_negative_denied(self):
+ with self.assertRaises(ValueError):
+ BatchAddGraph(Graph(), batch_size=-12)
+
+ def test_exit_submits_partial_batch(self):
+ trip = (URIRef('a'), URIRef('b'), URIRef('c'))
+ g = Graph()
+ with BatchAddGraph(g, batch_size=10) as cut:
+ cut.add(trip)
+ self.assertIn(trip, g)
+
+ def test_add_more_than_batch_size(self):
+ trips = [(URIRef('a'), URIRef('b%d' % i), URIRef('c%d' % i))
+ for i in range(12)]
+ g = Graph()
+ with BatchAddGraph(g, batch_size=10) as cut:
+ for trip in trips:
+ cut.add(trip)
+ self.assertEqual(12, len(g))
+
+ def test_add_quad_for_non_conjunctive_empty(self):
+ '''
+ Graph drops quads that don't match our graph. Make sure we do the same
+ '''
+ g = Graph(identifier='http://example.org/g')
+ badg = Graph(identifier='http://example.org/badness')
+ with BatchAddGraph(g) as cut:
+ cut.add((URIRef('a'), URIRef('b'), URIRef('c'), badg))
+ self.assertEqual(0, len(g))
+
+ def test_add_quad_for_non_conjunctive_pass_on_context_matches(self):
+ g = Graph()
+ with BatchAddGraph(g) as cut:
+ cut.add((URIRef('a'), URIRef('b'), URIRef('c'), g))
+ self.assertEqual(1, len(g))
+
+ def test_no_addN_on_exception(self):
+ '''
+ Even if we've added triples so far, it may be that attempting to add the last
+ batch is the cause of our exception, so we don't want to attempt again
+ '''
+ g = Graph()
+ trips = [(URIRef('a'), URIRef('b%d' % i), URIRef('c%d' % i))
+ for i in range(12)]
+
+ try:
+ with BatchAddGraph(g, batch_size=10) as cut:
+ for i, trip in enumerate(trips):
+ cut.add(trip)
+ if i == 11:
+ raise Exception('myexc')
+ except Exception as e:
+ if str(e) != 'myexc':
+ pass
+ self.assertEqual(10, len(g))
+
+ def test_addN_batching_addN(self):
+ class MockGraph(object):
+ def __init__(self):
+ self.counts = []
+
+ def addN(self, quads):
+ self.counts.append(sum(1 for _ in quads))
+
+ g = MockGraph()
+ quads = [(URIRef('a'), URIRef('b%d' % i), URIRef('c%d' % i), g)
+ for i in range(12)]
+
+ with BatchAddGraph(g, batch_size=10, batch_addn=True) as cut:
+ cut.addN(quads)
+ self.assertEqual(g.counts, [10, 2])
diff --git a/test/test_sparqlstore.py b/test/test_sparqlstore.py
index 26a69460..a0a93b57 100644
--- a/test/test_sparqlstore.py
+++ b/test/test_sparqlstore.py
@@ -4,7 +4,10 @@ import os
import unittest
from nose import SkipTest
from requests import HTTPError
-
+from http.server import BaseHTTPRequestHandler, HTTPServer
+import socket
+from threading import Thread
+import requests
try:
assert len(urlopen("http://dbpedia.org/sparql").read()) > 0
@@ -67,5 +70,78 @@ class SPARQLStoreDBPediaTestCase(unittest.TestCase):
assert type(i[0]) == Literal, i[0].n3()
+class SPARQLStoreUpdateTestCase(unittest.TestCase):
+ def setUp(self):
+ port = self.setup_mocked_endpoint()
+ self.graph = Graph(store="SPARQLUpdateStore", identifier=URIRef("urn:ex"))
+ self.graph.open(("http://localhost:{port}/query".format(port=port),
+ "http://localhost:{port}/update".format(port=port)), create=False)
+ ns = list(self.graph.namespaces())
+ assert len(ns) > 0, ns
+
+ def setup_mocked_endpoint(self):
+ # Configure mock server.
+ s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
+ s.bind(('localhost', 0))
+ address, port = s.getsockname()
+ s.close()
+ mock_server = HTTPServer(('localhost', port), SPARQL11ProtocolStoreMock)
+
+ # Start running mock server in a separate thread.
+ # Daemon threads automatically shut down when the main process exits.
+ mock_server_thread = Thread(target=mock_server.serve_forever)
+ mock_server_thread.setDaemon(True)
+ mock_server_thread.start()
+ print("Started mocked sparql endpoint on http://localhost:{port}/".format(port=port))
+ return port
+
+ def tearDown(self):
+ self.graph.close()
+
+ def test_Query(self):
+ query = "insert data {<urn:s> <urn:p> <urn:o>}"
+ res = self.graph.update(query)
+ print(res)
+
+
+class SPARQL11ProtocolStoreMock(BaseHTTPRequestHandler):
+ def do_POST(self):
+ """
+ If the body should be analysed as well, just use:
+ ```
+ body = self.rfile.read(int(self.headers['Content-Length'])).decode()
+ print(body)
+ ```
+ """
+ contenttype = self.headers.get("Content-Type")
+ if self.path == "/query":
+ if self.headers.get("Content-Type") == "application/sparql-query":
+ pass
+ elif self.headers.get("Content-Type") == "application/x-www-form-urlencoded":
+ pass
+ else:
+ self.send_response(requests.codes.not_acceptable)
+ self.end_headers()
+ elif self.path == "/update":
+ if self.headers.get("Content-Type") == "application/sparql-update":
+ pass
+ elif self.headers.get("Content-Type") == "application/x-www-form-urlencoded":
+ pass
+ else:
+ self.send_response(requests.codes.not_acceptable)
+ self.end_headers()
+ else:
+ self.send_response(requests.codes.not_found)
+ self.end_headers()
+ self.send_response(requests.codes.ok)
+ self.end_headers()
+ return
+
+ def do_GET(self):
+ # Process an HTTP GET request and return a response with an HTTP 200 status.
+ self.send_response(requests.codes.ok)
+ self.end_headers()
+ return
+
if __name__ == '__main__':
unittest.main()