def test_set_fail_will_cause_async(self): self.mq.reset_mock() def async_log(key): self.mq.send(key) mc = mc_from_config(self.config, async_cleaner = async_log) mc.set('test_set_key', 'test_value') self.mq.send.assert_called_with('test_set_key')
class LocalCachedTest(PureMCTest): config = { 'servers': ['127.0.0.1:11211'], } mc = LocalCached(mc_from_config(config)) def test_wrapper_is_right(self): self.assertTrue(isinstance(self.mc, LocalCached))
def setUp(self): unittest.TestCase.setUp(self) config = { 'servers': ['127.0.0.1:11211'], } self.mc = mc_from_config(config) self.cache = VersionedLocalCached(self.mc)
class AdjustMCTest(PureMCTest): config = { 'servers': ['127.0.0.1:11211'], 'new_servers': ['127.0.0.1:11212'], } mc = mc_from_config(config) def test_wrapper_is_right(self): self.assertTrue(isinstance(self.mc.mc, AdjustMC))
def test_set_fail_will_cause_async(self): self.mq.reset_mock() def async_log(key): self.mq.send(key) mc = mc_from_config(self.config, async_cleaner=async_log) mc.set('test_set_key', 'test_value') self.mq.send.assert_called_with('test_set_key')
def test_set_multi_fail_will_cause_async(self): self.mq.reset_mock() async_log = lambda key: self.mq.send(key) mc = mc_from_config(self.config, async_cleaner=async_log) mc.set_multi({'key1': 'value1', 'key2': 'value2'}) assert self.mq.send.call_count == 2 calls = self.mq.send.call_args_list for call in calls: assert call == call('key1') or call == call('key2')
def test_set_multi_fail_will_cause_async(self): self.mq.reset_mock() async_log = lambda key: self.mq.send(key) mc = mc_from_config(self.config, async_cleaner = async_log) mc.set_multi({'key1':'value1', 'key2':'value2'}) assert self.mq.send.call_count == 2 calls = self.mq.send.call_args_list for call in calls: assert call == call('key1') or call == call('key2')
def test_delete_multi_fail_will_cause_async(self): self.mq.reset_mock() async_log = lambda key: self.mq.send(key) mc = mc_from_config(self.config, async_cleaner = async_log) mc.delete_multi(['key1', 'key2', 'key3']) assert self.mq.send.call_count == 3 calls = self.mq.send.call_args_list for call in calls: assert call == call('key1') or call == call('key2') or \ call == call('key3')
class ReplicatedTest(PureMCTest): config = { 'servers': ['127.0.0.1:11211'], 'new_servers': [], 'backup_servers': ['127.0.0.1:11213'], } mc = mc_from_config(config) def test_wrapper_is_right(self): self.assertTrue(isinstance(self.mc.mc, Replicated))
def test_delete_multi_fail_will_cause_async(self): self.mq.reset_mock() async_log = lambda key: self.mq.send(key) mc = mc_from_config(self.config, async_cleaner=async_log) mc.delete_multi(['key1', 'key2', 'key3']) assert self.mq.send.call_count == 3 calls = self.mq.send.call_args_list for call in calls: assert call == call('key1') or call == call('key2') or \ call == call('key3')
MEMCACHED = { 'servers' : [], 'disabled' : False, } # from corelib.config import MEMCACHED import sys import os sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "stub")) from douban.mc import mc_from_config from douban.mc.wrapper import LocalCached mc = LocalCached(mc_from_config(MEMCACHED)) from douban.sqlstore import store_from_config from ORZ.exports import orz_decorate, OrzField, orz_get_multi, start_transaction, OrzForceRollBack DATABASE = { 'farms': { "luz_farm": { "master": "localhost:test_vagrant9010:eye:sauron", "tables": ["*"], }, }, 'options': { 'show_warnings': True, } } from unittest import TestCase
MEMCACHED = { 'servers': [], 'disabled': False, } # from corelib.config import MEMCACHED import sys import os sys.path.insert( 0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "stub")) from douban.mc import mc_from_config from douban.mc.wrapper import LocalCached mc = LocalCached(mc_from_config(MEMCACHED)) from douban.sqlstore import store_from_config from ORZ.exports import orz_decorate, OrzField, orz_get_multi, start_transaction, OrzForceRollBack DATABASE = { 'farms': { "luz_farm": { "master": "localhost:test_vagrant9010:eye:sauron", "tables": ["*"], }, }, 'options': { 'show_warnings': True, } } from unittest import TestCase
def test_get_fail_will_never_use_async(self): self.mq.reset_mock() async_log = lambda key: self.mq.send(key) mc = mc_from_config(self.config, async_cleaner=async_log) mc.get('test_key') assert not self.mq.send.called
def test_get_fail_will_never_use_async(self): self.mq.reset_mock() async_log = lambda key: self.mq.send(key) mc = mc_from_config(self.config, async_cleaner = async_log) mc.get('test_key') assert not self.mq.send.called
def get_mc(): return mc_from_config(MEMCACHED, use_cache=False)
class PureMCTest(unittest.TestCase): config = { 'servers': ['127.0.0.1:11211'], } mc = mc_from_config(config) def get_random_key(self): return 'MCTEST:%s:%s' % (time.time(), random.random()) def test_wrapper_is_right(self): self.assertTrue(isinstance(self.mc.mc, cmemcached.Client)) def test_incr_decr(self): key = self.get_random_key() self.assertTrue(self.mc.set(key, 10)) self.mc.incr(key) self.assertEqual(11, self.mc.get(key)) self.mc.decr(key, 10) self.assertEqual(1, self.mc.get(key)) self.mc.delete(key) def test_add_replace(self): key = self.get_random_key() val1 = str(random.random()) val2 = str(random.random()) self.assertTrue(self.mc.get(key) is None) self.assertTrue(self.mc.add(key, val1)) self.assertEquals(val1, self.mc.get(key)) self.assertFalse(self.mc.add(key, val2)) self.assertEqual(val1, self.mc.get(key)) self.mc.delete(key) self.assertTrue(self.mc.get(key) is None) self.assertFalse(self.mc.replace(key, val1)) self.assertTrue(self.mc.get(key) is None) self.mc.set(key, val1) self.assertEqual(val1, self.mc.get(key)) self.assertTrue(self.mc.replace(key, val2)) self.assertEqual(val2, self.mc.get(key)) self.mc.delete(key) def test_set_delete_append_prepend(self): key = self.get_random_key() val = str(random.random()) self.mc.delete(key) self.assertTrue(self.mc.get(key) is None) self.mc.set(key, val) self.assertEqual(val, self.mc.get(key)) self.mc.append(key, 'a') self.assertEqual(val + 'a', self.mc.get(key)) self.mc.prepend(key, 'a') self.assertEqual('a' + val + 'a', self.mc.get(key)) self.mc.delete(key) self.assertTrue(self.mc.get(key) is None) def test_multi_set_delete_append_prepend(self): key1, key2 = self.get_random_key(), self.get_random_key() val1, val2 = str(random.random()), str(random.random()) self.mc.delete_multi([key1, key2]) self.assertEqual({}, self.mc.get_multi([key1, key2])) data = {key1: val1, key2: val2} self.mc.set_multi(data) self.assertEqual(data, self.mc.get_multi([key1, key2])) self.mc.append_multi([key1, key2], 'a') self.assertEqual({ key1: val1 + 'a', key2: val2 + 'a' }, self.mc.get_multi([key1, key2])) self.mc.prepend_multi([key1, key2], 'a') self.assertEqual({ key1: 'a' + val1 + 'a', key2: 'a' + val2 + 'a' }, self.mc.get_multi([key1, key2])) self.mc.delete_multi([key1, key2]) self.assertEqual({}, self.mc.get_multi([key1, key2]))
# -*- coding: utf-8 -*- from flask import Flask from config import MEMCACHED, DOUBANDB from douban.mc import mc_from_config from douban.beansdb import beansdb_from_config mc = mc_from_config(MEMCACHED) db = beansdb_from_config(DOUBANDB, mc=mc) app = Flask(__name__) app.debug = True @app.route('/') def hello(): return "hello world" if __name__ == "__main__": app.run(host="0.0.0.0")