##// END OF EJS Templates
jupyter -> IPython
jupyter -> IPython

File last commit:

r18255:e8b4bab5
r18533:b46a0ee8
Show More
test_manager.py
334 lines | 11.4 KiB | text/x-python | PythonLexer
MinRK
test nbmanager.copy_notebook
r13136 # coding: utf-8
MinRK
add tests for notebook_dir validation
r7637 """Tests for the notebook manager."""
Thomas Kluyver
Convert print statements to print function calls...
r13348 from __future__ import print_function
MinRK
add tests for notebook_dir validation
r7637
MinRK
Add Trust Notebook to File menu
r15655 import logging
MinRK
add tests for notebook_dir validation
r7637 import os
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
from tornado.web import HTTPError
MinRK
add tests for notebook_dir validation
r7637 from unittest import TestCase
from tempfile import NamedTemporaryFile
MinRK
Add Trust Notebook to File menu
r15655 from IPython.nbformat import current
MinRK
add tests for notebook_dir validation
r7637 from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.traitlets import TraitError
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 from IPython.html.utils import url_path_join
MinRK
fix directory listing with broken symlinks...
r17710 from IPython.testing import decorators as dec
MinRK
add tests for notebook_dir validation
r7637
MinRK
rename notebooks service to contents service...
r17524 from ..filemanager import FileContentsManager
from ..manager import ContentsManager
MinRK
add tests for notebook_dir validation
r7637
Brian E. Granger
Get the existing tests working.
r15079
MinRK
rename notebooks service to contents service...
r17524 class TestFileContentsManager(TestCase):
MinRK
add tests for notebook_dir validation
r7637
MinRK
rename notebooks service to contents service...
r17524 def test_root_dir(self):
MinRK
add tests for notebook_dir validation
r7637 with TemporaryDirectory() as td:
MinRK
rename notebooks service to contents service...
r17524 fm = FileContentsManager(root_dir=td)
self.assertEqual(fm.root_dir, td)
MinRK
add tests for notebook_dir validation
r7637
MinRK
rename notebooks service to contents service...
r17524 def test_missing_root_dir(self):
MinRK
add tests for notebook_dir validation
r7637 with TemporaryDirectory() as td:
MinRK
rename notebooks service to contents service...
r17524 root = os.path.join(td, 'notebook', 'dir', 'is', 'missing')
self.assertRaises(TraitError, FileContentsManager, root_dir=root)
MinRK
add tests for notebook_dir validation
r7637
MinRK
rename notebooks service to contents service...
r17524 def test_invalid_root_dir(self):
MinRK
add tests for notebook_dir validation
r7637 with NamedTemporaryFile() as tf:
MinRK
rename notebooks service to contents service...
r17524 self.assertRaises(TraitError, FileContentsManager, root_dir=tf.name)
MinRK
add tests for notebook_dir validation
r7637
Zachary Sailer
handle path separators with os.sep and add tests...
r13032 def test_get_os_path(self):
# full filesystem path should be returned with correct operating system
# separators.
with TemporaryDirectory() as td:
MinRK
rename notebooks service to contents service...
r17524 root = td
fm = FileContentsManager(root_dir=root)
MinRK
reorganize who knows what about paths...
r15420 path = fm._get_os_path('test.ipynb', '/path/to/notebook/')
Paul Ivanov
clean up of get_os_path and its tests...
r13034 rel_path_list = '/path/to/notebook/test.ipynb'.split('/')
MinRK
rename notebooks service to contents service...
r17524 fs_path = os.path.join(fm.root_dir, *rel_path_list)
Paul Ivanov
clean up of get_os_path and its tests...
r13034 self.assertEqual(path, fs_path)
MinRK
rename notebooks service to contents service...
r17524 fm = FileContentsManager(root_dir=root)
MinRK
reorganize who knows what about paths...
r15420 path = fm._get_os_path('test.ipynb')
MinRK
rename notebooks service to contents service...
r17524 fs_path = os.path.join(fm.root_dir, 'test.ipynb')
Paul Ivanov
clean up of get_os_path and its tests...
r13034 self.assertEqual(path, fs_path)
MinRK
rename notebooks service to contents service...
r17524 fm = FileContentsManager(root_dir=root)
MinRK
reorganize who knows what about paths...
r15420 path = fm._get_os_path('test.ipynb', '////')
MinRK
rename notebooks service to contents service...
r17524 fs_path = os.path.join(fm.root_dir, 'test.ipynb')
Paul Ivanov
clean up of get_os_path and its tests...
r13034 self.assertEqual(path, fs_path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
test notebook checkpoints in subdirectories
r16440 def test_checkpoint_subdir(self):
subd = u'sub ∂ir'
cp_name = 'test-cp.ipynb'
with TemporaryDirectory() as td:
MinRK
rename notebooks service to contents service...
r17524 root = td
MinRK
test notebook checkpoints in subdirectories
r16440 os.mkdir(os.path.join(td, subd))
MinRK
rename notebooks service to contents service...
r17524 fm = FileContentsManager(root_dir=root)
MinRK
test notebook checkpoints in subdirectories
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)
MinRK
rename notebooks service to contents service...
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))
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
handle path separators with os.sep and add tests...
r13032
MinRK
teach contents service about non-notebook files
r17525 class TestContentsManager(TestCase):
MinRK
mv services/notebooks services/contents
r17523
MinRK
Add Trust Notebook to File menu
r15655 def setUp(self):
self._temp_dir = TemporaryDirectory()
self.td = self._temp_dir.name
MinRK
rename notebooks service to contents service...
r17524 self.contents_manager = FileContentsManager(
root_dir=self.td,
MinRK
cleanup test_nbmanager...
r15656 log=logging.getLogger()
)
MinRK
mv services/notebooks services/contents
r17523
MinRK
Add Trust Notebook to File menu
r15655 def tearDown(self):
self._temp_dir.cleanup()
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
refactoring of nbmanager and filenbmanager...
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:
Brian E. Granger
Adding dashboard navigation tests for dir browsing.
r15080 print("Directory already exists: %r" % os_path)
MinRK
fix directory listing with broken symlinks...
r17710 return os_path
MinRK
mv services/notebooks services/contents
r17523
MinRK
Add Trust Notebook to File menu
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)
MinRK
mv services/notebooks services/contents
r17523
MinRK
Add Trust Notebook to File menu
r15655 def new_notebook(self):
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file()
MinRK
Add Trust Notebook to File menu
r15655 name = model['name']
path = model['path']
MinRK
mv services/notebooks services/contents
r17523
MinRK
teach contents service about non-notebook files
r17525 full_model = cm.get_model(name, path)
MinRK
Add Trust Notebook to File menu
r15655 nb = full_model['content']
self.add_code_cell(nb)
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 cm.save(full_model, name, path)
MinRK
Add Trust Notebook to File menu
r15655 return nb, name, path
MinRK
mv services/notebooks services/contents
r17523
MinRK
add support and tests for uploading and saving regular files
r17527 def test_create_file(self):
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
MinRK
cleanup test_nbmanager...
r15656 # Test in root directory
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file()
MinRK
cleanup test_nbmanager...
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/'
MinRK
rename notebooks service to contents service...
r17524 self.make_dir(cm.root_dir, 'foo')
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file(None, sub_dir)
MinRK
cleanup test_nbmanager...
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('/'))
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
rename notebooks service to contents service...
r17524 def test_get(self):
cm = self.contents_manager
MinRK
cleanup test_nbmanager...
r15656 # Create a notebook
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file()
MinRK
cleanup test_nbmanager...
r15656 name = model['name']
path = model['path']
# Check that we 'get' on the notebook we just created
MinRK
teach contents service about non-notebook files
r17525 model2 = cm.get_model(name, path)
MinRK
cleanup test_nbmanager...
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/'
MinRK
rename notebooks service to contents service...
r17524 self.make_dir(cm.root_dir, 'foo')
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file(None, sub_dir)
MinRK
teach contents service about non-notebook files
r17525 model2 = cm.get_model(name, sub_dir)
MinRK
cleanup test_nbmanager...
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('/'))
MinRK
fix directory listing with broken symlinks...
r17710
@dec.skip_win32
def test_bad_symlink(self):
cm = self.contents_manager
path = 'test bad symlink'
os_path = self.make_dir(cm.root_dir, path)
file_model = cm.create_file(path=path, ext='.txt')
# create a broken symlink
os.symlink("target", os.path.join(os_path, "bad symlink"))
model = cm.get_model(path)
self.assertEqual(model['content'], [file_model])
@dec.skip_win32
def test_good_symlink(self):
cm = self.contents_manager
path = 'test good symlink'
os_path = self.make_dir(cm.root_dir, path)
file_model = cm.create_file(path=path, ext='.txt')
# create a good symlink
os.symlink(file_model['name'], os.path.join(os_path, "good symlink"))
symlink_model = cm.get_model(name="good symlink", path=path, content=False)
dir_model = cm.get_model(path)
self.assertEqual(
Thomas Kluyver
Sort directory contents in test before assertion...
r17787 sorted(dir_model['content'], key=lambda x: x['name']),
MinRK
fix directory listing with broken symlinks...
r17710 [symlink_model, file_model],
)
MinRK
rename notebooks service to contents service...
r17524 def test_update(self):
cm = self.contents_manager
MinRK
cleanup test_nbmanager...
r15656 # Create a notebook
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file()
MinRK
cleanup test_nbmanager...
r15656 name = model['name']
path = model['path']
# Change the name in the model for rename
model['name'] = 'test.ipynb'
MinRK
rename notebooks service to contents service...
r17524 model = cm.update(model, name, path)
MinRK
cleanup test_nbmanager...
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
MinRK
teach contents service about non-notebook files
r17525 self.assertRaises(HTTPError, cm.get_model, name, path)
MinRK
cleanup test_nbmanager...
r15656
# Test in sub-directory
# Create a directory and notebook in that directory
sub_dir = '/foo/'
MinRK
rename notebooks service to contents service...
r17524 self.make_dir(cm.root_dir, 'foo')
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file(None, sub_dir)
MinRK
cleanup test_nbmanager...
r15656 name = model['name']
path = model['path']
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # Change the name in the model for rename
model['name'] = 'test_in_sub.ipynb'
MinRK
rename notebooks service to contents service...
r17524 model = cm.update(model, name, path)
MinRK
cleanup test_nbmanager...
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('/'))
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # Make sure the old name is gone
MinRK
teach contents service about non-notebook files
r17525 self.assertRaises(HTTPError, cm.get_model, name, path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
rename notebooks service to contents service...
r17524 def test_save(self):
cm = self.contents_manager
MinRK
cleanup test_nbmanager...
r15656 # Create a notebook
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file()
MinRK
cleanup test_nbmanager...
r15656 name = model['name']
path = model['path']
# Get the model with 'content'
MinRK
teach contents service about non-notebook files
r17525 full_model = cm.get_model(name, path)
MinRK
cleanup test_nbmanager...
r15656
# Save the notebook
MinRK
rename notebooks service to contents service...
r17524 model = cm.save(full_model, name, path)
MinRK
cleanup test_nbmanager...
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/'
MinRK
rename notebooks service to contents service...
r17524 self.make_dir(cm.root_dir, 'foo')
MinRK
add support and tests for uploading and saving regular files
r17527 model = cm.create_file(None, sub_dir)
MinRK
cleanup test_nbmanager...
r15656 name = model['name']
path = model['path']
MinRK
teach contents service about non-notebook files
r17525 model = cm.get_model(name, path)
MinRK
cleanup test_nbmanager...
r15656
# Change the name in the model for rename
MinRK
rename notebooks service to contents service...
r17524 model = cm.save(model, name, path)
MinRK
cleanup test_nbmanager...
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('/'))
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
rename notebooks service to contents service...
r17524 def test_delete(self):
cm = self.contents_manager
MinRK
cleanup test_nbmanager...
r15656 # Create a notebook
nb, name, path = self.new_notebook()
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # Delete the notebook
MinRK
rename notebooks service to contents service...
r17524 cm.delete(name, path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # Check that a 'get' on the deleted notebook raises and error
MinRK
teach contents service about non-notebook files
r17525 self.assertRaises(HTTPError, cm.get_model, name, path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 def test_copy(self):
cm = self.contents_manager
MinRK
cleanup test_nbmanager...
r15656 path = u'Ã¥ b'
name = u'nb √.ipynb'
MinRK
rename notebooks service to contents service...
r17524 os.mkdir(os.path.join(cm.root_dir, path))
MinRK
add support and tests for uploading and saving regular files
r17527 orig = cm.create_file({'name' : name}, path=path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # copy with unspecified name
MinRK
rename notebooks service to contents service...
r17524 copy = cm.copy(name, path=path)
MinRK
cleanup test_nbmanager...
r15656 self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy0.ipynb'))
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # copy with specified name
MinRK
rename notebooks service to contents service...
r17524 copy2 = cm.copy(name, u'copy 2.ipynb', path=path)
MinRK
cleanup test_nbmanager...
r15656 self.assertEqual(copy2['name'], u'copy 2.ipynb')
MinRK
mv services/notebooks services/contents
r17523
MinRK
Add Trust Notebook to File menu
r15655 def test_trust_notebook(self):
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
MinRK
Add Trust Notebook to File menu
r15655 nb, name, path = self.new_notebook()
MinRK
mv services/notebooks services/contents
r17523
MinRK
teach contents service about non-notebook files
r17525 untrusted = cm.get_model(name, path)['content']
MinRK
rename notebooks service to contents service...
r17524 assert not cm.notary.check_cells(untrusted)
MinRK
mv services/notebooks services/contents
r17523
MinRK
cleanup test_nbmanager...
r15656 # print(untrusted)
MinRK
rename notebooks service to contents service...
r17524 cm.trust_notebook(name, path)
MinRK
teach contents service about non-notebook files
r17525 trusted = cm.get_model(name, path)['content']
MinRK
cleanup test_nbmanager...
r15656 # print(trusted)
MinRK
rename notebooks service to contents service...
r17524 assert cm.notary.check_cells(trusted)
MinRK
mv services/notebooks services/contents
r17523
MinRK
Add Trust Notebook to File menu
r15655 def test_mark_trusted_cells(self):
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
MinRK
Add Trust Notebook to File menu
r15655 nb, name, path = self.new_notebook()
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 cm.mark_trusted_cells(nb, name, path)
MinRK
Add Trust Notebook to File menu
r15655 for cell in nb.worksheets[0].cells:
if cell.cell_type == 'code':
MinRK
trust is stored in code_cell.metadata...
r18255 assert not cell.metadata.trusted
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 cm.trust_notebook(name, path)
MinRK
teach contents service about non-notebook files
r17525 nb = cm.get_model(name, path)['content']
MinRK
Add Trust Notebook to File menu
r15655 for cell in nb.worksheets[0].cells:
if cell.cell_type == 'code':
MinRK
trust is stored in code_cell.metadata...
r18255 assert cell.metadata.trusted
MinRK
Add Trust Notebook to File menu
r15655
def test_check_and_sign(self):
MinRK
rename notebooks service to contents service...
r17524 cm = self.contents_manager
MinRK
Add Trust Notebook to File menu
r15655 nb, name, path = self.new_notebook()
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 cm.mark_trusted_cells(nb, name, path)
cm.check_and_sign(nb, name, path)
assert not cm.notary.check_signature(nb)
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 cm.trust_notebook(name, path)
MinRK
teach contents service about non-notebook files
r17525 nb = cm.get_model(name, path)['content']
MinRK
rename notebooks service to contents service...
r17524 cm.mark_trusted_cells(nb, name, path)
cm.check_and_sign(nb, name, path)
assert cm.notary.check_signature(nb)