diff options
| author | Graham Higgins <gjh@bel-epa.com> | 2022-03-18 17:58:12 +0000 |
|---|---|---|
| committer | Graham Higgins <gjh@bel-epa.com> | 2022-03-18 17:58:12 +0000 |
| commit | 9be9d837b55c93eba38cfd20f24f6646dbbaaee7 (patch) | |
| tree | 21245969253d0db9d2596904e09da416ee29de35 /test/test_store | |
| parent | 29bacc537e238aed5d38c009ed9f3001ba5bfc86 (diff) | |
| download | rdflib-9be9d837b55c93eba38cfd20f24f6646dbbaaee7.tar.gz | |
revert unintended change
Diffstat (limited to 'test/test_store')
| -rw-r--r-- | test/test_store/test_store_berkeleydb.py | 239 |
1 files changed, 112 insertions, 127 deletions
diff --git a/test/test_store/test_store_berkeleydb.py b/test/test_store/test_store_berkeleydb.py index 9b68605e..f96fb9bc 100644 --- a/test/test_store/test_store_berkeleydb.py +++ b/test/test_store/test_store_berkeleydb.py @@ -1,145 +1,130 @@ -import pytest +import unittest from tempfile import mktemp -from rdflib import Dataset, URIRef +from rdflib import ConjunctiveGraph, URIRef from rdflib.store import VALID_STORE from rdflib.plugins.stores.berkeleydb import has_bsddb -pytestmark = pytest.mark.skipif( - not has_bsddb, reason="skipping berkeleydb tests, modile not available" -) - - -@pytest.fixture -def get_dataset(): - path = mktemp() - dataset = Dataset("BerkeleyDB") - rt = dataset.open(path, create=True) - assert rt == VALID_STORE, "The underlying store is corrupt" - assert ( - len(dataset) == 0 - ), "There must be zero triples in the graph just after store (file) creation" - data = """ - PREFIX : <https://example.org/> - - :a :b :c . - :d :e :f . - :d :g :h . - """ - dataset.default_graph.parse(data=data, format="ttl") - - yield path, dataset - - dataset.close() - dataset.destroy(path) - - -def test_write(get_dataset): - path, dataset = get_dataset - assert ( - len(dataset.default_graph) == 3 - ), "There must be three triples in the graph after the first data chunk parse" - data2 = """ - PREFIX : <https://example.org/> - :d :i :j . - """ - dataset.parse(data=data2, format="ttl") - assert ( - len(dataset) == 4 - ), "There must be four triples in the graph after the second data chunk parse" - data3 = """ +class BerkeleyDBTestCase(unittest.TestCase): + def setUp(self): + if not has_bsddb: + self.skipTest("skipping as berkleydb is missing") + self.store_name = "BerkeleyDB" + self.path = mktemp() + self.g = ConjunctiveGraph(store=self.store_name) + self.rt = self.g.open(self.path, create=True) + assert self.rt == VALID_STORE, "The underlying store is corrupt" + assert ( + len(self.g) == 0 + ), "There must be zero triples in the graph just after store (file) creation" + data = """ + PREFIX : <https://example.org/> + + :a :b :c . + :d :e :f . + :d :g :h . + """ + self.g.parse(data=data, format="ttl") + + def tearDown(self): + self.g.close() + + def test_write(self): + assert ( + len(self.g) == 3 + ), "There must be three triples in the graph after the first data chunk parse" + data2 = """ + PREFIX : <https://example.org/> + + :d :i :j . + """ + self.g.parse(data=data2, format="ttl") + assert ( + len(self.g) == 4 + ), "There must be four triples in the graph after the second data chunk parse" + data3 = """ + PREFIX : <https://example.org/> + + :d :i :j . + """ + self.g.parse(data=data3, format="ttl") + assert ( + len(self.g) == 4 + ), "There must still be four triples in the graph after the thrd data chunk parse" + + def test_read(self): + sx = None + for s in self.g.subjects( + predicate=URIRef("https://example.org/e"), + object=URIRef("https://example.org/f"), + ): + sx = s + assert sx == URIRef("https://example.org/d") + + def test_sparql_query(self): + q = """ PREFIX : <https://example.org/> - :d :i :j . - """ - dataset.parse(data=data3, format="ttl") - assert ( - len(dataset) == 4 - ), "There must still be four triples in the graph after the third data chunk parse" - - -def test_read(get_dataset): - path, dataset = get_dataset - sx = None - for s in dataset.subjects( - predicate=URIRef("https://example.org/e"), - object=URIRef("https://example.org/f"), - ): - sx = s - assert sx == URIRef("https://example.org/d") - - -def test_sparql_query(get_dataset): - path, dataset = get_dataset - q = """ - PREFIX : <https://example.org/> - - SELECT (COUNT(*) AS ?c) - WHERE { - :d ?p ?o . - }""" - - c = 0 - for row in dataset.query(q): - c = int(row.c) - assert c == 2, "SPARQL COUNT must return 2" - + SELECT (COUNT(*) AS ?c) + WHERE { + :d ?p ?o . + }""" -def test_sparql_insert(get_dataset): - path, dataset = get_dataset - q = """ - PREFIX : <https://example.org/> + c = 0 + for row in self.g.query(q): + c = int(row.c) + assert c == 2, "SPARQL COUNT must return 2" - INSERT DATA { - :x :y :z . - }""" + def test_sparql_insert(self): + q = """ + PREFIX : <https://example.org/> - dataset.update(q) - assert len(dataset) == 4, "After extra triple insert, length must be 4" + INSERT DATA { + :x :y :z . + }""" + self.g.update(q) + assert len(self.g) == 4, "After extra triple insert, length must be 4" -def test_multigraph(get_dataset): - path, dataset = get_dataset - q = """ - PREFIX : <https://example.org/> + def test_multigraph(self): + q = """ + PREFIX : <https://example.org/> - INSERT DATA { - GRAPH :m { - :x :y :z . - } - GRAPH :n { - :x :y :z . - } - }""" + INSERT DATA { + GRAPH :m { + :x :y :z . + } + GRAPH :n { + :x :y :z . + } + }""" - dataset.update(q) + self.g.update(q) - q = """ - SELECT (COUNT(?g) AS ?c) - WHERE { - SELECT DISTINCT ?g + q = """ + SELECT (COUNT(?g) AS ?c) WHERE { - GRAPH ?g { - ?s ?p ?o + SELECT DISTINCT ?g + WHERE { + GRAPH ?g { + ?s ?p ?o + } } } - } - """ - c = 0 - for row in dataset.query(q): - c = int(row.c) - assert c == 2, "SPARQL COUNT must return 2 (:m & :n)" - - -def test_open_shut(get_dataset): - path, dataset = get_dataset - assert len(dataset) == 3, "Initially we must have 3 triples from setUp" - dataset.close() - dataset = None - - # reopen the graph - dataset = Dataset("BerkeleyDB") - dataset.open(path, create=False) - assert ( - len(dataset) == 3 - ), "After close and reopen, we should still have the 3 originally added triples" + """ + c = 0 + for row in self.g.query(q): + c = int(row.c) + assert c == 3, "SPARQL COUNT must return 3 (default, :m & :n)" + + def test_open_shut(self): + assert len(self.g) == 3, "Initially we must have 3 triples from setUp" + self.g.close() + self.g = None + + # reopen the graph + self.g = ConjunctiveGraph("BerkeleyDB") + self.g.open(self.path, create=False) + assert ( + len(self.g) == 3 + ), "After close and reopen, we should still have the 3 originally added triples" |
