-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
89 lines (61 loc) · 1.89 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#coding: utf-8
import random, string
import redis
from redis_import import copy
def random_word(words, length):
return ''.join(random.choice(words) for i in range(length))
def get_words():
word_file = "/usr/share/dict/words"
words = open(word_file).read().splitlines()
return words[:10]
def get_mapping(words, length=1000):
d = {}
for word in words:
d[word] = random.randint(1, length)
return d
def random_set(client, words, length=1000):
d = get_mapping(words, length)
client.mset(d)
def random_hset(client, words, length=1000):
d = get_mapping(words, length)
client.hmset("hashName", d)
def random_lpush(client, words, length=1000):
client.lpush("listName", *words)
def random_zadd(client, words, length=1000):
d = get_mapping(words, length)
client.zadd("myset", **d)
def test():
words = get_words()
rds = redis.Redis()
print "Flush all redis data before insert new."
rds.flushall()
random_set(rds, words)
print "random_set done"
random_hset(rds, words)
print "random_hset done"
random_lpush(rds, words)
print "random_lpush done"
random_zadd(rds, words)
lds = redis.Redis(port=6380)
copy(rds, lds, 0)
# for all keys
keys = rds.scan(0, count=rds.dbsize())
for key in keys:
if rds.type(key) == "string" and not lds.exists(key):
print key
print "String data not consistent"
# for list
l1 = rds.lrange("listName", 0, -1)
l2 = lds.lrange("listName", 0, -1)
assert l1 == l2
#for hash
for key in keys:
if rds.type(key) == "hash" and not lds.hexists("hashName", key):
print "List data not consistent"
# for zset
z1 = rds.zrange("myset", 0, -1, withscores=True)
z2 = lds.zrange("myset", 0, -1, withscores=True)
assert z1 == z2
if __name__ == "__main__":
test()
print "Test passed."