def testNoneInputs(self): class c(object): a = "HERE" def __init__(self): self.b = "THERE" obj = c() self.assertEquals("HERE", get_prop(obj, "a")) self.assertEquals("THERE", get_prop(obj, "b")) self.assertIsNone(get_prop(obj, None)) self.assertIsNone(get_prop(None, "a")) self.assertIsNone(get_prop(None, None))
def _checkIndexedClass(self, cls, expected_indexes): #Pretty white-box, I know #Force a lazy init and grab the wrapper cls._lazyWrapper() wrapper = cls._dbWrapper #Insure we're getting the right indexes cls_indexes = sorted(get_prop(cls, "INDEXES")) self.assertEquals(expected_indexes, cls_indexes) #Indexes from class self.assertEquals(cls_indexes, sorted(wrapper._listIndexes(IndexedSimple))) #The actual DB should index _id, classId, and our indexes expected = sorted(cls_indexes + ['_id', 'classId']) #checkIndexes should have already been called - so we can check #the DB directly for the indexes db_indexes = [] found = wrapper._collection.index_information() for name, index in found.iteritems(): keys = [k for k, direct in index.get('key', [])] self.assertEquals(1, len(keys)) db_indexes.append(keys[0]) self.assertEquals(expected, sorted(db_indexes))
def _checkIndexedClass(self, cls, expected_indexes): #Pretty white-box, I know #Force a lazy init and grab the wrapper cls._lazyWrapper() wrapper = cls._dbWrapper #Insure we're getting the right indexes cls_indexes = sorted(get_prop(cls, "INDEXES")) self.assertEquals(expected_indexes, cls_indexes) #Indexes from class self.assertEquals(cls_indexes, sorted(wrapper._listIndexes(IndexedSimple))) #The actual DB should index _id, classId, and our indexes expected = sorted(cls_indexes + ['_id', 'classId']) #checkIndexes should have already been called - so we can check #the DB directly for the indexes db_indexes = [] found = wrapper._collection.index_information() for name,index in found.iteritems(): keys = [k for k,direct in index.get('key', [])] self.assertEquals(1, len(keys)) db_indexes.append(keys[0]) self.assertEquals(expected, sorted(db_indexes))
def testList(self): class tester(object): def __init__(self): self.a = ["I am a"] self._c = ["I am _c"] self.D = ["I am D"] self._F = ["I am _F"] def getB(self): return ["I am getB"] def gete(self): return ["I am gete"] def deepfunc(self): def f1(): def f2(): return ["I am deepfunc o f1 o f2"] + self.a return f2 return f1 def yielder(self): yield "I Yielded" def yielder2(self): def f1(): yield "I Yielded in a subfunc" return f1 t = tester() self.assertIsNone(get_prop(t, "XYZ", list)) self.assertEquals(["I am a"], get_prop(t, "a", list)) self.assertEquals(["I am getB"], get_prop(t, "b", list)) self.assertEquals(["I am _c"], get_prop(t, "c", list)) self.assertEquals(["I am D"], get_prop(t, "d", list)) self.assertEquals(["I am gete"], get_prop(t, "e", list)) self.assertEquals(["I am _F"], get_prop(t, "f", list)) self.assertEquals(["I am deepfunc o f1 o f2", "I am a"], get_prop(t, "deepfunc", list)) self.assertEquals(["I Yielded"], get_prop(t, "yielder", list)) self.assertEquals(["I Yielded in a subfunc"], get_prop(t, "yielder2", list))
def testVanilla(self): class tester(object): def __init__(self): self.a = "I am a" self._c = "I am _c" self.D = "I am D" self._F = "I am _F" def getB(self): return "I am getB" def gete(self): return "I am gete" def deepfunc(self): def f1(): def f2(): return "I am deepfunc o f1 o f2 " + self.a return f2 return f1 def yielder(self): yield "I Yielded" def yielder2(self): def f1(): yield "I Yielded in a subfunc" return f1 t = tester() self.assertIsNone(get_prop(t, "XYZ")) self.assertEquals("I am a", get_prop(t, "a")) self.assertEquals("I am getB", get_prop(t, "b")) self.assertEquals("I am _c", get_prop(t, "c")) self.assertEquals("I am D", get_prop(t, "d")) self.assertEquals("I am gete", get_prop(t, "e")) self.assertEquals("I am _F", get_prop(t, "f")) self.assertEquals("I am deepfunc o f1 o f2 I am a", get_prop(t, "deepfunc")) self.assertEquals(["I Yielded"], list(get_prop(t, "yielder"))) self.assertEquals(["I Yielded in a subfunc"], list(get_prop(t, "yielder2")))
def _listIndexes(self, cls): """ Helper to examine the given class and identifying all index names. Note that the meaning of the name is probably a field in the class, the backend, or both... BUT it is dependent on the implementing wrapper and the backend. @param a class that implements Serializable""" indexes = get_prop(cls, INDEXES_KEY, list) return indexes or []
def _save(self, obj): if obj.getId() is None: obj.updateId() token = tokenizeObject(obj) js = makeSerialized(token) dbobj = json.loads(js) dbobj[MONGO_ID_KEY] = token.getId() dbobj[CLASS_ID_KEY] = token.getClassId() #Set index values - note that we use the original object and #not the token idxList = self._listIndexes(obj.__class__) for indexName in idxList: dbobj[indexName] = get_prop(obj, indexName) self._collection.update({MONGO_ID_KEY: dbobj[MONGO_ID_KEY]}, dbobj, upsert=True)