def test_base(self): """基础函数测试""" logging.info(u'测试 get put 等基本函数') key = '123*456' value = """ '"'"xxx.!1!1@#$%^&*()_+=-\|][{}?><,.;:'"+-*/.25eww """ # 特殊字符串 assert cache.get(key) == None cache.set(key, value) assert cache.get(key) == value assert cache.exists(key) == True assert cache.pop(key) == value assert cache.exists(key) == False assert cache.get(key) == None assert cache.get(key, 0) == 0 # 默认值检查 # 特殊参数检查 assert cache.get(None) == None assert cache.exists(None) == False assert cache.pop(None) == None assert cache.pop(None, None) == [] if getattr(cache, 'keys', None): assert cache.keys(None) == []
def test_keys(self): if not getattr(cache, 'keys', None): logging.info(u'缓存里面没有 keys 函数,无法测试') return logging.info(u'测试 keys 函数及返回长度检查') cache.set('a1', 1) cache.set('b1', 1555L) cache.set('c1', 1) cache.set('d1', 2, -1) # 已过期 assert cache.get('d1') == None cache.set('a2', 1) cache.set('b2', 1.0245) cache.set('c2', 1) assert set(cache.keys('a*')) == set(['a1', 'a2']) assert set(cache.keys('*1')) == set(['a1', 'b1', 'c1']) assert len(cache.keys('*')) == 6 assert cache.pop('a1', 'a2') == [1,1] # 取多个值,并删除 assert cache.pop('a1') == None assert set(cache.pop('a1', 'b1', 'b2')) == set([1555L, 1.0245]) # 取多个值,并删除 assert cache.pop('a1', 'b1', 'b2') == []
def test_set_json(self): logging.info(u'测试 get put 函数存储非字符串情况') key = u'""""哈哈' value = { u"aa": u"哈哈", "bb": "嘿嘿", 0: [1, 2, 3], 2: { 'cc': [2.01, 547L] }, '嘿嘿': set(u'哆来咪') } # 嵌套json cache.set(key, value) assert cache.get(key) == value assert cache.pop(key) == value assert cache.pop(key) is None value = datetime.datetime.now() # datetime 类型的存储 cache.set(key, value) assert cache.get(key) == value assert cache.pop(key) == value assert cache.pop(key) is None value = time.localtime() # time 类型的存储 cache.set(key, value) assert time.mktime(cache.get(key)) == time.mktime( value) # 避免夏令时引起的判断不同 assert time.mktime(cache.pop(key)) == time.mktime( value) # 避免夏令时引起的判断不同 assert cache.pop(key) is None