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