From 6563353bbf4b934d2be963d8d2b9a6e14f9df356 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 1 Nov 2014 00:36:02 -0400 Subject: bunch more tests --- t/__init__.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 't/__init__.py') diff --git a/t/__init__.py b/t/__init__.py index e69de29..a714b9e 100644 --- a/t/__init__.py +++ b/t/__init__.py @@ -0,0 +1,55 @@ +import json +import mop + +CLASS_REGISTRY = {} + +class InMemoryDatabase(object): + def __init__(self): + self.store = {} + self.class_registry = {} + + def register_class(self, c): + self.class_registry[c.get_name()] = c + + def insert(self, name, obj): + data = self._get_repr(obj) + self.store[name] = json.dumps( + data, + separators=(',', ':'), + sort_keys=True + ) + + def lookup(self, name): + if name in self.store: + data = json.loads(self.store[name]) + if data["type"] == "plain": + return data["data"] + elif data["type"] == "object": + metaclass = self.class_registry[data["class"]] + return metaclass.create_instance(data["data"]) + else: + raise Exception("unknown object type") + else: + raise Exception("object not in database") + + def _get_repr(self, obj): + if type(obj) == type([]): + return { "type": "plain", "data": obj } + if type(obj) == type({}): + return { "type": "plain", "data": obj } + if type(obj) == type(""): + return { "type": "plain", "data": obj } + if type(obj) == type(0): + return { "type": "plain", "data": obj } + if type(obj) == type(True): + return { "type": "plain", "data": obj } + if type(obj) == type(None): + return { "type": "plain", "data": obj } + if hasattr(obj, 'isa') and obj.isa(mop.Object): + self.register_class(obj.metaclass) + return { + "type": "object", + "class": obj.metaclass.get_name(), + "data": obj.slots, + } + raise Exception("unknown object type") -- cgit v1.2.3-54-g00ecf