Exemplo n.º 1
0
 def setUp(self):
     fixture.setup_database_settings()
     self._redis = Redis()
     self._mongo = Mongo("unittest")
     self._memcache = Memcache()
     self._redis.clear()
     self._mongo.clear()
     self._memcache.put("unittest:test", "X")
Exemplo n.º 2
0
 def setUp(self):
     self._redis = Redis()
     self._redis.clear()
Exemplo n.º 3
0
class MongoStoreTest(unittest.TestCase):
    
    def setUp(self):
        fixture.setup_database_settings()
        self._redis = Redis()
        self._mongo = Mongo("unittest")
        self._memcache = Memcache()
        self._redis.clear()
        self._mongo.clear()
        self._memcache.put("unittest:test", "X")

    def test_no_cache(self):
        store = MongoStore("unittest")
        actual = store.save({"_id": "test", "hello": "world"})
        expected = "test"
        self.assertEqual(expected, actual)
        
        actual = store.find_one("test")
        expected = {"_id": "test", "hello": "world"}
        self.assertEqual(expected, actual)
        
        store.save({"_id": "test2", "goodbye": "world"})
        
        actual = list(store.find({}))
        expected = [
            {"_id": "test", "hello": "world"},
            {"_id": "test2", "goodbye": "world"}
        ]
        self.assertEqual(expected, actual)

        actual = list(store.find({"goodbye": {"$exists": True}}))
        expected = [{"_id": "test2", "goodbye": "world"}]
        self.assertEqual(expected, actual)
        
        actual = list(store.find().limit(1))
        expected = [{"_id": "test", "hello": "world"}]
        self.assertEqual(expected, actual)
        
        actual = store.count()
        self.assertEqual(2, actual)
        
        store.delete_one("test")
        actual = list(store.find())
        expected = [{"_id": "test2", "goodbye": "world"}]
        
        store.update({"_id": "test2"}, {"$set":{"goodbye": "universe"}})
        actual = store.find_one("test2")
        expected = {"_id": "test2", "goodbye": "universe"}
        self.assertEqual(expected, actual)
        
        store.update({"_id": "test3"}, {"$set":{"goodbye": "multiverse"}}, upsert=True)
        actual = store.find_one("test3")
        expected = {"_id": "test3", "goodbye": "multiverse"}
        self.assertEqual(expected, actual)
        
    def test_redis_cache(self):
        settings.MongoStores["unittest"]["cachetype"] = CacheType.Persistent
        store = MongoStore("unittest")
        
        store.save({"_id": "test", "hello": "world"})
        actual = self._mongo.find_one("test")
        self.assertEqual({"_id": "test", "hello": "world"}, actual)
        
        actual = self._redis.get("unittest:test")
        expected = serialize({"_id": "test", "hello": "world"})
        self.assertEqual(expected, actual)
        
        self._mongo.delete_one("test")
        actual = self._mongo.find_one("test")
        self.assertEqual(None, actual)
        
        actual = store.find_one("test")
        self.assertEqual({"_id": "test", "hello": "world"}, actual)
        
        store.save({"_id": "test", "hello": "world"})
        store.update({"_id": "test"}, {"$set":{"hello": "universe"}})
        actual = self._redis.get("unittest:test")
        expected = serialize({"_id": "test", "hello": "universe"})
        self.assertEqual(expected, actual)
        
        store.delete_one("test")
        actual = store.find_one("test")
        self.assertEqual(None, actual)
        actual = self._redis.get("unittest:test")
        self.assertEqual(None, actual)
        
    def test_memcache_cache(self):
        settings.MongoStores["unittest"]["cachetype"] = CacheType.Volatile
        store = MongoStore("unittest")
        
        actual = self._memcache.get("unittest:test")
        self.assertEqual("X", actual)
        
        store.save({"_id": "test", "hello": "world"})
        actual = self._mongo.find_one("test")
        self.assertEqual({"_id": "test", "hello": "world"}, actual)
        
        actual = self._memcache.get("unittest:test")
        expected = serialize({"_id": "test", "hello": "world"})
        self.assertEqual(expected, actual)
        
        self._mongo.delete_one("test")
        actual = self._mongo.find_one("test")
        self.assertEqual(None, actual)
        
        actual = store.find_one("test")
        self.assertEqual({"_id": "test", "hello": "world"}, actual)
        
        store.save({"_id": "test", "hello": "world"})
        store.update({"_id": "test"}, {"$set":{"hello": "universe"}})
        actual = self._memcache.get("unittest:test")
        expected = serialize({"_id": "test", "hello": "universe"})
        self.assertEqual(expected, actual)
        
        store.delete_one("test")
        actual = store.find_one("test")
        self.assertEqual(None, actual)
        actual = self._memcache.get("unittest:test")
        self.assertEqual("X", actual)
Exemplo n.º 4
0
class RedisCacheTest(unittest.TestCase):
    
    def setUp(self):
        self._redis = Redis()
        self._redis.clear()

    def test_put_and_get_no_namespace(self):
        cache = RedisCache()
        cache.put("unittestkey1", "val1")
        actual = self._redis.get("unittestkey1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
        actual = cache.get("unittestkey1")
        expected = "val1"
        self.assertEqual(expected, actual)

    def test_put_and_get_with_namespace(self):
        cache = RedisCache(namespace="unittest:")
        cache.put("key1", "val1")
        actual = self._redis.get("unittest:key1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
        actual = self._redis.get("key1")
        expected = None
        self.assertEqual(expected, actual)
        
        actual = cache.get("key1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
    def test_delete(self):
        cache = RedisCache(namespace="unittest:")
        cache.put("key1", "val1")
        actual = cache.get("key1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
        cache.delete("key1")
        actual = cache.get("key1")
        expected = None
        self.assertEqual(expected, actual)

    def test_expire(self):
        cache = RedisCache(namespace="unittest:")
        cache.put("key1", "val1")
        actual = cache.get("key1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
        cache.expire("key1", 2)
        time.sleep(1)
        
        actual = cache.get("key1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
        time.sleep(1.5)
        
        actual = cache.get("key1")
        expected = None
        self.assertEqual(expected, actual)
        
    def test_setting_cachesecs(self):
        cache = RedisCache(namespace="unittest:", cachesecs=2)
        cache.put("key1", "val1")
        time.sleep(1)
        
        actual = cache.get("key1")
        expected = "val1"
        self.assertEqual(expected, actual)
        
        time.sleep(1.5)
        
        actual = cache.get("key1")
        expected = None
        self.assertEqual(expected, actual)
        
    def test_non_string_storage(self):
        cache = RedisCache(namespace="unittest:")
        cache.put("key1", {"hello": "world"})
        actual = cache.get("key1")
        expected = {"hello": "world"}
        self.assertEqual(expected, actual)