summaryrefslogtreecommitdiff
path: root/test/test_graph/test_batch_add.py
blob: b8d037e950cc7458031b17acd25bd8e421a84645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pytest

from rdflib.graph import BatchAddGraph, Graph
from rdflib.term import URIRef


class TestBatchAddGraph:
    def test_batch_size_zero_denied(self):
        with pytest.raises(ValueError):
            BatchAddGraph(Graph(), batch_size=0)

    def test_batch_size_none_denied(self):
        with pytest.raises(ValueError):
            BatchAddGraph(Graph(), batch_size=None)

    def test_batch_size_one_denied(self):
        with pytest.raises(ValueError):
            BatchAddGraph(Graph(), batch_size=1)

    def test_batch_size_negative_denied(self):
        with pytest.raises(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)
        assert trip in 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)
        assert 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))
        assert 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))
        assert 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
        assert 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)
        assert g.counts == [10, 2]