Show More
@@ -1,306 +1,320 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Tests for the notebook manager.""" |
|
3 | 3 | from __future__ import print_function |
|
4 | 4 | |
|
5 | 5 | import logging |
|
6 | 6 | import os |
|
7 | 7 | |
|
8 | 8 | from tornado.web import HTTPError |
|
9 | 9 | from unittest import TestCase |
|
10 | 10 | from tempfile import NamedTemporaryFile |
|
11 | 11 | |
|
12 | 12 | from IPython.nbformat import current |
|
13 | 13 | |
|
14 | 14 | from IPython.utils.tempdir import TemporaryDirectory |
|
15 | 15 | from IPython.utils.traitlets import TraitError |
|
16 | 16 | from IPython.html.utils import url_path_join |
|
17 | 17 | |
|
18 | 18 | from ..filenbmanager import FileNotebookManager |
|
19 | 19 | from ..nbmanager import NotebookManager |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | class TestFileNotebookManager(TestCase): |
|
23 | 23 | |
|
24 | 24 | def test_nb_dir(self): |
|
25 | 25 | with TemporaryDirectory() as td: |
|
26 | 26 | fm = FileNotebookManager(notebook_dir=td) |
|
27 | 27 | self.assertEqual(fm.notebook_dir, td) |
|
28 | 28 | |
|
29 | 29 | def test_missing_nb_dir(self): |
|
30 | 30 | with TemporaryDirectory() as td: |
|
31 | 31 | nbdir = os.path.join(td, 'notebook', 'dir', 'is', 'missing') |
|
32 | 32 | self.assertRaises(TraitError, FileNotebookManager, notebook_dir=nbdir) |
|
33 | 33 | |
|
34 | 34 | def test_invalid_nb_dir(self): |
|
35 | 35 | with NamedTemporaryFile() as tf: |
|
36 | 36 | self.assertRaises(TraitError, FileNotebookManager, notebook_dir=tf.name) |
|
37 | 37 | |
|
38 | 38 | def test_get_os_path(self): |
|
39 | 39 | # full filesystem path should be returned with correct operating system |
|
40 | 40 | # separators. |
|
41 | 41 | with TemporaryDirectory() as td: |
|
42 | 42 | nbdir = td |
|
43 | 43 | fm = FileNotebookManager(notebook_dir=nbdir) |
|
44 | 44 | path = fm._get_os_path('test.ipynb', '/path/to/notebook/') |
|
45 | 45 | rel_path_list = '/path/to/notebook/test.ipynb'.split('/') |
|
46 | 46 | fs_path = os.path.join(fm.notebook_dir, *rel_path_list) |
|
47 | 47 | self.assertEqual(path, fs_path) |
|
48 | 48 | |
|
49 | 49 | fm = FileNotebookManager(notebook_dir=nbdir) |
|
50 | 50 | path = fm._get_os_path('test.ipynb') |
|
51 | 51 | fs_path = os.path.join(fm.notebook_dir, 'test.ipynb') |
|
52 | 52 | self.assertEqual(path, fs_path) |
|
53 | 53 | |
|
54 | 54 | fm = FileNotebookManager(notebook_dir=nbdir) |
|
55 | 55 | path = fm._get_os_path('test.ipynb', '////') |
|
56 | 56 | fs_path = os.path.join(fm.notebook_dir, 'test.ipynb') |
|
57 | 57 | self.assertEqual(path, fs_path) |
|
58 | ||
|
59 | def test_checkpoint_subdir(self): | |
|
60 | subd = u'sub βir' | |
|
61 | cp_name = 'test-cp.ipynb' | |
|
62 | with TemporaryDirectory() as td: | |
|
63 | nbdir = td | |
|
64 | os.mkdir(os.path.join(td, subd)) | |
|
65 | fm = FileNotebookManager(notebook_dir=nbdir) | |
|
66 | cp_dir = fm.get_checkpoint_path('cp', 'test.ipynb', '/') | |
|
67 | cp_subdir = fm.get_checkpoint_path('cp', 'test.ipynb', '/%s/' % subd) | |
|
68 | self.assertNotEqual(cp_dir, cp_subdir) | |
|
69 | self.assertEqual(cp_dir, os.path.join(nbdir, fm.checkpoint_dir, cp_name)) | |
|
70 | self.assertEqual(cp_subdir, os.path.join(nbdir, subd, fm.checkpoint_dir, cp_name)) | |
|
71 | ||
|
58 | 72 | |
|
59 | 73 | class TestNotebookManager(TestCase): |
|
60 | 74 | |
|
61 | 75 | def setUp(self): |
|
62 | 76 | self._temp_dir = TemporaryDirectory() |
|
63 | 77 | self.td = self._temp_dir.name |
|
64 | 78 | self.notebook_manager = FileNotebookManager( |
|
65 | 79 | notebook_dir=self.td, |
|
66 | 80 | log=logging.getLogger() |
|
67 | 81 | ) |
|
68 | 82 | |
|
69 | 83 | def tearDown(self): |
|
70 | 84 | self._temp_dir.cleanup() |
|
71 | 85 | |
|
72 | 86 | def make_dir(self, abs_path, rel_path): |
|
73 | 87 | """make subdirectory, rel_path is the relative path |
|
74 | 88 | to that directory from the location where the server started""" |
|
75 | 89 | os_path = os.path.join(abs_path, rel_path) |
|
76 | 90 | try: |
|
77 | 91 | os.makedirs(os_path) |
|
78 | 92 | except OSError: |
|
79 | 93 | print("Directory already exists: %r" % os_path) |
|
80 | 94 | |
|
81 | 95 | def add_code_cell(self, nb): |
|
82 | 96 | output = current.new_output("display_data", output_javascript="alert('hi');") |
|
83 | 97 | cell = current.new_code_cell("print('hi')", outputs=[output]) |
|
84 | 98 | if not nb.worksheets: |
|
85 | 99 | nb.worksheets.append(current.new_worksheet()) |
|
86 | 100 | nb.worksheets[0].cells.append(cell) |
|
87 | 101 | |
|
88 | 102 | def new_notebook(self): |
|
89 | 103 | nbm = self.notebook_manager |
|
90 | 104 | model = nbm.create_notebook() |
|
91 | 105 | name = model['name'] |
|
92 | 106 | path = model['path'] |
|
93 | 107 | |
|
94 | 108 | full_model = nbm.get_notebook(name, path) |
|
95 | 109 | nb = full_model['content'] |
|
96 | 110 | self.add_code_cell(nb) |
|
97 | 111 | |
|
98 | 112 | nbm.save_notebook(full_model, name, path) |
|
99 | 113 | return nb, name, path |
|
100 | 114 | |
|
101 | 115 | def test_create_notebook(self): |
|
102 | 116 | nm = self.notebook_manager |
|
103 | 117 | # Test in root directory |
|
104 | 118 | model = nm.create_notebook() |
|
105 | 119 | assert isinstance(model, dict) |
|
106 | 120 | self.assertIn('name', model) |
|
107 | 121 | self.assertIn('path', model) |
|
108 | 122 | self.assertEqual(model['name'], 'Untitled0.ipynb') |
|
109 | 123 | self.assertEqual(model['path'], '') |
|
110 | 124 | |
|
111 | 125 | # Test in sub-directory |
|
112 | 126 | sub_dir = '/foo/' |
|
113 | 127 | self.make_dir(nm.notebook_dir, 'foo') |
|
114 | 128 | model = nm.create_notebook(None, sub_dir) |
|
115 | 129 | assert isinstance(model, dict) |
|
116 | 130 | self.assertIn('name', model) |
|
117 | 131 | self.assertIn('path', model) |
|
118 | 132 | self.assertEqual(model['name'], 'Untitled0.ipynb') |
|
119 | 133 | self.assertEqual(model['path'], sub_dir.strip('/')) |
|
120 | 134 | |
|
121 | 135 | def test_get_notebook(self): |
|
122 | 136 | nm = self.notebook_manager |
|
123 | 137 | # Create a notebook |
|
124 | 138 | model = nm.create_notebook() |
|
125 | 139 | name = model['name'] |
|
126 | 140 | path = model['path'] |
|
127 | 141 | |
|
128 | 142 | # Check that we 'get' on the notebook we just created |
|
129 | 143 | model2 = nm.get_notebook(name, path) |
|
130 | 144 | assert isinstance(model2, dict) |
|
131 | 145 | self.assertIn('name', model2) |
|
132 | 146 | self.assertIn('path', model2) |
|
133 | 147 | self.assertEqual(model['name'], name) |
|
134 | 148 | self.assertEqual(model['path'], path) |
|
135 | 149 | |
|
136 | 150 | # Test in sub-directory |
|
137 | 151 | sub_dir = '/foo/' |
|
138 | 152 | self.make_dir(nm.notebook_dir, 'foo') |
|
139 | 153 | model = nm.create_notebook(None, sub_dir) |
|
140 | 154 | model2 = nm.get_notebook(name, sub_dir) |
|
141 | 155 | assert isinstance(model2, dict) |
|
142 | 156 | self.assertIn('name', model2) |
|
143 | 157 | self.assertIn('path', model2) |
|
144 | 158 | self.assertIn('content', model2) |
|
145 | 159 | self.assertEqual(model2['name'], 'Untitled0.ipynb') |
|
146 | 160 | self.assertEqual(model2['path'], sub_dir.strip('/')) |
|
147 | 161 | |
|
148 | 162 | def test_update_notebook(self): |
|
149 | 163 | nm = self.notebook_manager |
|
150 | 164 | # Create a notebook |
|
151 | 165 | model = nm.create_notebook() |
|
152 | 166 | name = model['name'] |
|
153 | 167 | path = model['path'] |
|
154 | 168 | |
|
155 | 169 | # Change the name in the model for rename |
|
156 | 170 | model['name'] = 'test.ipynb' |
|
157 | 171 | model = nm.update_notebook(model, name, path) |
|
158 | 172 | assert isinstance(model, dict) |
|
159 | 173 | self.assertIn('name', model) |
|
160 | 174 | self.assertIn('path', model) |
|
161 | 175 | self.assertEqual(model['name'], 'test.ipynb') |
|
162 | 176 | |
|
163 | 177 | # Make sure the old name is gone |
|
164 | 178 | self.assertRaises(HTTPError, nm.get_notebook, name, path) |
|
165 | 179 | |
|
166 | 180 | # Test in sub-directory |
|
167 | 181 | # Create a directory and notebook in that directory |
|
168 | 182 | sub_dir = '/foo/' |
|
169 | 183 | self.make_dir(nm.notebook_dir, 'foo') |
|
170 | 184 | model = nm.create_notebook(None, sub_dir) |
|
171 | 185 | name = model['name'] |
|
172 | 186 | path = model['path'] |
|
173 | 187 | |
|
174 | 188 | # Change the name in the model for rename |
|
175 | 189 | model['name'] = 'test_in_sub.ipynb' |
|
176 | 190 | model = nm.update_notebook(model, name, path) |
|
177 | 191 | assert isinstance(model, dict) |
|
178 | 192 | self.assertIn('name', model) |
|
179 | 193 | self.assertIn('path', model) |
|
180 | 194 | self.assertEqual(model['name'], 'test_in_sub.ipynb') |
|
181 | 195 | self.assertEqual(model['path'], sub_dir.strip('/')) |
|
182 | 196 | |
|
183 | 197 | # Make sure the old name is gone |
|
184 | 198 | self.assertRaises(HTTPError, nm.get_notebook, name, path) |
|
185 | 199 | |
|
186 | 200 | def test_save_notebook(self): |
|
187 | 201 | nm = self.notebook_manager |
|
188 | 202 | # Create a notebook |
|
189 | 203 | model = nm.create_notebook() |
|
190 | 204 | name = model['name'] |
|
191 | 205 | path = model['path'] |
|
192 | 206 | |
|
193 | 207 | # Get the model with 'content' |
|
194 | 208 | full_model = nm.get_notebook(name, path) |
|
195 | 209 | |
|
196 | 210 | # Save the notebook |
|
197 | 211 | model = nm.save_notebook(full_model, name, path) |
|
198 | 212 | assert isinstance(model, dict) |
|
199 | 213 | self.assertIn('name', model) |
|
200 | 214 | self.assertIn('path', model) |
|
201 | 215 | self.assertEqual(model['name'], name) |
|
202 | 216 | self.assertEqual(model['path'], path) |
|
203 | 217 | |
|
204 | 218 | # Test in sub-directory |
|
205 | 219 | # Create a directory and notebook in that directory |
|
206 | 220 | sub_dir = '/foo/' |
|
207 | 221 | self.make_dir(nm.notebook_dir, 'foo') |
|
208 | 222 | model = nm.create_notebook(None, sub_dir) |
|
209 | 223 | name = model['name'] |
|
210 | 224 | path = model['path'] |
|
211 | 225 | model = nm.get_notebook(name, path) |
|
212 | 226 | |
|
213 | 227 | # Change the name in the model for rename |
|
214 | 228 | model = nm.save_notebook(model, name, path) |
|
215 | 229 | assert isinstance(model, dict) |
|
216 | 230 | self.assertIn('name', model) |
|
217 | 231 | self.assertIn('path', model) |
|
218 | 232 | self.assertEqual(model['name'], 'Untitled0.ipynb') |
|
219 | 233 | self.assertEqual(model['path'], sub_dir.strip('/')) |
|
220 | 234 | |
|
221 | 235 | def test_save_notebook_with_script(self): |
|
222 | 236 | nm = self.notebook_manager |
|
223 | 237 | # Create a notebook |
|
224 | 238 | model = nm.create_notebook() |
|
225 | 239 | nm.save_script = True |
|
226 | 240 | model = nm.create_notebook() |
|
227 | 241 | name = model['name'] |
|
228 | 242 | path = model['path'] |
|
229 | 243 | |
|
230 | 244 | # Get the model with 'content' |
|
231 | 245 | full_model = nm.get_notebook(name, path) |
|
232 | 246 | |
|
233 | 247 | # Save the notebook |
|
234 | 248 | model = nm.save_notebook(full_model, name, path) |
|
235 | 249 | |
|
236 | 250 | # Check that the script was created |
|
237 | 251 | py_path = os.path.join(nm.notebook_dir, os.path.splitext(name)[0]+'.py') |
|
238 | 252 | assert os.path.exists(py_path), py_path |
|
239 | 253 | |
|
240 | 254 | def test_delete_notebook(self): |
|
241 | 255 | nm = self.notebook_manager |
|
242 | 256 | # Create a notebook |
|
243 | 257 | nb, name, path = self.new_notebook() |
|
244 | 258 | |
|
245 | 259 | # Delete the notebook |
|
246 | 260 | nm.delete_notebook(name, path) |
|
247 | 261 | |
|
248 | 262 | # Check that a 'get' on the deleted notebook raises and error |
|
249 | 263 | self.assertRaises(HTTPError, nm.get_notebook, name, path) |
|
250 | 264 | |
|
251 | 265 | def test_copy_notebook(self): |
|
252 | 266 | nm = self.notebook_manager |
|
253 | 267 | path = u'Γ₯ b' |
|
254 | 268 | name = u'nb β.ipynb' |
|
255 | 269 | os.mkdir(os.path.join(nm.notebook_dir, path)) |
|
256 | 270 | orig = nm.create_notebook({'name' : name}, path=path) |
|
257 | 271 | |
|
258 | 272 | # copy with unspecified name |
|
259 | 273 | copy = nm.copy_notebook(name, path=path) |
|
260 | 274 | self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy0.ipynb')) |
|
261 | 275 | |
|
262 | 276 | # copy with specified name |
|
263 | 277 | copy2 = nm.copy_notebook(name, u'copy 2.ipynb', path=path) |
|
264 | 278 | self.assertEqual(copy2['name'], u'copy 2.ipynb') |
|
265 | 279 | |
|
266 | 280 | def test_trust_notebook(self): |
|
267 | 281 | nbm = self.notebook_manager |
|
268 | 282 | nb, name, path = self.new_notebook() |
|
269 | 283 | |
|
270 | 284 | untrusted = nbm.get_notebook(name, path)['content'] |
|
271 | 285 | assert not nbm.notary.check_cells(untrusted) |
|
272 | 286 | |
|
273 | 287 | # print(untrusted) |
|
274 | 288 | nbm.trust_notebook(name, path) |
|
275 | 289 | trusted = nbm.get_notebook(name, path)['content'] |
|
276 | 290 | # print(trusted) |
|
277 | 291 | assert nbm.notary.check_cells(trusted) |
|
278 | 292 | |
|
279 | 293 | def test_mark_trusted_cells(self): |
|
280 | 294 | nbm = self.notebook_manager |
|
281 | 295 | nb, name, path = self.new_notebook() |
|
282 | 296 | |
|
283 | 297 | nbm.mark_trusted_cells(nb, name, path) |
|
284 | 298 | for cell in nb.worksheets[0].cells: |
|
285 | 299 | if cell.cell_type == 'code': |
|
286 | 300 | assert not cell.trusted |
|
287 | 301 | |
|
288 | 302 | nbm.trust_notebook(name, path) |
|
289 | 303 | nb = nbm.get_notebook(name, path)['content'] |
|
290 | 304 | for cell in nb.worksheets[0].cells: |
|
291 | 305 | if cell.cell_type == 'code': |
|
292 | 306 | assert cell.trusted |
|
293 | 307 | |
|
294 | 308 | def test_check_and_sign(self): |
|
295 | 309 | nbm = self.notebook_manager |
|
296 | 310 | nb, name, path = self.new_notebook() |
|
297 | 311 | |
|
298 | 312 | nbm.mark_trusted_cells(nb, name, path) |
|
299 | 313 | nbm.check_and_sign(nb, name, path) |
|
300 | 314 | assert not nbm.notary.check_signature(nb) |
|
301 | 315 | |
|
302 | 316 | nbm.trust_notebook(name, path) |
|
303 | 317 | nb = nbm.get_notebook(name, path)['content'] |
|
304 | 318 | nbm.mark_trusted_cells(nb, name, path) |
|
305 | 319 | nbm.check_and_sign(nb, name, path) |
|
306 | 320 | assert nbm.notary.check_signature(nb) |
General Comments 0
You need to be logged in to leave comments.
Login now