forked from rossant/ipycache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ipycache.py
More file actions
151 lines (117 loc) · 4.41 KB
/
Copy pathtest_ipycache.py
File metadata and controls
151 lines (117 loc) · 4.41 KB
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
# -*- coding: utf-8 -*-
"""Tests for ipycache.
"""
#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------
import os
import sys
from cStringIO import StringIO
from nose.tools import raises, assert_raises
from ipycache import (save_vars, load_vars, clean_var, clean_vars, do_save,
cache, exec_, conditional_eval)
#------------------------------------------------------------------------------
# Functions tests
#------------------------------------------------------------------------------
def test_conditional_eval():
test_var = 'abc'
assert conditional_eval('$test_var', locals()) == 'abc'
def test_clean_var():
assert clean_var('abc') == 'abc'
assert clean_var('abc ') == 'abc'
assert clean_var('abc,') == 'abc'
assert clean_var(',abc') == 'abc'
def test_clean_vars():
assert clean_vars(['abc', 'abc,']) == ['abc'] * 2
def test_do_save():
path = 'myvars.pkl'
# File exists.
open(path, 'wb').close()
assert_raises(ValueError, do_save, path, force=True, read=True)
assert do_save(path, force=True, read=False)
assert not do_save(path, force=False, read=False)
assert not do_save(path, force=False, read=True)
os.remove(path)
# File does not exist.
assert_raises(ValueError, do_save, path, force=True, read=True)
assert do_save(path, force=True, read=False)
assert do_save(path, force=False, read=False)
assert not do_save(path, force=False, read=True)
@raises(IOError)
def test_load_fail():
path = 'myvars.pkl'
load_vars(path, ['a', 'b'])
def test_save_load():
path = 'myvars.pkl'
vars = {'a': 1, 'b': '2'}
save_vars(path, vars)
vars2 = load_vars(path, list(vars.keys()))
assert vars == vars2
os.remove(path)
#------------------------------------------------------------------------------
# Cache magic tests
#------------------------------------------------------------------------------
def test_cache_1():
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)
assert 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.
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)
assert 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)
assert 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)
assert user_ns['a'] == 2
os.remove(path)
def test_cache_outputs():
"""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)
assert 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)
assert user_ns['a'] == 1
sys.stdout = old_stdout
# Check that stdout contains the print statement of the cached cell.
assert mystdout.getvalue() == '2\n'
os.remove(path)
@raises(ValueError)
def test_cache_fail_1():
"""Fails when saving inexistent 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)
cache(cell, path, vars=['a', 'b'],
ip_user_ns=user_ns, ip_run_cell=ip_run_cell, ip_push=ip_push)
os.remove(path)