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