def test_clear_expire(self): """删除过期测试""" logging.warn(u'测试 删除过期的缓存') key1 = 'key1' key2 = 'key2' value = 'value1' # 设置过期时间 0.1 秒,过期前取值正常返回 assert cache.put(key1, value, 0.1) # put 函数的过期测试 assert cache.put(key2, value) assert cache.expire(key2, 0.1) # expire 函数的过期测试 # 让它们过期 time.sleep(0.11) cache._clear_expire() # 删除过期缓存 assert len(cache._expire_cache) == 0 assert len(cache._value_cache) == 0
def test_base(self): '''基础函数测试''' logging.warn(u'测试 get put 等基本函数') key = '123*456' value = """ '"'"xxx.!1!1@#$%^&*()_+=-\|][{}?><,.;:'"+-*/.25eww """ # 特殊字符串 assert cache.get(key) == None cache.put(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) == [] assert cache.keys(None) == []
def test_timeout(self): logging.warn(u'测试 过期时间') key1 = 'key1' key2 = 'key2' value = 'value1' # 设置过期时间 0.1 秒,过期前取值正常返回 assert cache.put(key1, value, 0.1) # put 函数的过期测试 assert cache.get(key1) == value assert cache.exists(key1) == True assert cache.put(key2, value) assert cache.expire(key2, 0.1) # expire 函数的过期测试 assert cache.get(key2) == value assert cache.exists(key2) == True # 让它们过期 time.sleep(0.11) assert cache.exists(key1) == False assert cache.exists(key2) == False
def test_chinese(self): """中文检查""" logging.warn(u'测试 中文缓存') key = '123*45测试key。6' value = u""" '"'"哈哈嘿嘿|“‘’” """ # unicode 中文 cache.put(key, value) assert cache.get(key) == value assert cache.pop(key) == value value = """ '"'"哈哈嘿嘿|“‘’” """ # str 中文 cache.put(key, value) assert cache.get(key) == value assert cache.pop(key) == value cache.pop(key)
def test_set_json(self): logging.warn(u'测试 get put 函数存储非字符串情况') key = '""""哈哈' value = { u"aa": u"哈哈", "bb": "嘿嘿", 0: [1, 2, 3], 2: { 'cc': [2.01, long(547)] }, '嘿嘿': set(u'哆来咪') } # 嵌套json cache.put(key, value) assert cache.get(key) == value assert cache.pop(key) == value assert cache.pop(key) is None value = datetime.datetime.now() # datetime 类型的存储 cache.put(key, value) assert cache.get(key) == value assert cache.pop(key) == value assert cache.pop(key) is None value = time.localtime() # time 类型的存储 cache.put(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
def test_keys(self): logging.warn(u'测试 keys 函数及返回长度检查') cache.put('a1', 1) cache.put('b1', long(1555)) cache.put('c1', 1) cache.put('d1', 1, 0.01) time.sleep(0.02) # 让上面的过期 assert cache.get('d1') == None cache.put('a2', 1) cache.put('b2', 1.0245) cache.put('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([long(1555), 1.0245]) # 取多个值,并删除 assert cache.pop('a1', 'b1', 'b2') == []