-
Notifications
You must be signed in to change notification settings - Fork 35
/
test_ipycache.py
216 lines (166 loc) · 6.96 KB
/
test_ipycache.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
"""Tests for ipycache.
"""
import hashlib
import os
import pickle
import sys
import unittest
from ipycache import (save_vars, load_vars, clean_var, clean_vars, do_save,
cache, exec_, conditional_eval)
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY2:
from cStringIO import StringIO
else:
from io import StringIO
def removeFile(path):
"""Remove a file which may or may not exist, without throwing exceptions."""
# In Python 3, we can write:
#
# with contextlib.suppress(FileNotFoundError):
# os.remove(path)
#
# but to be compatible with Python 2, we explicitly check + remove.
if os.path.exists(path):
os.remove(path)
class FunctionTests(unittest.TestCase):
def test_conditional_eval(self):
test_var = 'abc'
self.assertEqual(conditional_eval('$test_var', locals()), 'abc')
x, fun = 10, lambda x: x
test_eval = 'abc_{"10" if x==10 else "not_10"}_{fun(10)}'
expect = 'abc_10_10'
self.assertEqual(conditional_eval(test_eval, locals()), expect)
def test_clean_var(self):
self.assertEqual(clean_var('abc'), 'abc')
self.assertEqual(clean_var('abc '), 'abc')
self.assertEqual(clean_var('abc,'), 'abc')
self.assertEqual(clean_var(',abc'), 'abc')
def test_clean_vars(self):
self.assertEqual(clean_vars(['abc', 'abc,']), ['abc'] * 2)
def test_do_save(self):
path = 'myvars.pkl'
# File exists.
open(path, 'wb').close()
self.assertRaises(ValueError, do_save, path, force=True, read=True)
self.assertTrue(do_save(path, force=True, read=False))
self.assertFalse(do_save(path, force=False, read=False))
self.assertFalse(do_save(path, force=False, read=True))
removeFile(path)
# File does not exist.
self.assertRaises(ValueError, do_save, path, force=True, read=True)
self.assertTrue(do_save(path, force=True, read=False))
self.assertTrue(do_save(path, force=False, read=False))
self.assertFalse(do_save(path, force=False, read=True))
def test_load_fail(self):
path = 'myvars.pkl'
self.assertRaises(IOError, load_vars, path, ['a', 'b'])
def test_save_load(self):
path = 'myvars.pkl'
vars = {'a': 1, 'b': '2'}
save_vars(path, vars)
vars2 = load_vars(path, list(vars.keys()))
self.assertEqual(vars, vars2)
removeFile(path)
class CacheMagicTests(unittest.TestCase):
def test_cache_1(self):
path = 'myvars.pkl'
cell = """a = 1"""
user_ns = {}
def ip_run_cell(cell):
exec_(cell, {}, user_ns)
def ip_push(vars):
user_ns.update(vars)
cache(cell, path, vars=['a'], force=False, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 1)
# We modify the variable in the namespace,
user_ns['a'] = 2
# and execute the cell again. The value should be loaded from the pickle
# file. Note how we did not change cell contents
cache("""a = 1""", path, vars=['a'], force=False, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 1)
# changing the cell will trigger reload
# file. Note how we did not change cell contents
cache("""a = 2""", path, vars=['a'], force=False, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 2)
# store 1 again
user_ns['a'] = 1
cache("""a = 1""", path, vars=['a'], force=False, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
# hack the md5 so code change does not retrigger
with open(path, 'rb') as op:
data = pickle.load(op)
data['_cell_md5'] = hashlib.md5("""a = 2""".encode()).hexdigest()
with open(path, 'wb') as op:
pickle.dump(data, op)
# ensure we don't rerun
user_ns['a'] = 2
# and execute the cell again. The value should be loaded from the pickle
# file. Note how we did not change cell contents
cache("""a = 1""", path, vars=['a'], force=False, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 1)
# Now, we force the cell's execution.
cache("""a = 2""", path, vars=['a'], force=True, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 2)
# Now, we prevent the cell's execution.
user_ns['a'] = 0
cache("""a = 3""", path, vars=['a'], force=False, read=True,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 2)
removeFile(path)
def test_cache_exception(self):
"""Check that, if an exception is raised during the cell's execution,
the pickle file is not written."""
path = 'myvars.pkl'
cell = """a = 1;b = 1/0"""
user_ns = {}
def ip_run_cell(cell):
exec_(cell, {}, user_ns)
def ip_push(vars):
user_ns.update(vars)
cache(cell, path, vars=['a'], force=False, read=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 1)
self.assertFalse(os.path.exists(path))
removeFile(path)
def test_cache_outputs(self):
"""Test the capture of stdout."""
path = 'myvars.pkl'
cell = """a = 1;print(a+1)"""
user_ns = {}
def ip_run_cell(cell):
exec_(cell, {}, user_ns)
def ip_push(vars):
user_ns.update(vars)
cache(cell, path, vars=['a'], verbose=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 1)
# Capture stdout.
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
user_ns = {}
cache(cell, path, vars=['a'], verbose=False,
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
self.assertEqual(user_ns['a'], 1)
sys.stdout = old_stdout
# Check that stdout contains the print statement of the cached cell.
self.assertEqual(mystdout.getvalue(), '2\n')
removeFile(path)
def test_cache_fail_1(self):
"""Fails when saving nonexistent variables."""
path = 'myvars.pkl'
cell = """a = 1"""
user_ns = {}
def ip_run_cell(cell):
exec_(cell, {}, user_ns)
def ip_push(vars):
user_ns.update(vars)
self.assertRaises(ValueError, cache, cell, path, vars=['a', 'b'],
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
removeFile(path)