diff options
author | Diego Roversi <diego.roversi@gmail.com> | 2019-04-26 16:43:23 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-27 17:52:18 +0200 |
commit | 8ec9be0c8009b86affe588de774119c5f8e149fa (patch) | |
tree | 1879efaa9454346cdde6a6541e929e906542c837 | |
parent | ffea1da6716b199b4254dc0cf019388f90c8db1a (diff) |
sqlite3 implentation of store
-rw-r--r-- | pyapd/stores/sqlite.py | 44 | ||||
-rw-r--r-- | tests/test_sqlite_store.py | 32 |
2 files changed, 76 insertions, 0 deletions
diff --git a/pyapd/stores/sqlite.py b/pyapd/stores/sqlite.py new file mode 100644 index 0000000..a2a8145 --- /dev/null +++ b/pyapd/stores/sqlite.py @@ -0,0 +1,44 @@ +from .. import objects +from . import exceptions + +import pickle +import sqlite3 + + +class Store(): + def __init__(self): + self.db = sqlite3.connect('pyad.db') + self.objects = {} + + def add(self, obj: objects.Object): + obj_type = type(obj).__name__.lower() + c = self.db.cursor() + # cur.execute("insert into pickled(data) values (?)", (sqlite3.Binary(pickle.dumps(p1, protocol=2)),)) + c.execute('insert or replace into objects(type, oid, obj) values ( :type, :oid, :obj )', + (obj_type, obj.ap_id, sqlite3.Binary(pickle.dumps(obj, protocol=2)))) + self.db.commit() + c.close() + +# if obj_type not in self.objects: +# self.objects[obj_type] = {} +# self.objects[obj_type][obj.ap_id] = obj + + def get(self, obj_type: str, oid: str) -> objects.Object: + c = self.db.cursor() + c.execute( + 'select obj from objects where type = :type and oid = :oid', (obj_type, oid)) + row = c.fetchone() + if row is None: + raise exceptions.DoesNotExist( + "An object with id {} does not exist".format(oid)) + return pickle.loads(row[0]) + + def createdb(self): + c = self.db.cursor() + c.execute('create table objects(type text, oid text, obj blob)') + c.execute('create index idx on objects(type, oid)') + + +if __name__ == '__main__': + s = Store() + s.createdb() diff --git a/tests/test_sqlite_store.py b/tests/test_sqlite_store.py new file mode 100644 index 0000000..849c7a2 --- /dev/null +++ b/tests/test_sqlite_store.py @@ -0,0 +1,32 @@ +import unittest + +from pyapd import objects +from pyapd.stores import sqlite, exceptions + + +class TestSqliteStore(unittest.TestCase): + def setUp(self): + self.store = sqlite.Store() + self.oid = 'https://test/object/12345' + self.obj = objects.Object(id=self.oid) + self.store.add(self.obj) + + def test_add_object(self): + oid = 'https://test/object/12345' + obj = objects.Object(id=oid) + self.store.add(obj) + self.assertIn('object', self.store.objects) + self.assertIn(oid, self.store.objects['object']) + + def test_get_object(self): + res = self.store.get('object', self.oid) + self.assertEqual(res, self.obj) + + def test_get_object_not_existing(self): + oid = 'https://test/object/does_not_exist' + with self.assertRaises(exceptions.DoesNotExist): + self.store.get('object', oid) + + def test_get_object_wrong_type(self): + with self.assertRaises(exceptions.UnknownObjectType): + self.store.get('no_such_type', self.oid) |