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