forked from onigame/czar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateserver_test.py
executable file
·134 lines (101 loc) · 3.68 KB
/
stateserver_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
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
#!/usr/bin/python
"""
Test suite for stateserver.py.
Start with an empty state:
>>> try: os.remove("stateserver_test.state")
... except OSError: pass
>>> channel = stateserver.Channel("stateserver_test.state")
>>> channel.get(0, 0)
(0, {})
Add two items, which should bump the version to 2.
>>> channel.set({1: "one", 2: "two"})
New file: stateserver_test.state
>>> pprint.pprint([channel.get(n, 0) for n in range(3)])
[(2, {1: 'one', 2: 'two'}), (2, {2: 'two'}), (2, {})]
Add two more items, one redundant; only bumps the version by +1.
>>> channel.set({2: "two", 3: "three"})
>>> pprint.pprint([channel.get(n, 0) for n in range(4)])
[(3, {1: 'one', 2: 'two', 3: 'three'}),
(3, {2: 'two', 3: 'three'}),
(3, {3: 'three'}),
(3, {})]
Modify the first item and the last item; version is bumped by +2.
Verify that non-ASCII characters survive.
>>> channel.set({1: "one+", 3: u"\u4e09"})
>>> pprint.pprint([channel.get(n, 0) for n in range(6)])
[(5, {1: 'one+', 2: 'two', 3: u'\u4e09'}),
(5, {1: 'one+', 2: 'two', 3: u'\u4e09'}),
(5, {1: 'one+', 3: u'\u4e09'}),
(5, {1: 'one+', 3: u'\u4e09'}),
(5, {3: u'\u4e09'}),
(5, {})]
Delete something, make sure that works properly.
Empty values (like "" or 0) should not delete.
>>> channel.set({0: "", 1: 0, 2: None, 4: None})
>>> pprint.pprint([channel.get(n, 0) for n in range(9)])
[(8, {0: '', 1: 0, 3: u'\u4e09'}),
(8, {0: '', 1: 0, 3: u'\u4e09'}),
(8, {0: '', 1: 0, 2: None, 3: u'\u4e09'}),
(8, {0: '', 1: 0, 2: None, 3: u'\u4e09'}),
(8, {0: '', 1: 0, 2: None, 3: u'\u4e09'}),
(8, {0: '', 1: 0, 2: None}),
(8, {1: 0, 2: None}),
(8, {2: None}),
(8, {})]
Reload the channel from the data file, make sure it's intact.
(Note, the JSON reload makes all strings unicode.)
>>> channel = None
>>> channel = stateserver.Channel("stateserver_test.state")
>>> pprint.pprint([channel.get(n, 0) for n in range(9)])
Read 4 keys: stateserver_test.state
[(8, {0: u'', 1: 0, 3: u'\u4e09'}),
(8, {0: u'', 1: 0, 3: u'\u4e09'}),
(8, {0: u'', 1: 0, 2: None, 3: u'\u4e09'}),
(8, {0: u'', 1: 0, 2: None, 3: u'\u4e09'}),
(8, {0: u'', 1: 0, 2: None, 3: u'\u4e09'}),
(8, {0: u'', 1: 0, 2: None}),
(8, {1: 0, 2: None}),
(8, {2: None}),
(8, {})]
Make sure rewrite() works properly, reducing the file size.
>>> before_rewrite = os.path.getsize("stateserver_test.state")
>>> channel.rewrite()
Rewrote 4 keys: stateserver_test.state
>>> after_rewrite = os.path.getsize("stateserver_test.state")
>>> after_rewrite < before_rewrite
True
>>> channel.set({4: "four"})
>>> after_rewrite < os.path.getsize("stateserver_test.state")
True
>>> pprint.pprint([channel.get(n, 0) for n in range(10)])
[(9, {0: u'', 1: 0, 3: u'\u4e09', 4: 'four'}),
(9, {0: u'', 1: 0, 3: u'\u4e09', 4: 'four'}),
(9, {0: u'', 1: 0, 2: None, 3: u'\u4e09', 4: 'four'}),
(9, {0: u'', 1: 0, 2: None, 3: u'\u4e09', 4: 'four'}),
(9, {0: u'', 1: 0, 2: None, 3: u'\u4e09', 4: 'four'}),
(9, {0: u'', 1: 0, 2: None, 4: 'four'}),
(9, {1: 0, 2: None, 4: 'four'}),
(9, {2: None, 4: 'four'}),
(9, {4: 'four'}),
(9, {})]
Reload the rewritten state file to make sure it's all good.
>>> channel = None
>>> channel = stateserver.Channel("stateserver_test.state")
>>> pprint.pprint([channel.get(n, 0) for n in range(10)])
Read 5 keys: stateserver_test.state
[(9, {0: u'', 1: 0, 3: u'\u4e09', 4: u'four'}),
(9, {0: u'', 1: 0, 3: u'\u4e09', 4: u'four'}),
(9, {0: u'', 1: 0, 2: None, 3: u'\u4e09', 4: u'four'}),
(9, {0: u'', 1: 0, 2: None, 3: u'\u4e09', 4: u'four'}),
(9, {0: u'', 1: 0, 2: None, 3: u'\u4e09', 4: u'four'}),
(9, {0: u'', 1: 0, 2: None, 4: u'four'}),
(9, {1: 0, 2: None, 4: u'four'}),
(9, {2: None, 4: u'four'}),
(9, {4: u'four'}),
(9, {})]
"""
import doctest
import os
import pprint
import stateserver
doctest.testmod()