123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850 |
- import pdb
- import pytest
- from shutil import rmtree
- from rdflib import Graph, Namespace, URIRef
- from lakesuperior.model.rdf.graph import Graph
- from lakesuperior.store.ldp_rs.lmdb_store import LmdbStore
- @pytest.fixture(scope='class')
- def store():
- """
- Test LMDB store.
- This store has a different life cycle than the one used for tests in higher
- levels of the stack and is not bootstrapped (i.e. starts completely empty).
- """
- env_path = '/tmp/test_lmdbstore'
- # Remove previous test DBs
- rmtree(env_path, ignore_errors=True)
- store = LmdbStore(env_path)
- yield store
- store.close()
- store.destroy()
- @pytest.fixture(scope='class')
- def trp():
- return (
- (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
- # Exact same as [0].
- (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
- # NOTE: s and o are in reversed order.
- (URIRef('urn:o:0'), URIRef('urn:p:0'), URIRef('urn:s:0')),
- (URIRef('urn:s:0'), URIRef('urn:p:1'), URIRef('urn:o:0')),
- (URIRef('urn:s:0'), URIRef('urn:p:1'), URIRef('urn:o:1')),
- (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:1')),
- (URIRef('urn:s:1'), URIRef('urn:p:2'), URIRef('urn:o:2')),
- )
- @pytest.mark.usefixtures('trp')
- @pytest.mark.usefixtures('store')
- class TestGraphInit:
- """
- Test initialization of graphs with different base data sets.
- """
- def test_empty(self, store):
- """
- Test creation of an empty graph.
- """
- # No transaction needed to init an empty graph.
- gr = Graph(store)
- # len() should not need a DB transaction open.
- assert len(gr) == 0
- def test_init_triples(self, trp, store):
- """
- Test creation using a Python set.
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- assert len(gr) == 6
- for t in trp:
- assert t in gr
- @pytest.mark.usefixtures('trp')
- @pytest.mark.usefixtures('store')
- class TestGraphLookup:
- """
- Test triple lookup.
- """
- def test_lookup_all_unbound(self, trp, store):
- """
- Test lookup ? ? ? (all unbound)
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((None, None, None))
- assert len(flt_gr) == 6
- assert trp[0] in flt_gr
- assert trp[2] in flt_gr
- assert trp[3] in flt_gr
- assert trp[4] in flt_gr
- assert trp[5] in flt_gr
- assert trp[6] in flt_gr
- def test_lookup_s(self, trp, store):
- """
- Test lookup s ? ?
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((URIRef('urn:s:0'), None, None))
- assert len(flt_gr) == 3
- assert trp[0] in flt_gr
- assert trp[3] in flt_gr
- assert trp[4] in flt_gr
- assert trp[2] not in flt_gr
- assert trp[5] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup((URIRef('urn:s:8'), None, None))
- assert len(empty_flt_gr) == 0
- def test_lookup_p(self, trp, store):
- """
- Test lookup ? p ?
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((None, URIRef('urn:p:0'), None))
- assert len(flt_gr) == 2
- assert trp[0] in flt_gr
- assert trp[2] in flt_gr
- assert trp[3] not in flt_gr
- assert trp[4] not in flt_gr
- assert trp[5] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup((None, URIRef('urn:p:8'), None))
- assert len(empty_flt_gr) == 0
- def test_lookup_o(self, trp, store):
- """
- Test lookup ? ? o
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((None, None, URIRef('urn:o:1')))
- assert len(flt_gr) == 2
- assert trp[4] in flt_gr
- assert trp[5] in flt_gr
- assert trp[0] not in flt_gr
- assert trp[2] not in flt_gr
- assert trp[3] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup((None, None, URIRef('urn:o:8')))
- assert len(empty_flt_gr) == 0
- def test_lookup_sp(self, trp, store):
- """
- Test lookup s p ?
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((URIRef('urn:s:0'), URIRef('urn:p:1'), None))
- assert len(flt_gr) == 2
- assert trp[3] in flt_gr
- assert trp[4] in flt_gr
- assert trp[0] not in flt_gr
- assert trp[2] not in flt_gr
- assert trp[5] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup((URIRef('urn:s:0'), URIRef('urn:p:2'), None))
- assert len(empty_flt_gr) == 0
- def test_lookup_so(self, trp, store):
- """
- Test lookup s ? o
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((URIRef('urn:s:0'), None, URIRef('urn:o:0')))
- assert len(flt_gr) == 2
- assert trp[0] in flt_gr
- assert trp[3] in flt_gr
- assert trp[2] not in flt_gr
- assert trp[4] not in flt_gr
- assert trp[5] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup((URIRef('urn:s:0'), None, URIRef('urn:o:2')))
- assert len(empty_flt_gr) == 0
- def test_lookup_po(self, trp, store):
- """
- Test lookup ? p o
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup((None, URIRef('urn:p:1'), URIRef('urn:o:1')))
- assert len(flt_gr) == 2
- assert trp[4] in flt_gr
- assert trp[5] in flt_gr
- assert trp[0] not in flt_gr
- assert trp[2] not in flt_gr
- assert trp[3] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup((None, URIRef('urn:p:1'), URIRef('urn:o:2')))
- assert len(empty_flt_gr) == 0
- def test_lookup_spo(self, trp, store):
- """
- Test lookup s p o
- """
- with store.txn_ctx():
- gr = Graph(store, data=set(trp))
- flt_gr = gr.lookup(
- (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:1'))
- )
- assert len(flt_gr) == 1
- assert trp[5] in flt_gr
- assert trp[0] not in flt_gr
- assert trp[2] not in flt_gr
- assert trp[3] not in flt_gr
- assert trp[4] not in flt_gr
- assert trp[6] not in flt_gr
- # Test for empty results.
- empty_flt_gr = gr.lookup(
- (URIRef('urn:s:1'), URIRef('urn:p:1'), URIRef('urn:o:2'))
- )
- assert len(empty_flt_gr) == 0
- @pytest.mark.usefixtures('trp')
- @pytest.mark.usefixtures('store')
- class TestGraphSlicing:
- """
- Test triple lookup.
- """
- # TODO
- pass
- @pytest.mark.usefixtures('trp')
- @pytest.mark.usefixtures('store')
- class TestGraphOps:
- """
- Test various graph operations.
- """
- def test_len(self, trp, store):
- """
- Test the length of a graph with and without duplicates.
- """
- with store.txn_ctx():
- gr = Graph(store)
- assert len(gr) == 0
- gr.add((trp[0],))
- assert len(gr) == 1
- gr.add((trp[1],)) # Same values
- assert len(gr) == 1
- gr.add((trp[2],))
- assert len(gr) == 2
- gr.add(trp)
- assert len(gr) == 6
- def test_dup(self, trp, store):
- """
- Test operations with duplicate triples.
- """
- with store.txn_ctx():
- gr = Graph(store)
- gr.add((trp[0],))
- assert trp[1] in gr
- assert trp[2] not in gr
- def test_remove(self, trp, store):
- """
- Test adding and removing triples.
- """
- with store.txn_ctx():
- gr = Graph(store)
- gr.add(trp)
- gr.remove(trp[0])
- assert len(gr) == 5
- assert trp[0] not in gr
- assert trp[1] not in gr
- # This is the duplicate triple.
- gr.remove(trp[1])
- assert len(gr) == 5
- # This is the triple in reverse order.
- gr.remove(trp[2])
- assert len(gr) == 4
- gr.remove(trp[4])
- assert len(gr) == 3
- def test_union(self, trp, store):
- """
- Test graph union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:3]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr3 = gr1 | gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- def test_ip_union(self, trp, store):
- """
- Test graph in-place union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:3]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr1 |= gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- def test_addition(self, trp, store):
- """
- Test graph addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:3]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr3 = gr1 + gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- def test_ip_addition(self, trp, store):
- """
- Test graph in-place addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:3]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr1 += gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- def test_subtraction(self, trp, store):
- """
- Test graph addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:4]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr3 = gr1 - gr2
- assert len(gr3) == 1
- assert trp[0] in gr3
- assert trp[1] in gr3
- assert trp[2] not in gr3
- assert trp[3] not in gr3
- assert trp[4] not in gr3
- gr3 = gr2 - gr1
- assert len(gr3) == 2
- assert trp[0] not in gr3
- assert trp[1] not in gr3
- assert trp[2] not in gr3
- assert trp[3] not in gr3
- assert trp[4] in gr3
- assert trp[5] in gr3
- def test_ip_subtraction(self, trp, store):
- """
- Test graph in-place addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:4]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr1 -= gr2
- assert len(gr1) == 1
- assert trp[0] in gr1
- assert trp[1] in gr1
- assert trp[2] not in gr1
- assert trp[3] not in gr1
- assert trp[4] not in gr1
- def test_intersect(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:4]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr3 = gr1 & gr2
- assert len(gr3) == 2
- assert trp[2] in gr3
- assert trp[3] in gr3
- assert trp[0] not in gr3
- assert trp[5] not in gr3
- def test_ip_intersect(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:4]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr1 &= gr2
- assert len(gr1) == 2
- assert trp[2] in gr1
- assert trp[3] in gr1
- assert trp[0] not in gr1
- assert trp[5] not in gr1
- def test_xor(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:4]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr3 = gr1 ^ gr2
- assert len(gr3) == 3
- assert trp[2] not in gr3
- assert trp[3] not in gr3
- assert trp[0] in gr3
- assert trp[5] in gr3
- def test_ip_xor(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:4]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr1 ^= gr2
- assert len(gr1) == 3
- assert trp[2] not in gr1
- assert trp[3] not in gr1
- assert trp[0] in gr1
- assert trp[5] in gr1
- @pytest.mark.usefixtures('trp')
- @pytest.mark.usefixtures('store')
- class TestNamedGraphOps:
- """
- Test various operations on a named graph.
- """
- def test_len(self, trp, store):
- """
- Test the length of a graph with and without duplicates.
- """
- imr = Graph(store, uri='http://example.edu/imr01')
- assert len(imr) == 0
- with store.txn_ctx():
- imr.add((trp[0],))
- assert len(imr) == 1
- imr.add((trp[1],)) # Same values
- assert len(imr) == 1
- imr.add((trp[2],))
- assert len(imr) == 2
- imr.add(trp)
- assert len(imr) == 6
- def test_dup(self, trp, store):
- """
- Test operations with duplicate triples.
- """
- imr = Graph(store, uri='http://example.edu/imr01')
- with store.txn_ctx():
- imr.add((trp[0],))
- assert trp[1] in imr
- assert trp[2] not in imr
- def test_remove(self, trp, store):
- """
- Test adding and removing triples.
- """
- with store.txn_ctx():
- imr = Graph(store, uri='http://example.edu/imr01', data={*trp})
- imr.remove(trp[0])
- assert len(imr) == 5
- assert trp[0] not in imr
- assert trp[1] not in imr
- # This is the duplicate triple.
- imr.remove(trp[1])
- assert len(imr) == 5
- # This is the triple in reverse order.
- imr.remove(trp[2])
- assert len(imr) == 4
- imr.remove(trp[4])
- assert len(imr) == 3
- def test_union(self, trp, store):
- """
- Test graph union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr3 = gr1 | gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- assert gr3.uri == None
- def test_ip_union(self, trp, store):
- """
- Test graph in-place union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr1 |= gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- assert gr1.uri == URIRef('http://example.edu/imr01')
- def test_addition(self, trp, store):
- """
- Test graph addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr3 = gr1 + gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- assert gr3.uri == None
- def test_ip_addition(self, trp, store):
- """
- Test graph in-place addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr1 += gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- assert gr1.uri == URIRef('http://example.edu/imr01')
- def test_subtraction(self, trp, store):
- """
- Test graph addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr3 = gr1 - gr2
- assert len(gr3) == 1
- assert trp[0] in gr3
- assert trp[1] in gr3
- assert trp[2] not in gr3
- assert trp[3] not in gr3
- assert trp[4] not in gr3
- assert gr3.uri == None
- gr3 = gr2 - gr1
- assert len(gr3) == 2
- assert trp[0] not in gr3
- assert trp[1] not in gr3
- assert trp[2] not in gr3
- assert trp[3] not in gr3
- assert trp[4] in gr3
- assert trp[5] in gr3
- assert gr3.uri == None
- def test_ip_subtraction(self, trp, store):
- """
- Test graph in-place addition.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr1 -= gr2
- assert len(gr1) == 1
- assert trp[0] in gr1
- assert trp[1] in gr1
- assert trp[2] not in gr1
- assert trp[3] not in gr1
- assert trp[4] not in gr1
- assert gr1.uri == URIRef('http://example.edu/imr01')
- def test_intersect(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr3 = gr1 & gr2
- assert len(gr3) == 2
- assert trp[2] in gr3
- assert trp[3] in gr3
- assert trp[0] not in gr3
- assert trp[5] not in gr3
- assert gr3.uri == None
- def test_ip_intersect(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr1 &= gr2
- assert len(gr1) == 2
- assert trp[2] in gr1
- assert trp[3] in gr1
- assert trp[0] not in gr1
- assert trp[5] not in gr1
- assert gr1.uri == URIRef('http://example.edu/imr01')
- def test_xor(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr3 = gr1 ^ gr2
- assert len(gr3) == 3
- assert trp[2] not in gr3
- assert trp[3] not in gr3
- assert trp[0] in gr3
- assert trp[5] in gr3
- assert gr3.uri == None
- def test_ip_xor(self, trp, store):
- """
- Test graph intersextion.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:4]})
- gr2 = Graph(store, uri='http://example.edu/imr02', data={*trp[2:6]})
- gr1 ^= gr2
- assert len(gr1) == 3
- assert trp[2] not in gr1
- assert trp[3] not in gr1
- assert trp[0] in gr1
- assert trp[5] in gr1
- assert gr1.uri == URIRef('http://example.edu/imr01')
- @pytest.mark.usefixtures('trp')
- @pytest.mark.usefixtures('store')
- class TestHybridOps:
- """
- Test operations between IMR and graph.
- """
- def test_hybrid_union(self, trp, store):
- """
- Test hybrid IMR + graph union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr3 = gr1 | gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- assert isinstance(gr3, Graph)
- assert gr3.uri == None
- gr4 = gr2 | gr1
- assert isinstance(gr4, Graph)
- assert gr3 == gr4
- def test_ip_union_imr(self, trp, store):
- """
- Test IMR + graph in-place union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, uri='http://example.edu/imr01', data={*trp[:3]})
- gr2 = Graph(store, data={*trp[2:6]})
- gr1 |= gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- assert gr1.uri == URIRef('http://example.edu/imr01')
- def test_ip_union_gr(self, trp, store):
- """
- Test graph + IMR in-place union.
- """
- with store.txn_ctx():
- gr1 = Graph(store, data={*trp[:3]})
- gr2 = Graph(store, uri='http://example.edu/imr01', data={*trp[2:6]})
- gr1 |= gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- assert isinstance(gr1, Graph)
|