-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_commands.py
148 lines (114 loc) · 4.23 KB
/
test_commands.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from redis.exceptions import ResponseError
import pytest
import json
def genmap():
"""return a n empty hashmap"""
initial = "begin"
current = "begin"
mapstates = {
"a": ["this", "maps", "states"],
"b": ["this", "too", "maps", "somewhere"],
"begin": ["too", "maps"],
"too": ["b"],
}
return {"initial": initial, "map": mapstates, "current": current, "reason": ""}
def test_invalid_set_get(r):
r.flushdb()
r.set("foo", "bar")
with pytest.raises(ResponseError):
r.execute_command("SM.GET", "foo")
def test_set_get(r):
r.flushdb()
initial = "begin"
current = "somewhere"
mapstates = {
"a": ["this", "maps", "states"],
"b": ["this", "too", "maps", "somewhere"],
}
invalid_mapstates = {"a": "badmap", "b": ["this", "is", "a", "good", "map"]}
invalid_no_current = {"initial": initial, "map": mapstates}
with pytest.raises(ResponseError):
r.execute_command("SM.SET", "foo", json.dumps(invalid_mapstates))
r.execute_command(
"SM.SET", "foo", json.dumps({"initial": initial, "map": invalid_mapstates})
)
r.execute_command(
"SM.SET", "foo", json.dumps({"current": current, "map": invalid_mapstates})
)
r.execute_command(
"SM.SET", "foocurrent", json.dumps(invalid_no_current), current
)
sm = genmap()
assert r.execute_command("SM.SET", "bar", json.dumps(sm))
bar = json.loads(r.execute_command("SM.GET", "bar"))
assert bar == sm
def test_get_current_state(r):
r.flushdb()
assert r.execute_command("SM.SET", "fooforcurrent", json.dumps(genmap()))
assert r.execute_command("SM.STATE", "fooforcurrent") == [genmap()["current"]]
def test_get_states(r):
r.flushdb()
sm = genmap()
assert r.execute_command("SM.SET", "foostates", json.dumps(sm))
states = r.execute_command("SM.STATE", "foostates", "list")
mapkeys = list(sm["map"].keys())
mapkeys.sort()
states.sort()
assert mapkeys == states
def test_get_reason(r):
r.flushdb()
sm = genmap()
sm["reason"] = "I am the reason just because"
assert r.execute_command("SM.SET", "foostates", json.dumps(sm))
assert r.execute_command("SM.GET", "foostates", "reason") == sm["reason"]
assert r.execute_command("SM.GET", "foostates", "REASON") == sm["reason"]
def test_set_del(r):
r.flushdb()
assert r.execute_command("SM.SET", "foostates", json.dumps(genmap()))
assert r.delete("foostates")
keys = r.keys()
assert "foostates" not in keys
def test_reset(r):
r.flushdb()
sm = genmap()
assert r.execute_command("SM.SET", "foostates", json.dumps(sm))
r.execute_command("SM.RESET", "foostates")
assert r.execute_command("SM.STATE", "foostates") == [sm["initial"]]
def test_create(r):
r.flushdb()
key = "foo"
assert r.execute_command("SM.CREATE", key)
assert r.execute_command("SM.STATE", key) == [""]
res = r.execute_command("SM.GET", key)
val = json.loads(res)
assert val["initial"] == ""
assert val["map"] == {}
assert val["current"] == ""
assert val["reason"] == ""
def test_template(r):
res = r.execute_command("SM.TEMPLATE")
val = json.loads(res)
assert val["initial"] == ""
assert val["map"] == {}
assert val["current"] == ""
assert val["reason"] == ""
def test_mutate(r):
r.flushdb()
sm = genmap()
assert r.execute_command("SM.SET", "bar", json.dumps(sm))
# bad state
with pytest.raises(ResponseError):
assert r.execute_command("SM.MUTATE", "bar", "smurfy")
# good state
assert r.execute_command("SM.MUTATE", "bar", "too")
assert r.execute_command("SM.MUTATE", "bar", "b", "with a reason!")
assert r.execute_command("SM.GET", "bar", "reason") == "with a reason!"
r.flushdb()
assert r.execute_command("SM.SET", "bar", json.dumps(sm))
# bad forced state
with pytest.raises(ResponseError):
assert r.execute_command("SM.MUTATE", "bar", "smurfy", "FORCE")
assert r.execute_command("SM.MUTATE", "bar", "smurfy", "force")
# now the force
assert r.execute_command("SM.MUTATE", "bar", "b", "this reason!", "force")
assert r.execute_command("SM.GET", "bar", "reason") == "this reason!"