test_manager.py
301 lines
| 10.2 KiB
| text/x-python
|
PythonLexer
|
r13136 | # coding: utf-8 | ||
|
r7637 | """Tests for the notebook manager.""" | ||
|
r13348 | from __future__ import print_function | ||
|
r7637 | |||
|
r15655 | import logging | ||
|
r7637 | import os | ||
|
r13046 | |||
from tornado.web import HTTPError | ||||
|
r7637 | from unittest import TestCase | ||
from tempfile import NamedTemporaryFile | ||||
|
r15655 | from IPython.nbformat import current | ||
|
r7637 | from IPython.utils.tempdir import TemporaryDirectory | ||
from IPython.utils.traitlets import TraitError | ||||
|
r13046 | from IPython.html.utils import url_path_join | ||
|
r7637 | |||
|
r17524 | from ..filemanager import FileContentsManager | ||
from ..manager import ContentsManager | ||||
|
r7637 | |||
|
r15079 | |||
|
r17524 | class TestFileContentsManager(TestCase): | ||
|
r7637 | |||
|
r17524 | def test_root_dir(self): | ||
|
r7637 | with TemporaryDirectory() as td: | ||
|
r17524 | fm = FileContentsManager(root_dir=td) | ||
self.assertEqual(fm.root_dir, td) | ||||
|
r7637 | |||
|
r17524 | def test_missing_root_dir(self): | ||
|
r7637 | with TemporaryDirectory() as td: | ||
|
r17524 | root = os.path.join(td, 'notebook', 'dir', 'is', 'missing') | ||
self.assertRaises(TraitError, FileContentsManager, root_dir=root) | ||||
|
r7637 | |||
|
r17524 | def test_invalid_root_dir(self): | ||
|
r7637 | with NamedTemporaryFile() as tf: | ||
|
r17524 | self.assertRaises(TraitError, FileContentsManager, root_dir=tf.name) | ||
|
r7637 | |||
|
r13032 | def test_get_os_path(self): | ||
# full filesystem path should be returned with correct operating system | ||||
# separators. | ||||
with TemporaryDirectory() as td: | ||||
|
r17524 | root = td | ||
fm = FileContentsManager(root_dir=root) | ||||
|
r15420 | path = fm._get_os_path('test.ipynb', '/path/to/notebook/') | ||
|
r13034 | rel_path_list = '/path/to/notebook/test.ipynb'.split('/') | ||
|
r17524 | fs_path = os.path.join(fm.root_dir, *rel_path_list) | ||
|
r13034 | self.assertEqual(path, fs_path) | ||
|
r17524 | fm = FileContentsManager(root_dir=root) | ||
|
r15420 | path = fm._get_os_path('test.ipynb') | ||
|
r17524 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') | ||
|
r13034 | self.assertEqual(path, fs_path) | ||
|
r17524 | fm = FileContentsManager(root_dir=root) | ||
|
r15420 | path = fm._get_os_path('test.ipynb', '////') | ||
|
r17524 | fs_path = os.path.join(fm.root_dir, 'test.ipynb') | ||
|
r13034 | self.assertEqual(path, fs_path) | ||
|
r17523 | |||
|
r16440 | def test_checkpoint_subdir(self): | ||
subd = u'sub ∂ir' | ||||
cp_name = 'test-cp.ipynb' | ||||
with TemporaryDirectory() as td: | ||||
|
r17524 | root = td | ||
|
r16440 | os.mkdir(os.path.join(td, subd)) | ||
|
r17524 | fm = FileContentsManager(root_dir=root) | ||
|
r16440 | cp_dir = fm.get_checkpoint_path('cp', 'test.ipynb', '/') | ||
cp_subdir = fm.get_checkpoint_path('cp', 'test.ipynb', '/%s/' % subd) | ||||
self.assertNotEqual(cp_dir, cp_subdir) | ||||
|
r17524 | self.assertEqual(cp_dir, os.path.join(root, fm.checkpoint_dir, cp_name)) | ||
self.assertEqual(cp_subdir, os.path.join(root, subd, fm.checkpoint_dir, cp_name)) | ||||
|
r17523 | |||
|
r13032 | |||
|
r17525 | class TestContentsManager(TestCase): | ||
|
r17523 | |||
|
r15655 | def setUp(self): | ||
self._temp_dir = TemporaryDirectory() | ||||
self.td = self._temp_dir.name | ||||
|
r17524 | self.contents_manager = FileContentsManager( | ||
root_dir=self.td, | ||||
|
r15656 | log=logging.getLogger() | ||
) | ||||
|
r17523 | |||
|
r15655 | def tearDown(self): | ||
self._temp_dir.cleanup() | ||||
|
r17523 | |||
|
r13046 | def make_dir(self, abs_path, rel_path): | ||
"""make subdirectory, rel_path is the relative path | ||||
to that directory from the location where the server started""" | ||||
os_path = os.path.join(abs_path, rel_path) | ||||
try: | ||||
os.makedirs(os_path) | ||||
except OSError: | ||||
|
r15080 | print("Directory already exists: %r" % os_path) | ||
|
r17523 | |||
|
r15655 | def add_code_cell(self, nb): | ||
output = current.new_output("display_data", output_javascript="alert('hi');") | ||||
cell = current.new_code_cell("print('hi')", outputs=[output]) | ||||
if not nb.worksheets: | ||||
nb.worksheets.append(current.new_worksheet()) | ||||
nb.worksheets[0].cells.append(cell) | ||||
|
r17523 | |||
|
r15655 | def new_notebook(self): | ||
|
r17524 | cm = self.contents_manager | ||
|
r17527 | model = cm.create_file() | ||
|
r15655 | name = model['name'] | ||
path = model['path'] | ||||
|
r17523 | |||
|
r17525 | full_model = cm.get_model(name, path) | ||
|
r15655 | nb = full_model['content'] | ||
self.add_code_cell(nb) | ||||
|
r17523 | |||
|
r17524 | cm.save(full_model, name, path) | ||
|
r15655 | return nb, name, path | ||
|
r17523 | |||
|
r17527 | def test_create_file(self): | ||
|
r17524 | cm = self.contents_manager | ||
|
r15656 | # Test in root directory | ||
|
r17527 | model = cm.create_file() | ||
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'Untitled0.ipynb') | ||||
self.assertEqual(model['path'], '') | ||||
# Test in sub-directory | ||||
sub_dir = '/foo/' | ||||
|
r17524 | self.make_dir(cm.root_dir, 'foo') | ||
|
r17527 | model = cm.create_file(None, sub_dir) | ||
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'Untitled0.ipynb') | ||||
self.assertEqual(model['path'], sub_dir.strip('/')) | ||||
|
r13046 | |||
|
r17524 | def test_get(self): | ||
cm = self.contents_manager | ||||
|
r15656 | # Create a notebook | ||
|
r17527 | model = cm.create_file() | ||
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
# Check that we 'get' on the notebook we just created | ||||
|
r17525 | model2 = cm.get_model(name, path) | ||
|
r15656 | assert isinstance(model2, dict) | ||
self.assertIn('name', model2) | ||||
self.assertIn('path', model2) | ||||
self.assertEqual(model['name'], name) | ||||
self.assertEqual(model['path'], path) | ||||
# Test in sub-directory | ||||
sub_dir = '/foo/' | ||||
|
r17524 | self.make_dir(cm.root_dir, 'foo') | ||
|
r17527 | model = cm.create_file(None, sub_dir) | ||
|
r17525 | model2 = cm.get_model(name, sub_dir) | ||
|
r15656 | assert isinstance(model2, dict) | ||
self.assertIn('name', model2) | ||||
self.assertIn('path', model2) | ||||
self.assertIn('content', model2) | ||||
self.assertEqual(model2['name'], 'Untitled0.ipynb') | ||||
self.assertEqual(model2['path'], sub_dir.strip('/')) | ||||
|
r17523 | |||
|
r17524 | def test_update(self): | ||
cm = self.contents_manager | ||||
|
r15656 | # Create a notebook | ||
|
r17527 | model = cm.create_file() | ||
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
# Change the name in the model for rename | ||||
model['name'] = 'test.ipynb' | ||||
|
r17524 | model = cm.update(model, name, path) | ||
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'test.ipynb') | ||||
# Make sure the old name is gone | ||||
|
r17525 | self.assertRaises(HTTPError, cm.get_model, name, path) | ||
|
r15656 | |||
# Test in sub-directory | ||||
# Create a directory and notebook in that directory | ||||
sub_dir = '/foo/' | ||||
|
r17524 | self.make_dir(cm.root_dir, 'foo') | ||
|
r17527 | model = cm.create_file(None, sub_dir) | ||
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
|
r17523 | |||
|
r15656 | # Change the name in the model for rename | ||
model['name'] = 'test_in_sub.ipynb' | ||||
|
r17524 | model = cm.update(model, name, path) | ||
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'test_in_sub.ipynb') | ||||
self.assertEqual(model['path'], sub_dir.strip('/')) | ||||
|
r17523 | |||
|
r15656 | # Make sure the old name is gone | ||
|
r17525 | self.assertRaises(HTTPError, cm.get_model, name, path) | ||
|
r13046 | |||
|
r17524 | def test_save(self): | ||
cm = self.contents_manager | ||||
|
r15656 | # Create a notebook | ||
|
r17527 | model = cm.create_file() | ||
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
# Get the model with 'content' | ||||
|
r17525 | full_model = cm.get_model(name, path) | ||
|
r15656 | |||
# Save the notebook | ||||
|
r17524 | model = cm.save(full_model, name, path) | ||
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], name) | ||||
self.assertEqual(model['path'], path) | ||||
# Test in sub-directory | ||||
# Create a directory and notebook in that directory | ||||
sub_dir = '/foo/' | ||||
|
r17524 | self.make_dir(cm.root_dir, 'foo') | ||
|
r17527 | model = cm.create_file(None, sub_dir) | ||
|
r15656 | name = model['name'] | ||
path = model['path'] | ||||
|
r17525 | model = cm.get_model(name, path) | ||
|
r15656 | |||
# Change the name in the model for rename | ||||
|
r17524 | model = cm.save(model, name, path) | ||
|
r15656 | assert isinstance(model, dict) | ||
self.assertIn('name', model) | ||||
self.assertIn('path', model) | ||||
self.assertEqual(model['name'], 'Untitled0.ipynb') | ||||
self.assertEqual(model['path'], sub_dir.strip('/')) | ||||
|
r13046 | |||
|
r17524 | def test_delete(self): | ||
cm = self.contents_manager | ||||
|
r15656 | # Create a notebook | ||
nb, name, path = self.new_notebook() | ||||
|
r17523 | |||
|
r15656 | # Delete the notebook | ||
|
r17524 | cm.delete(name, path) | ||
|
r17523 | |||
|
r15656 | # Check that a 'get' on the deleted notebook raises and error | ||
|
r17525 | self.assertRaises(HTTPError, cm.get_model, name, path) | ||
|
r17523 | |||
|
r17524 | def test_copy(self): | ||
cm = self.contents_manager | ||||
|
r15656 | path = u'Ã¥ b' | ||
name = u'nb √.ipynb' | ||||
|
r17524 | os.mkdir(os.path.join(cm.root_dir, path)) | ||
|
r17527 | orig = cm.create_file({'name' : name}, path=path) | ||
|
r17523 | |||
|
r15656 | # copy with unspecified name | ||
|
r17524 | copy = cm.copy(name, path=path) | ||
|
r15656 | self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy0.ipynb')) | ||
|
r17523 | |||
|
r15656 | # copy with specified name | ||
|
r17524 | copy2 = cm.copy(name, u'copy 2.ipynb', path=path) | ||
|
r15656 | self.assertEqual(copy2['name'], u'copy 2.ipynb') | ||
|
r17523 | |||
|
r15655 | def test_trust_notebook(self): | ||
|
r17524 | cm = self.contents_manager | ||
|
r15655 | nb, name, path = self.new_notebook() | ||
|
r17523 | |||
|
r17525 | untrusted = cm.get_model(name, path)['content'] | ||
|
r17524 | assert not cm.notary.check_cells(untrusted) | ||
|
r17523 | |||
|
r15656 | # print(untrusted) | ||
|
r17524 | cm.trust_notebook(name, path) | ||
|
r17525 | trusted = cm.get_model(name, path)['content'] | ||
|
r15656 | # print(trusted) | ||
|
r17524 | assert cm.notary.check_cells(trusted) | ||
|
r17523 | |||
|
r15655 | def test_mark_trusted_cells(self): | ||
|
r17524 | cm = self.contents_manager | ||
|
r15655 | nb, name, path = self.new_notebook() | ||
|
r17523 | |||
|
r17524 | cm.mark_trusted_cells(nb, name, path) | ||
|
r15655 | for cell in nb.worksheets[0].cells: | ||
if cell.cell_type == 'code': | ||||
assert not cell.trusted | ||||
|
r17523 | |||
|
r17524 | cm.trust_notebook(name, path) | ||
|
r17525 | nb = cm.get_model(name, path)['content'] | ||
|
r15655 | for cell in nb.worksheets[0].cells: | ||
if cell.cell_type == 'code': | ||||
assert cell.trusted | ||||
def test_check_and_sign(self): | ||||
|
r17524 | cm = self.contents_manager | ||
|
r15655 | nb, name, path = self.new_notebook() | ||
|
r17523 | |||
|
r17524 | cm.mark_trusted_cells(nb, name, path) | ||
cm.check_and_sign(nb, name, path) | ||||
assert not cm.notary.check_signature(nb) | ||||
|
r17523 | |||
|
r17524 | cm.trust_notebook(name, path) | ||
|
r17525 | nb = cm.get_model(name, path)['content'] | ||
|
r17524 | cm.mark_trusted_cells(nb, name, path) | ||
cm.check_and_sign(nb, name, path) | ||||
assert cm.notary.check_signature(nb) | ||||