| 1 |
import unittest |
| 2 |
|
| 3 |
try: |
| 4 |
from unittest.mock import patch, MagicMock |
| 5 |
except ImportError: |
| 6 |
from mock import patch, MagicMock |
| 7 |
|
| 8 |
import six |
| 9 |
|
| 10 |
from rdflib import ( |
| 11 |
ConjunctiveGraph, |
| 12 |
Literal, |
| 13 |
URIRef, |
| 14 |
plugin |
| 15 |
) |
| 16 |
from rdflib.store import Store |
| 17 |
|
| 18 |
from rdflib_sqlalchemy import registerplugins |
| 19 |
from sqlalchemy.sql.selectable import Select |
| 20 |
|
| 21 |
|
| 22 |
michel = URIRef(u"michel") |
| 23 |
likes = URIRef(u"likes") |
| 24 |
pizza = URIRef(u"pizza") |
| 25 |
|
| 26 |
|
| 27 |
class mock_cursor(): |
| 28 |
def execute(x): |
| 29 |
raise Exception("Forced exception") |
| 30 |
|
| 31 |
|
| 32 |
class ConfigTest(unittest.TestCase): |
| 33 |
''' |
| 34 |
Test configuration with a dict |
| 35 |
''' |
| 36 |
|
| 37 |
def setUp(self): |
| 38 |
self.store = plugin.get("SQLAlchemy", Store)() |
| 39 |
self.graph = ConjunctiveGraph(self.store) |
| 40 |
|
| 41 |
def tearDown(self): |
| 42 |
self.graph.close() |
| 43 |
|
| 44 |
def test_success(self): |
| 45 |
with patch('rdflib_sqlalchemy.store.sqlalchemy') as p: |
| 46 |
self.graph.open({'url': 'sqlite://', 'random_key': 'something'}, create=True) |
| 47 |
p.create_engine.assert_called_with('sqlite://', random_key='something') |
| 48 |
|
| 49 |
def test_no_url(self): |
| 50 |
with patch('rdflib_sqlalchemy.store.sqlalchemy'): |
| 51 |
with six.assertRaisesRegex(self, Exception, '.*url.*'): |
| 52 |
self.graph.open({'random_key': 'something'}, create=True) |
| 53 |
|
| 54 |
|
| 55 |
class SQLATestCase(unittest.TestCase): |
| 56 |
identifier = URIRef("rdflib_test") |
| 57 |
dburi = Literal("sqlite://") |
| 58 |
|
| 59 |
def setUp(self): |
| 60 |
self.store = plugin.get( |
| 61 |
"SQLAlchemy", Store)(identifier=self.identifier, configuration=self.dburi) |
| 62 |
self.graph = ConjunctiveGraph(self.store, identifier=self.identifier) |
| 63 |
self.graph.open(self.dburi, create=True) |
| 64 |
|
| 65 |
def tearDown(self): |
| 66 |
self.graph.destroy(self.dburi) |
| 67 |
self.graph.close() |
| 68 |
|
| 69 |
def test_registerplugins(self): |
| 70 |
|
| 71 |
|
| 72 |
registerplugins() |
| 73 |
self.assertIsNotNone(plugin.get("SQLAlchemy", Store)) |
| 74 |
p = plugin._plugins |
| 75 |
self.assertIn(("SQLAlchemy", Store), p) |
| 76 |
del p[("SQLAlchemy", Store)] |
| 77 |
plugin._plugins = p |
| 78 |
registerplugins() |
| 79 |
self.assertIn(("SQLAlchemy", Store), p) |
| 80 |
|
| 81 |
def test_namespaces(self): |
| 82 |
self.assertNotEqual(list(self.graph.namespaces()), []) |
| 83 |
|
| 84 |
def test_contexts_without_triple(self): |
| 85 |
self.assertEqual(list(self.graph.contexts()), []) |
| 86 |
|
| 87 |
def test_contexts_result(self): |
| 88 |
ctx_id = URIRef('http://example.org/context') |
| 89 |
g = self.graph.get_context(ctx_id) |
| 90 |
g.add((michel, likes, pizza)) |
| 91 |
actual = list(self.store.contexts()) |
| 92 |
self.assertEqual(actual[0], ctx_id) |
| 93 |
|
| 94 |
def test_contexts_with_triple(self): |
| 95 |
statemnt = (michel, likes, pizza) |
| 96 |
self.assertEqual(list(self.graph.contexts(triple=statemnt)), []) |
| 97 |
|
| 98 |
def test__len(self): |
| 99 |
self.assertEqual(self.store.__len__(), 0) |
| 100 |
|
| 101 |
def test__remove_context(self): |
| 102 |
ctx_id = URIRef('http://example.org/context') |
| 103 |
g = self.graph.get_context(ctx_id) |
| 104 |
g.add((michel, likes, pizza)) |
| 105 |
self.store._remove_context(g) |
| 106 |
self.assertEqual(list(self.store.contexts()), []) |
| 107 |
|
| 108 |
def test_triples_choices(self): |
| 109 |
|
| 110 |
p = MagicMock(name='engine') |
| 111 |
self.store.engine = p |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
self.store.STRONGLY_TYPED_TERMS = True |
| 116 |
|
| 117 |
self.store.max_terms_per_where = 2 |
| 118 |
|
| 119 |
for x in self.store.triples_choices((None, likes, [michel, pizza, likes])): |
| 120 |
pass |
| 121 |
args = p.connect().__enter__().execute.call_args[0] |
| 122 |
children = args[0].get_children(column_collections=False) |
| 123 |
|
| 124 |
self.assertEqual(sum(1 for c in children if isinstance(c, Select)), 2) |
| 125 |
|
| 126 |
def test_quoted_statements(self): |
| 127 |
''' |
| 128 |
Regression test for RDFLib/rdflib-sqlalchemy#92 |
| 129 |
''' |
| 130 |
cg = ConjunctiveGraph('SQLAlchemy') |
| 131 |
cg.open('sqlite://', create=True) |
| 132 |
testN3 = """ |
| 133 |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 134 |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 135 |
@prefix : <http://test/> . |
| 136 |
{:a :b :c;a :foo} => {:a :d :c,?y}. |
| 137 |
_:foo a rdfs:Class. |
| 138 |
:a :d :c.""" |
| 139 |
cg.parse(data=testN3, format='n3') |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
unittest.main() |