diff options
-rw-r--r-- | pyapd/objects.py | 2 | ||||
-rw-r--r-- | pyapd/stores/exceptions.py | 11 | ||||
-rw-r--r-- | pyapd/stores/memory.py | 25 | ||||
-rw-r--r-- | tests/test_memory_store.py | 32 | ||||
-rw-r--r-- | tests/test_objects.py | 6 |
5 files changed, 72 insertions, 4 deletions
diff --git a/pyapd/objects.py b/pyapd/objects.py index 4a97200..6750b09 100644 --- a/pyapd/objects.py +++ b/pyapd/objects.py @@ -1,7 +1,7 @@ import json -class ActivityObject(): +class Object(): PROPERTIES = [ 'id', 'type', diff --git a/pyapd/stores/exceptions.py b/pyapd/stores/exceptions.py new file mode 100644 index 0000000..644a237 --- /dev/null +++ b/pyapd/stores/exceptions.py @@ -0,0 +1,11 @@ + +class DoesNotExist(Exception): + """ + Raised when an object is not found in the store. + """ + + +class UnknownObjectType(Exception): + """ + Raised when an unknown object is referred. + """ diff --git a/pyapd/stores/memory.py b/pyapd/stores/memory.py new file mode 100644 index 0000000..0b1140c --- /dev/null +++ b/pyapd/stores/memory.py @@ -0,0 +1,25 @@ +from .. import objects +from . import exceptions + + +class Store(): + def __init__(self): + self.objects = {} + + def add(self, obj: objects.Object): + obj_type = type(obj).__name__.lower() + 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: + if obj_type not in self.objects: + raise exceptions.UnknownObjectType( + "Object type {} is unknown".format(obj_type) + ) + try: + return self.objects[obj_type][oid] + except KeyError: + raise exceptions.DoesNotExist( + "An object with id {} does not exist".format(oid) + ) diff --git a/tests/test_memory_store.py b/tests/test_memory_store.py new file mode 100644 index 0000000..8c19954 --- /dev/null +++ b/tests/test_memory_store.py @@ -0,0 +1,32 @@ +import unittest + +from pyapd import objects +from pyapd.stores import memory, exceptions + + +class TestMemoryStore(unittest.TestCase): + def setUp(self): + self.store = memory.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) diff --git a/tests/test_objects.py b/tests/test_objects.py index 68bc900..4358e41 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -6,14 +6,14 @@ from pyapd import objects class TestObjects(unittest.TestCase): def test_activity_from_json(self): - act = objects.ActivityObject.from_jsons('''{ + act = objects.Object.from_jsons('''{ "id": "http://example.org/123456" }''') - self.assertIsInstance(act, objects.ActivityObject) + self.assertIsInstance(act, objects.Object) self.assertEqual(act.ap_id, 'http://example.org/123456') def test_activity_to_json(self): - act = objects.ActivityObject( + act = objects.Object( id="http://example.org/123456" ) data = act.to_jsons() |