summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Watts <watts.mark2015@gmail.com>2020-03-20 18:19:43 -0500
committerMark Watts <watts.mark2015@gmail.com>2020-03-20 18:19:43 -0500
commit9f5ce5cf6104a75f380987213c1c7eab9d8a4a44 (patch)
tree2477641c94b22fb57c1c84b21ed87ca7d273554b
parent6c751e79ebca2f9374c6afe47ec4de65c0644404 (diff)
downloadrdflib-9f5ce5cf6104a75f380987213c1c7eab9d8a4a44.tar.gz
Passing tests
- Also, fixing batching
-rw-r--r--rdflib/graph.py11
-rw-r--r--test/test_batch_add.py60
-rw-r--r--test/test_batch_add_graph.py38
3 files changed, 63 insertions, 46 deletions
diff --git a/rdflib/graph.py b/rdflib/graph.py
index 7de79e29..2caf13e6 100644
--- a/rdflib/graph.py
+++ b/rdflib/graph.py
@@ -1949,7 +1949,7 @@ class BatchAddGraph(object):
'''
def __init__(self, graph, batchsize=1000):
- if not batchsize or batchsize < 0:
+ if not batchsize or batchsize < 2:
raise ValueError("batchsize must be a positive number")
self.graph = graph
self.__graph_tuple = (graph,)
@@ -1963,17 +1963,20 @@ class BatchAddGraph(object):
self.batch = []
self.count = 0
- def add(self, triple):
+ def add(self, triple_or_quad):
'''
Add a triple to the buffer
:param triple: The triple to add
'''
- if len(self.batch) >= self.__batchsize == 0:
+ if len(self.batch) >= self.__batchsize:
self.graph.addN(self.batch)
self.batch = []
self.count += 1
- self.batch.append(triple + self.__graph_tuple)
+ if len(triple_or_quad) == 3:
+ self.batch.append(triple_or_quad + self.__graph_tuple)
+ else:
+ self.batch.append(triple_or_quad)
def __enter__(self):
self.reset()
diff --git a/test/test_batch_add.py b/test/test_batch_add.py
index b1e711f7..f36c7dfa 100644
--- a/test/test_batch_add.py
+++ b/test/test_batch_add.py
@@ -1,24 +1,76 @@
import unittest
+from unittest.mock import Mock
from rdflib.graph import Graph, BatchAddGraph
from rdflib.term import URIRef
class TestBatchAddGraph(unittest.TestCase):
- def test_batchsize_zero(self):
+ def test_batchsize_zero_denied(self):
with self.assertRaises(ValueError):
BatchAddGraph(Graph(), batchsize=0)
- def test_batchsize_none(self):
+ def test_batchsize_none_denied(self):
with self.assertRaises(ValueError):
BatchAddGraph(Graph(), batchsize=None)
- def test_batchsize_negative(self):
+ def test_batchsize_one_denied(self):
+ with self.assertRaises(ValueError):
+ BatchAddGraph(Graph(), batchsize=1)
+
+ def test_batchsize_negative_denied(self):
with self.assertRaises(ValueError):
BatchAddGraph(Graph(), batchsize=-12)
def test_exit_submits_partial_batch(self):
trip = (URIRef('a'), URIRef('b'), URIRef('c'))
g = Graph()
- with BatchAddGraph(g) as cut:
+ with BatchAddGraph(g, batchsize=10) as cut:
cut.add(trip)
self.assertIn(trip, g)
+
+ def test_add_more_than_batchsize(self):
+ trips = [(URIRef('a'), URIRef('b%d' % i), URIRef('c%d' % i))
+ for i in range(12)]
+ g = Graph()
+ with BatchAddGraph(g, batchsize=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()
+ badg = Graph(identifier='http://example.org/badness')
+ 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)]
+
+ badg = Graph(identifier='http://example.org/badness')
+ try:
+ with BatchAddGraph(g, batchsize=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))
diff --git a/test/test_batch_add_graph.py b/test/test_batch_add_graph.py
deleted file mode 100644
index 23e522dd..00000000
--- a/test/test_batch_add_graph.py
+++ /dev/null
@@ -1,38 +0,0 @@
-import unittest
-
-from rdflib.graph import BatchAddGraph
-
-
-class TestBatchAddGraph(unittest.TestCase):
- def test_add_less_than_batchsize(self):
- pass
-
- def test_add_more_than_batchsize(self):
- pass
-
- def test_negative_batchsize_denied(self):
- pass
-
- def test_zero_batchsize_denied(self):
- pass
-
- def test_one_batchsize_denied(self):
- pass
-
- def test_add_quad_for_non_conjunctive_fail(self):
- pass
-
- def test_add_quad_for_non_conjunctive_pass_on_context_matches(self):
- pass
-
- 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
- '''
-
- def test_batch_has_graph_added_to_triples(self):
- pass
-
- def test_batch_does_not_add_graph_to_quads(self):
- pass