test_largefilemanager.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from unittest import TestCase
  2. from ipython_genutils.tempdir import TemporaryDirectory
  3. from ..largefilemanager import LargeFileManager
  4. import os
  5. from tornado import web
  6. def _make_dir(contents_manager, api_path):
  7. """
  8. Make a directory.
  9. """
  10. os_path = contents_manager._get_os_path(api_path)
  11. try:
  12. os.makedirs(os_path)
  13. except OSError:
  14. print("Directory already exists: %r" % os_path)
  15. class TestLargeFileManager(TestCase):
  16. def setUp(self):
  17. self._temp_dir = TemporaryDirectory()
  18. self.td = self._temp_dir.name
  19. self.contents_manager = LargeFileManager(root_dir=self.td)
  20. def make_dir(self, api_path):
  21. """make a subdirectory at api_path
  22. override in subclasses if contents are not on the filesystem.
  23. """
  24. _make_dir(self.contents_manager, api_path)
  25. def test_save(self):
  26. cm = self.contents_manager
  27. # Create a notebook
  28. model = cm.new_untitled(type='notebook')
  29. name = model['name']
  30. path = model['path']
  31. # Get the model with 'content'
  32. full_model = cm.get(path)
  33. # Save the notebook
  34. model = cm.save(full_model, path)
  35. assert isinstance(model, dict)
  36. self.assertIn('name', model)
  37. self.assertIn('path', model)
  38. self.assertEqual(model['name'], name)
  39. self.assertEqual(model['path'], path)
  40. try:
  41. model = {'name': 'test', 'path': 'test', 'chunk': 1}
  42. cm.save(model, model['path'])
  43. except web.HTTPError as e:
  44. self.assertEqual('HTTP 400: Bad Request (No file type provided)', str(e))
  45. try:
  46. model = {'name': 'test', 'path': 'test', 'chunk': 1, 'type': 'notebook'}
  47. cm.save(model, model['path'])
  48. except web.HTTPError as e:
  49. self.assertEqual('HTTP 400: Bad Request (File type "notebook" is not supported for large file transfer)', str(e))
  50. try:
  51. model = {'name': 'test', 'path': 'test', 'chunk': 1, 'type': 'file'}
  52. cm.save(model, model['path'])
  53. except web.HTTPError as e:
  54. self.assertEqual('HTTP 400: Bad Request (No file content provided)', str(e))
  55. try:
  56. model = {'name': 'test', 'path': 'test', 'chunk': 2, 'type': 'file',
  57. 'content': u'test', 'format': 'json'}
  58. cm.save(model, model['path'])
  59. except web.HTTPError as e:
  60. self.assertEqual("HTTP 400: Bad Request (Must specify format of file contents as 'text' or 'base64')",
  61. str(e))
  62. # Save model for different chunks
  63. model = {'name': 'test', 'path': 'test', 'type': 'file',
  64. 'content': u'test==', 'format': 'text'}
  65. name = model['name']
  66. path = model['path']
  67. cm.save(model, path)
  68. for chunk in (1, 2, -1):
  69. for fm in ('text', 'base64'):
  70. full_model = cm.get(path)
  71. full_model['chunk'] = chunk
  72. full_model['format'] = fm
  73. model_res = cm.save(full_model, path)
  74. assert isinstance(model_res, dict)
  75. self.assertIn('name', model_res)
  76. self.assertIn('path', model_res)
  77. self.assertNotIn('chunk', model_res)
  78. self.assertEqual(model_res['name'], name)
  79. self.assertEqual(model_res['path'], path)
  80. # Test in sub-directory
  81. # Create a directory and notebook in that directory
  82. sub_dir = '/foo/'
  83. self.make_dir('foo')
  84. model = cm.new_untitled(path=sub_dir, type='notebook')
  85. name = model['name']
  86. path = model['path']
  87. model = cm.get(path)
  88. # Change the name in the model for rename
  89. model = cm.save(model, path)
  90. assert isinstance(model, dict)
  91. self.assertIn('name', model)
  92. self.assertIn('path', model)
  93. self.assertEqual(model['name'], 'Untitled.ipynb')
  94. self.assertEqual(model['path'], 'foo/Untitled.ipynb')