def test_is_successfull(self): dico = {'ok': 1.0, 'n': 2} self.assertTrue(MongoCollection.is_successfull(dico)) dico = {'ok': 666.667, 'n': 1} self.assertFalse(MongoCollection.is_successfull(dico)) dico = {'n': 2} self.assertFalse(MongoCollection.is_successfull(dico))
def test_update(self): res = self.collection.update(query={'_id': self.id_}, document={'strange': 'charm'}) self.assertTrue(MongoCollection.is_successfull(res)) self.assertEqual(res['n'], 0) res = self.collection.update(query={'_id': self.id_}, document={'yin': 'yang'}, upsert=True) self.assertTrue(MongoCollection.is_successfull(res)) self.assertEqual(res['n'], 1) res = self.collection.find_one(self.id_) self.assertEqual(res['yin'], 'yang') self.assertTrue('strange' not in res)
def test_remove(self): res = self.collection.insert(document={ '_id': self.id_, 'top': 'bottom' }) self.assertIsNotNone(res) res = self.collection.remove(query={'_id': self.id_}) self.assertTrue(MongoCollection.is_successfull(res)) self.assertEqual(res['n'], 1) # Deleting non-existing object doesn't throw error res = self.collection.remove(query={}) self.assertTrue(MongoCollection.is_successfull(res)) self.assertEqual(res['n'], 0)
class CanopsisVersionManager(object): """ Canopsis version manager abstraction. """ COLLECTION = "configuration" EDITION_FIELD = "edition" STACK_FIELD = "stack" VERSION_FIELD = "version" __DOCUMENT_ID = "canopsis_version" def __init__(self, collection): """ :param collection: `pymongo.collection.Collection` object. """ self.__collection = MongoCollection(collection) def find_canopsis_document(self): """ Find Canopsis version document. :returns: Canopsis version document or None if not found. :raises: (`pymongo.errors.PyMongoError`, ). """ return self.__collection.find_one({'_id': self.__DOCUMENT_ID}) def put_canopsis_document(self, edition, stack, version): """ Put Canopsis version document (upsert). :param version: `str` Canopsis version. :raises: (`canopsis.common.collection.CollectionError`, ). """ document = {} if edition is not None: document[self.EDITION_FIELD] = edition if stack is not None: document[self.STACK_FIELD] = stack if version is not None: document[self.VERSION_FIELD] = version if len(document) > 0: resp = self.__collection.update({'_id': self.__DOCUMENT_ID}, {'$set': document}, upsert=True) return self.__collection.is_successfull(resp) return True