##// END OF EJS Templates
docstring on test_dir
Min RK -
Show More
@@ -1,432 +1,434 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 os
6 6
7 7 from tornado.web import HTTPError
8 8 from unittest import TestCase
9 9 from tempfile import NamedTemporaryFile
10 10
11 11 from IPython.nbformat import v4 as nbformat
12 12
13 13 from IPython.utils.tempdir import TemporaryDirectory
14 14 from IPython.utils.traitlets import TraitError
15 15 from IPython.html.utils import url_path_join
16 16 from IPython.testing import decorators as dec
17 17
18 18 from ..filemanager import FileContentsManager
19 19
20 20
21 21 def _make_dir(contents_manager, api_path):
22 22 """
23 23 Make a directory.
24 24 """
25 25 os_path = contents_manager._get_os_path(api_path)
26 26 try:
27 27 os.makedirs(os_path)
28 28 except OSError:
29 29 print("Directory already exists: %r" % os_path)
30 30
31 31
32 32 class TestFileContentsManager(TestCase):
33 33
34 34 def symlink(self, contents_manager, src, dst):
35 35 """Make a symlink to src from dst
36 36
37 37 src and dst are api_paths
38 38 """
39 39 src_os_path = contents_manager._get_os_path(src)
40 40 dst_os_path = contents_manager._get_os_path(dst)
41 41 print(src_os_path, dst_os_path, os.path.isfile(src_os_path))
42 42 os.symlink(src_os_path, dst_os_path)
43 43
44 44 def test_root_dir(self):
45 45 with TemporaryDirectory() as td:
46 46 fm = FileContentsManager(root_dir=td)
47 47 self.assertEqual(fm.root_dir, td)
48 48
49 49 def test_missing_root_dir(self):
50 50 with TemporaryDirectory() as td:
51 51 root = os.path.join(td, 'notebook', 'dir', 'is', 'missing')
52 52 self.assertRaises(TraitError, FileContentsManager, root_dir=root)
53 53
54 54 def test_invalid_root_dir(self):
55 55 with NamedTemporaryFile() as tf:
56 56 self.assertRaises(TraitError, FileContentsManager, root_dir=tf.name)
57 57
58 58 def test_get_os_path(self):
59 59 # full filesystem path should be returned with correct operating system
60 60 # separators.
61 61 with TemporaryDirectory() as td:
62 62 root = td
63 63 fm = FileContentsManager(root_dir=root)
64 64 path = fm._get_os_path('/path/to/notebook/test.ipynb')
65 65 rel_path_list = '/path/to/notebook/test.ipynb'.split('/')
66 66 fs_path = os.path.join(fm.root_dir, *rel_path_list)
67 67 self.assertEqual(path, fs_path)
68 68
69 69 fm = FileContentsManager(root_dir=root)
70 70 path = fm._get_os_path('test.ipynb')
71 71 fs_path = os.path.join(fm.root_dir, 'test.ipynb')
72 72 self.assertEqual(path, fs_path)
73 73
74 74 fm = FileContentsManager(root_dir=root)
75 75 path = fm._get_os_path('////test.ipynb')
76 76 fs_path = os.path.join(fm.root_dir, 'test.ipynb')
77 77 self.assertEqual(path, fs_path)
78 78
79 79 def test_checkpoint_subdir(self):
80 80 subd = u'sub βˆ‚ir'
81 81 cp_name = 'test-cp.ipynb'
82 82 with TemporaryDirectory() as td:
83 83 root = td
84 84 os.mkdir(os.path.join(td, subd))
85 85 fm = FileContentsManager(root_dir=root)
86 86 cp_dir = fm.get_checkpoint_path('cp', 'test.ipynb')
87 87 cp_subdir = fm.get_checkpoint_path('cp', '/%s/test.ipynb' % subd)
88 88 self.assertNotEqual(cp_dir, cp_subdir)
89 89 self.assertEqual(cp_dir, os.path.join(root, fm.checkpoint_dir, cp_name))
90 90 self.assertEqual(cp_subdir, os.path.join(root, subd, fm.checkpoint_dir, cp_name))
91 91
92 92 @dec.skip_win32
93 93 def test_bad_symlink(self):
94 94 with TemporaryDirectory() as td:
95 95 cm = FileContentsManager(root_dir=td)
96 96 path = 'test bad symlink'
97 97 _make_dir(cm, path)
98 98
99 99 file_model = cm.new_untitled(path=path, ext='.txt')
100 100
101 101 # create a broken symlink
102 102 self.symlink(cm, "target", '%s/%s' % (path, 'bad symlink'))
103 103 model = cm.get(path)
104 104 self.assertEqual(model['content'], [file_model])
105 105
106 106 @dec.skip_win32
107 107 def test_good_symlink(self):
108 108 with TemporaryDirectory() as td:
109 109 cm = FileContentsManager(root_dir=td)
110 110 parent = 'test good symlink'
111 111 name = 'good symlink'
112 112 path = '{0}/{1}'.format(parent, name)
113 113 _make_dir(cm, parent)
114 114
115 115 file_model = cm.new(path=parent + '/zfoo.txt')
116 116
117 117 # create a good symlink
118 118 self.symlink(cm, file_model['path'], path)
119 119 symlink_model = cm.get(path, content=False)
120 120 dir_model = cm.get(parent)
121 121 self.assertEqual(
122 122 sorted(dir_model['content'], key=lambda x: x['name']),
123 123 [symlink_model, file_model],
124 124 )
125 125
126 126
127 127 class TestContentsManager(TestCase):
128 128
129 129 def setUp(self):
130 130 self._temp_dir = TemporaryDirectory()
131 131 self.td = self._temp_dir.name
132 132 self.contents_manager = FileContentsManager(
133 133 root_dir=self.td,
134 134 )
135 135
136 136 def tearDown(self):
137 137 self._temp_dir.cleanup()
138 138
139 139 def make_dir(self, api_path):
140 """make subdirectory, rel_path is the relative path
141 to that directory from the location where the server started"""
140 """make a subdirectory at api_path
141
142 override in subclasses if contents are not on the filesystem.
143 """
142 144 _make_dir(self.contents_manager, api_path)
143 145
144 146 def add_code_cell(self, nb):
145 147 output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"})
146 148 cell = nbformat.new_code_cell("print('hi')", outputs=[output])
147 149 nb.cells.append(cell)
148 150
149 151 def new_notebook(self):
150 152 cm = self.contents_manager
151 153 model = cm.new_untitled(type='notebook')
152 154 name = model['name']
153 155 path = model['path']
154 156
155 157 full_model = cm.get(path)
156 158 nb = full_model['content']
157 159 self.add_code_cell(nb)
158 160
159 161 cm.save(full_model, path)
160 162 return nb, name, path
161 163
162 164 def test_new_untitled(self):
163 165 cm = self.contents_manager
164 166 # Test in root directory
165 167 model = cm.new_untitled(type='notebook')
166 168 assert isinstance(model, dict)
167 169 self.assertIn('name', model)
168 170 self.assertIn('path', model)
169 171 self.assertIn('type', model)
170 172 self.assertEqual(model['type'], 'notebook')
171 173 self.assertEqual(model['name'], 'Untitled.ipynb')
172 174 self.assertEqual(model['path'], 'Untitled.ipynb')
173 175
174 176 # Test in sub-directory
175 177 model = cm.new_untitled(type='directory')
176 178 assert isinstance(model, dict)
177 179 self.assertIn('name', model)
178 180 self.assertIn('path', model)
179 181 self.assertIn('type', model)
180 182 self.assertEqual(model['type'], 'directory')
181 183 self.assertEqual(model['name'], 'Untitled Folder')
182 184 self.assertEqual(model['path'], 'Untitled Folder')
183 185 sub_dir = model['path']
184 186
185 187 model = cm.new_untitled(path=sub_dir)
186 188 assert isinstance(model, dict)
187 189 self.assertIn('name', model)
188 190 self.assertIn('path', model)
189 191 self.assertIn('type', model)
190 192 self.assertEqual(model['type'], 'file')
191 193 self.assertEqual(model['name'], 'untitled')
192 194 self.assertEqual(model['path'], '%s/untitled' % sub_dir)
193 195
194 196 def test_get(self):
195 197 cm = self.contents_manager
196 198 # Create a notebook
197 199 model = cm.new_untitled(type='notebook')
198 200 name = model['name']
199 201 path = model['path']
200 202
201 203 # Check that we 'get' on the notebook we just created
202 204 model2 = cm.get(path)
203 205 assert isinstance(model2, dict)
204 206 self.assertIn('name', model2)
205 207 self.assertIn('path', model2)
206 208 self.assertEqual(model['name'], name)
207 209 self.assertEqual(model['path'], path)
208 210
209 211 nb_as_file = cm.get(path, content=True, type='file')
210 212 self.assertEqual(nb_as_file['path'], path)
211 213 self.assertEqual(nb_as_file['type'], 'file')
212 214 self.assertEqual(nb_as_file['format'], 'text')
213 215 self.assertNotIsInstance(nb_as_file['content'], dict)
214 216
215 217 nb_as_bin_file = cm.get(path, content=True, type='file', format='base64')
216 218 self.assertEqual(nb_as_bin_file['format'], 'base64')
217 219
218 220 # Test in sub-directory
219 221 sub_dir = '/foo/'
220 222 self.make_dir('foo')
221 223 model = cm.new_untitled(path=sub_dir, ext='.ipynb')
222 224 model2 = cm.get(sub_dir + name)
223 225 assert isinstance(model2, dict)
224 226 self.assertIn('name', model2)
225 227 self.assertIn('path', model2)
226 228 self.assertIn('content', model2)
227 229 self.assertEqual(model2['name'], 'Untitled.ipynb')
228 230 self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name))
229 231
230 232 # Test with a regular file.
231 233 file_model_path = cm.new_untitled(path=sub_dir, ext='.txt')['path']
232 234 file_model = cm.get(file_model_path)
233 235 self.assertDictContainsSubset(
234 236 {
235 237 'content': u'',
236 238 'format': u'text',
237 239 'mimetype': u'text/plain',
238 240 'name': u'untitled.txt',
239 241 'path': u'foo/untitled.txt',
240 242 'type': u'file',
241 243 'writable': True,
242 244 },
243 245 file_model,
244 246 )
245 247 self.assertIn('created', file_model)
246 248 self.assertIn('last_modified', file_model)
247 249
248 250 # Test getting directory model
249 251
250 252 # Create a sub-sub directory to test getting directory contents with a
251 253 # subdir.
252 254 self.make_dir('foo/bar')
253 255 dirmodel = cm.get('foo')
254 256 self.assertEqual(dirmodel['type'], 'directory')
255 257 self.assertIsInstance(dirmodel['content'], list)
256 258 self.assertEqual(len(dirmodel['content']), 3)
257 259 self.assertEqual(dirmodel['path'], 'foo')
258 260 self.assertEqual(dirmodel['name'], 'foo')
259 261
260 262 # Directory contents should match the contents of each individual entry
261 263 # when requested with content=False.
262 264 model2_no_content = cm.get(sub_dir + name, content=False)
263 265 file_model_no_content = cm.get(u'foo/untitled.txt', content=False)
264 266 sub_sub_dir_no_content = cm.get('foo/bar', content=False)
265 267 self.assertEqual(sub_sub_dir_no_content['path'], 'foo/bar')
266 268 self.assertEqual(sub_sub_dir_no_content['name'], 'bar')
267 269
268 270 for entry in dirmodel['content']:
269 271 # Order isn't guaranteed by the spec, so this is a hacky way of
270 272 # verifying that all entries are matched.
271 273 if entry['path'] == sub_sub_dir_no_content['path']:
272 274 self.assertEqual(entry, sub_sub_dir_no_content)
273 275 elif entry['path'] == model2_no_content['path']:
274 276 self.assertEqual(entry, model2_no_content)
275 277 elif entry['path'] == file_model_no_content['path']:
276 278 self.assertEqual(entry, file_model_no_content)
277 279 else:
278 280 self.fail("Unexpected directory entry: %s" % entry())
279 281
280 282 with self.assertRaises(HTTPError):
281 283 cm.get('foo', type='file')
282 284
283 285 def test_update(self):
284 286 cm = self.contents_manager
285 287 # Create a notebook
286 288 model = cm.new_untitled(type='notebook')
287 289 name = model['name']
288 290 path = model['path']
289 291
290 292 # Change the name in the model for rename
291 293 model['path'] = 'test.ipynb'
292 294 model = cm.update(model, path)
293 295 assert isinstance(model, dict)
294 296 self.assertIn('name', model)
295 297 self.assertIn('path', model)
296 298 self.assertEqual(model['name'], 'test.ipynb')
297 299
298 300 # Make sure the old name is gone
299 301 self.assertRaises(HTTPError, cm.get, path)
300 302
301 303 # Test in sub-directory
302 304 # Create a directory and notebook in that directory
303 305 sub_dir = '/foo/'
304 306 self.make_dir('foo')
305 307 model = cm.new_untitled(path=sub_dir, type='notebook')
306 308 path = model['path']
307 309
308 310 # Change the name in the model for rename
309 311 d = path.rsplit('/', 1)[0]
310 312 new_path = model['path'] = d + '/test_in_sub.ipynb'
311 313 model = cm.update(model, path)
312 314 assert isinstance(model, dict)
313 315 self.assertIn('name', model)
314 316 self.assertIn('path', model)
315 317 self.assertEqual(model['name'], 'test_in_sub.ipynb')
316 318 self.assertEqual(model['path'], new_path)
317 319
318 320 # Make sure the old name is gone
319 321 self.assertRaises(HTTPError, cm.get, path)
320 322
321 323 def test_save(self):
322 324 cm = self.contents_manager
323 325 # Create a notebook
324 326 model = cm.new_untitled(type='notebook')
325 327 name = model['name']
326 328 path = model['path']
327 329
328 330 # Get the model with 'content'
329 331 full_model = cm.get(path)
330 332
331 333 # Save the notebook
332 334 model = cm.save(full_model, path)
333 335 assert isinstance(model, dict)
334 336 self.assertIn('name', model)
335 337 self.assertIn('path', model)
336 338 self.assertEqual(model['name'], name)
337 339 self.assertEqual(model['path'], path)
338 340
339 341 # Test in sub-directory
340 342 # Create a directory and notebook in that directory
341 343 sub_dir = '/foo/'
342 344 self.make_dir('foo')
343 345 model = cm.new_untitled(path=sub_dir, type='notebook')
344 346 name = model['name']
345 347 path = model['path']
346 348 model = cm.get(path)
347 349
348 350 # Change the name in the model for rename
349 351 model = cm.save(model, path)
350 352 assert isinstance(model, dict)
351 353 self.assertIn('name', model)
352 354 self.assertIn('path', model)
353 355 self.assertEqual(model['name'], 'Untitled.ipynb')
354 356 self.assertEqual(model['path'], 'foo/Untitled.ipynb')
355 357
356 358 def test_delete(self):
357 359 cm = self.contents_manager
358 360 # Create a notebook
359 361 nb, name, path = self.new_notebook()
360 362
361 363 # Delete the notebook
362 364 cm.delete(path)
363 365
364 366 # Check that deleting a non-existent path raises an error.
365 367 self.assertRaises(HTTPError, cm.delete, path)
366 368
367 369 # Check that a 'get' on the deleted notebook raises and error
368 370 self.assertRaises(HTTPError, cm.get, path)
369 371
370 372 def test_copy(self):
371 373 cm = self.contents_manager
372 374 parent = u'Γ₯ b'
373 375 name = u'nb √.ipynb'
374 376 path = u'{0}/{1}'.format(parent, name)
375 377 self.make_dir(parent)
376 378
377 379 orig = cm.new(path=path)
378 380 # copy with unspecified name
379 381 copy = cm.copy(path)
380 382 self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy1.ipynb'))
381 383
382 384 # copy with specified name
383 385 copy2 = cm.copy(path, u'Γ₯ b/copy 2.ipynb')
384 386 self.assertEqual(copy2['name'], u'copy 2.ipynb')
385 387 self.assertEqual(copy2['path'], u'Γ₯ b/copy 2.ipynb')
386 388 # copy with specified path
387 389 copy2 = cm.copy(path, u'/')
388 390 self.assertEqual(copy2['name'], name)
389 391 self.assertEqual(copy2['path'], name)
390 392
391 393 def test_trust_notebook(self):
392 394 cm = self.contents_manager
393 395 nb, name, path = self.new_notebook()
394 396
395 397 untrusted = cm.get(path)['content']
396 398 assert not cm.notary.check_cells(untrusted)
397 399
398 400 # print(untrusted)
399 401 cm.trust_notebook(path)
400 402 trusted = cm.get(path)['content']
401 403 # print(trusted)
402 404 assert cm.notary.check_cells(trusted)
403 405
404 406 def test_mark_trusted_cells(self):
405 407 cm = self.contents_manager
406 408 nb, name, path = self.new_notebook()
407 409
408 410 cm.mark_trusted_cells(nb, path)
409 411 for cell in nb.cells:
410 412 if cell.cell_type == 'code':
411 413 assert not cell.metadata.trusted
412 414
413 415 cm.trust_notebook(path)
414 416 nb = cm.get(path)['content']
415 417 for cell in nb.cells:
416 418 if cell.cell_type == 'code':
417 419 assert cell.metadata.trusted
418 420
419 421 def test_check_and_sign(self):
420 422 cm = self.contents_manager
421 423 nb, name, path = self.new_notebook()
422 424
423 425 cm.mark_trusted_cells(nb, path)
424 426 cm.check_and_sign(nb, path)
425 427 assert not cm.notary.check_signature(nb)
426 428
427 429 cm.trust_notebook(path)
428 430 nb = cm.get(path)['content']
429 431 cm.mark_trusted_cells(nb, path)
430 432 cm.check_and_sign(nb, path)
431 433 assert cm.notary.check_signature(nb)
432 434
General Comments 0
You need to be logged in to leave comments. Login now