-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_history.py
147 lines (112 loc) · 3.03 KB
/
test_history.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
import tempfile
from historydict import HistoryDict
import pytest
OBJ_ID_LEN = 36
h = None
def setup():
global h
h = HistoryDict(':memory:')
def test_historydict():
run_id = ''.join(['a'] * OBJ_ID_LEN)
# Simple round-trip: put and past
config1 = {'plot_x': 'long', 'plot_y': 'island'}
h._put(run_id, config1)
result1 = h.past(run_id)
assert result1 == config1
# Put a second entry. Check that past returns most recent.
config2 = {'plot_x': 'new', 'plot_y': 'york'}
h._put(run_id, config2)
result2 = h.past(run_id)
assert result2 == config2
# And.past(..., 1) returns previous.
result1 = h.past(run_id, 1)
assert result1 == config1
def test_clear():
h._put('hi', 'mom')
h.clear()
with pytest.raises(KeyError):
h.past('hi')
def test_trim():
with pytest.raises(NotImplementedError):
h.trim()
def test_neg_numback_fails():
with pytest.raises(ValueError):
h.past('test', -1)
def test_nonexistent_past_fails():
h['cats'] = 123
with pytest.raises(ValueError):
h.past('cats', 1)
h['cats'] = 456
h.past('cats', 1) # should not raise
def test_gs_items():
h[123] = 'aardvark'
assert h[123] == 'aardvark'
def test_opening():
with tempfile.NamedTemporaryFile(delete=False) as fn:
filename = fn.name
h1 = HistoryDict(filename)
h1['aardvark'] = 'ants'
del h1
h2 = HistoryDict(filename)
assert h2['aardvark'] == 'ants'
def test_iter():
h.clear()
keys = set('abcd')
for k in keys:
h[k] = k
for k, v in h.items():
assert k == v
def test_del():
h.clear()
h['a'] = 123
h['a'] = 456
assert 'a' in h
del h['a']
assert 'a' not in h
# Add a value back to check that all old values were cleared.
h['a'] = 789
with pytest.raises(ValueError):
h.past('a', 1)
assert h['a'] == 789
assert h.past('a', 0) == 789
def test_no_key_in_del():
h.clear()
with pytest.raises(KeyError):
del h['aardvark']
def test_len():
h.clear()
keys = set('abcd')
for k in keys:
h[k] = k
assert len(keys) == len(h)
def test_get():
h.clear()
b = h.get('b', 'aardvark')
assert b, 'aardvark'
def test_protected_key():
with pytest.raises(ValueError):
h[HistoryDict.RESERVED_KEY_KEY]
with pytest.raises(ValueError):
h[HistoryDict.RESERVED_KEY_KEY] = 'aardvark'
def test_repr():
h.clear()
kvpairs = (('foo', 'bar'), ('spam', 'spam spam spam'))
dct = {}
for k, v in kvpairs:
dct[k] = v
h[k] = v
assert repr(dict(h)) == repr(h)
assert dict(h) == dct
def test_flush():
with tempfile.NamedTemporaryFile(delete=False) as fn:
# Needed for windows compatibility
g = HistoryDict(fn.name)
g['k'] = {'a': 1}
g['k']['a'] = 2
# Check that mutated value persists.
del g
g = HistoryDict(fn.name)
assert g['k']['a'] == 2
# finally smoke test the API
g.flush()
g._flush('k')