123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- import pytest
- from shutil import rmtree
- from rdflib import Graph, Namespace, URIRef
- from lakesuperior.model.graph.graph import SimpleGraph, Imr
- 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'
-
- 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')),
-
- (URIRef('urn:s:0'), URIRef('urn:p:0'), URIRef('urn:o:0')),
-
- (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):
- """
- Test creation of an empty graph.
- """
- gr = SimpleGraph()
- assert len(gr) == 0
- def test_init_triples(self, trp):
- """
- Test creation using a Python set.
- """
- gr = SimpleGraph(data=set(trp))
- assert len(gr) == 6
- for t in trp:
- assert t in gr
- @pytest.mark.usefixtures('trp')
- class TestGraphLookup:
- """
- Test triple lookup.
- TODO
- """
- @pytest.mark.skip(reason='TODO')
- def test_lookup_pattern(self, trp):
- """
- Test lookup by basic pattern.
- """
- pass
- @pytest.mark.usefixtures('trp')
- class TestGraphOps:
- """
- Test various graph operations.
- """
- def test_len(self, trp):
- """
- Test the length of a graph with and without duplicates.
- """
- gr = SimpleGraph()
- assert len(gr) == 0
- gr.add((trp[0],))
- assert len(gr) == 1
- gr.add((trp[1],))
- assert len(gr) == 1
- gr.add((trp[2],))
- assert len(gr) == 2
- gr.add(trp)
- assert len(gr) == 6
- def test_dup(self, trp):
- """
- Test operations with duplicate triples.
- """
- gr = SimpleGraph()
-
- gr.add((trp[0],))
- assert trp[1] in gr
- assert trp[2] not in gr
- def test_remove(self, trp):
- """
- Test adding and removing triples.
- """
- gr = SimpleGraph()
- gr.add(trp)
- gr.remove(trp[0])
- assert len(gr) == 5
- assert trp[0] not in gr
- assert trp[1] not in gr
-
- gr.remove(trp[1])
- assert len(gr) == 5
-
- gr.remove(trp[2])
- assert len(gr) == 4
- gr.remove(trp[4])
- assert len(gr) == 3
- def test_union(self, trp):
- """
- Test graph union.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:3])
- gr2.add(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):
- """
- Test graph in-place union.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:3])
- gr2.add(trp[2:6])
- gr1 |= gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- def test_addition(self, trp):
- """
- Test graph addition.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:3])
- gr2.add(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):
- """
- Test graph in-place addition.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:3])
- gr2.add(trp[2:6])
- gr1 += gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- def test_subtraction(self, trp):
- """
- Test graph addition.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph in-place addition.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph intersextion.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph intersextion.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph intersextion.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph intersextion.
- """
- gr1 = SimpleGraph()
- gr2 = SimpleGraph()
- gr1.add(trp[0:4])
- gr2.add(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')
- class TestImrOps:
- """
- Test various graph operations.
- """
- def test_len(self, trp):
- """
- Test the length of a graph with and without duplicates.
- """
- imr = Imr(uri='http://example.edu/imr01')
- assert len(imr) == 0
- imr.add((trp[0],))
- assert len(imr) == 1
- imr.add((trp[1],))
- assert len(imr) == 1
- imr.add((trp[2],))
- assert len(imr) == 2
- imr.add(trp)
- assert len(imr) == 6
- def test_dup(self, trp):
- """
- Test operations with duplicate triples.
- """
- imr = Imr(uri='http://example.edu/imr01')
-
- imr.add((trp[0],))
- assert trp[1] in imr
- assert trp[2] not in imr
- def test_remove(self, trp):
- """
- Test adding and removing triples.
- """
- imr = Imr(uri='http://example.edu/imr01')
- imr.add(trp)
- imr.remove(trp[0])
- assert len(imr) == 5
- assert trp[0] not in imr
- assert trp[1] not in imr
-
- imr.remove(trp[1])
- assert len(imr) == 5
-
- imr.remove(trp[2])
- assert len(imr) == 4
- imr.remove(trp[4])
- assert len(imr) == 3
- def test_union(self, trp):
- """
- Test graph union.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:3])
- gr2.add(trp[2:6])
- gr3 = gr1 | gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- assert gr3.uri == URIRef('http://example.edu/imr01')
- def test_ip_union(self, trp):
- """
- Test graph in-place union.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:3])
- gr2.add(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):
- """
- Test graph addition.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:3])
- gr2.add(trp[2:6])
- gr3 = gr1 + gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- assert gr3.uri == URIRef('http://example.edu/imr01')
- def test_ip_addition(self, trp):
- """
- Test graph in-place addition.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:3])
- gr2.add(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):
- """
- Test graph addition.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:4])
- gr2.add(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 == URIRef('http://example.edu/imr01')
- 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 == URIRef('http://example.edu/imr02')
- def test_ip_subtraction(self, trp):
- """
- Test graph in-place addition.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph intersextion.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:4])
- gr2.add(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 == URIRef('http://example.edu/imr01')
- def test_ip_intersect(self, trp):
- """
- Test graph intersextion.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:4])
- gr2.add(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):
- """
- Test graph intersextion.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:4])
- gr2.add(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 == URIRef('http://example.edu/imr01')
- def test_ip_xor(self, trp):
- """
- Test graph intersextion.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = Imr(uri='http://example.edu/imr02')
- gr1.add(trp[0:4])
- gr2.add(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')
- class TestHybridOps:
- """
- Test operations between IMR and graph.
- """
- def test_union(self, trp):
- """
- Test hybrid IMR + graph union.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = SimpleGraph()
- gr1.add(trp[0:3])
- gr2.add(trp[2:6])
- gr3 = gr1 | gr2
- assert len(gr3) == 5
- assert trp[0] in gr3
- assert trp[4] in gr3
- assert isinstance(gr3, Imr)
- assert gr3.uri == URIRef('http://example.edu/imr01')
- gr4 = gr2 | gr1
- assert isinstance(gr4, SimpleGraph)
- assert gr3 == gr4
- def test_ip_union_imr(self, trp):
- """
- Test IMR + graph in-place union.
- """
- gr1 = Imr(uri='http://example.edu/imr01')
- gr2 = SimpleGraph()
- gr1.add(trp[0:3])
- gr2.add(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):
- """
- Test graph + IMR in-place union.
- """
- gr1 = SimpleGraph()
- gr2 = Imr(uri='http://example.edu/imr01')
- gr1.add(trp[0:3])
- gr2.add(trp[2:6])
- gr1 |= gr2
- assert len(gr1) == 5
- assert trp[0] in gr1
- assert trp[4] in gr1
- assert isinstance(gr1, SimpleGraph)
|