##// END OF EJS Templates
Add test for saving notebook with .py script
Thomas Kluyver -
Show More
@@ -1,231 +1,250
1 # coding: utf-8
1 # coding: utf-8
2 """Tests for the notebook manager."""
2 """Tests for the notebook manager."""
3 from __future__ import print_function
3 from __future__ import print_function
4
4
5 import os
5 import os
6
6
7 from tornado.web import HTTPError
7 from tornado.web import HTTPError
8 from unittest import TestCase
8 from unittest import TestCase
9 from tempfile import NamedTemporaryFile
9 from tempfile import NamedTemporaryFile
10
10
11 from IPython.utils.tempdir import TemporaryDirectory
11 from IPython.utils.tempdir import TemporaryDirectory
12 from IPython.utils.traitlets import TraitError
12 from IPython.utils.traitlets import TraitError
13 from IPython.html.utils import url_path_join
13 from IPython.html.utils import url_path_join
14
14
15 from ..filenbmanager import FileNotebookManager
15 from ..filenbmanager import FileNotebookManager
16 from ..nbmanager import NotebookManager
16 from ..nbmanager import NotebookManager
17
17
18 class TestFileNotebookManager(TestCase):
18 class TestFileNotebookManager(TestCase):
19
19
20 def test_nb_dir(self):
20 def test_nb_dir(self):
21 with TemporaryDirectory() as td:
21 with TemporaryDirectory() as td:
22 fm = FileNotebookManager(notebook_dir=td)
22 fm = FileNotebookManager(notebook_dir=td)
23 self.assertEqual(fm.notebook_dir, td)
23 self.assertEqual(fm.notebook_dir, td)
24
24
25 def test_create_nb_dir(self):
25 def test_create_nb_dir(self):
26 with TemporaryDirectory() as td:
26 with TemporaryDirectory() as td:
27 nbdir = os.path.join(td, 'notebooks')
27 nbdir = os.path.join(td, 'notebooks')
28 fm = FileNotebookManager(notebook_dir=nbdir)
28 fm = FileNotebookManager(notebook_dir=nbdir)
29 self.assertEqual(fm.notebook_dir, nbdir)
29 self.assertEqual(fm.notebook_dir, nbdir)
30
30
31 def test_missing_nb_dir(self):
31 def test_missing_nb_dir(self):
32 with TemporaryDirectory() as td:
32 with TemporaryDirectory() as td:
33 nbdir = os.path.join(td, 'notebook', 'dir', 'is', 'missing')
33 nbdir = os.path.join(td, 'notebook', 'dir', 'is', 'missing')
34 self.assertRaises(TraitError, FileNotebookManager, notebook_dir=nbdir)
34 self.assertRaises(TraitError, FileNotebookManager, notebook_dir=nbdir)
35
35
36 def test_invalid_nb_dir(self):
36 def test_invalid_nb_dir(self):
37 with NamedTemporaryFile() as tf:
37 with NamedTemporaryFile() as tf:
38 self.assertRaises(TraitError, FileNotebookManager, notebook_dir=tf.name)
38 self.assertRaises(TraitError, FileNotebookManager, notebook_dir=tf.name)
39
39
40 def test_get_os_path(self):
40 def test_get_os_path(self):
41 # full filesystem path should be returned with correct operating system
41 # full filesystem path should be returned with correct operating system
42 # separators.
42 # separators.
43 with TemporaryDirectory() as td:
43 with TemporaryDirectory() as td:
44 nbdir = os.path.join(td, 'notebooks')
44 nbdir = os.path.join(td, 'notebooks')
45 fm = FileNotebookManager(notebook_dir=nbdir)
45 fm = FileNotebookManager(notebook_dir=nbdir)
46 path = fm.get_os_path('test.ipynb', '/path/to/notebook/')
46 path = fm.get_os_path('test.ipynb', '/path/to/notebook/')
47 rel_path_list = '/path/to/notebook/test.ipynb'.split('/')
47 rel_path_list = '/path/to/notebook/test.ipynb'.split('/')
48 fs_path = os.path.join(fm.notebook_dir, *rel_path_list)
48 fs_path = os.path.join(fm.notebook_dir, *rel_path_list)
49 self.assertEqual(path, fs_path)
49 self.assertEqual(path, fs_path)
50
50
51 fm = FileNotebookManager(notebook_dir=nbdir)
51 fm = FileNotebookManager(notebook_dir=nbdir)
52 path = fm.get_os_path('test.ipynb')
52 path = fm.get_os_path('test.ipynb')
53 fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
53 fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
54 self.assertEqual(path, fs_path)
54 self.assertEqual(path, fs_path)
55
55
56 fm = FileNotebookManager(notebook_dir=nbdir)
56 fm = FileNotebookManager(notebook_dir=nbdir)
57 path = fm.get_os_path('test.ipynb', '////')
57 path = fm.get_os_path('test.ipynb', '////')
58 fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
58 fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
59 self.assertEqual(path, fs_path)
59 self.assertEqual(path, fs_path)
60
60
61 class TestNotebookManager(TestCase):
61 class TestNotebookManager(TestCase):
62
62
63 def make_dir(self, abs_path, rel_path):
63 def make_dir(self, abs_path, rel_path):
64 """make subdirectory, rel_path is the relative path
64 """make subdirectory, rel_path is the relative path
65 to that directory from the location where the server started"""
65 to that directory from the location where the server started"""
66 os_path = os.path.join(abs_path, rel_path)
66 os_path = os.path.join(abs_path, rel_path)
67 try:
67 try:
68 os.makedirs(os_path)
68 os.makedirs(os_path)
69 except OSError:
69 except OSError:
70 print("Directory already exists.")
70 print("Directory already exists.")
71
71
72 def test_create_notebook_model(self):
72 def test_create_notebook_model(self):
73 with TemporaryDirectory() as td:
73 with TemporaryDirectory() as td:
74 # Test in root directory
74 # Test in root directory
75 nm = FileNotebookManager(notebook_dir=td)
75 nm = FileNotebookManager(notebook_dir=td)
76 model = nm.create_notebook_model()
76 model = nm.create_notebook_model()
77 assert isinstance(model, dict)
77 assert isinstance(model, dict)
78 self.assertIn('name', model)
78 self.assertIn('name', model)
79 self.assertIn('path', model)
79 self.assertIn('path', model)
80 self.assertEqual(model['name'], 'Untitled0.ipynb')
80 self.assertEqual(model['name'], 'Untitled0.ipynb')
81 self.assertEqual(model['path'], '')
81 self.assertEqual(model['path'], '')
82
82
83 # Test in sub-directory
83 # Test in sub-directory
84 sub_dir = '/foo/'
84 sub_dir = '/foo/'
85 self.make_dir(nm.notebook_dir, 'foo')
85 self.make_dir(nm.notebook_dir, 'foo')
86 model = nm.create_notebook_model(None, sub_dir)
86 model = nm.create_notebook_model(None, sub_dir)
87 assert isinstance(model, dict)
87 assert isinstance(model, dict)
88 self.assertIn('name', model)
88 self.assertIn('name', model)
89 self.assertIn('path', model)
89 self.assertIn('path', model)
90 self.assertEqual(model['name'], 'Untitled0.ipynb')
90 self.assertEqual(model['name'], 'Untitled0.ipynb')
91 self.assertEqual(model['path'], sub_dir.strip('/'))
91 self.assertEqual(model['path'], sub_dir.strip('/'))
92
92
93 def test_get_notebook_model(self):
93 def test_get_notebook_model(self):
94 with TemporaryDirectory() as td:
94 with TemporaryDirectory() as td:
95 # Test in root directory
95 # Test in root directory
96 # Create a notebook
96 # Create a notebook
97 nm = FileNotebookManager(notebook_dir=td)
97 nm = FileNotebookManager(notebook_dir=td)
98 model = nm.create_notebook_model()
98 model = nm.create_notebook_model()
99 name = model['name']
99 name = model['name']
100 path = model['path']
100 path = model['path']
101
101
102 # Check that we 'get' on the notebook we just created
102 # Check that we 'get' on the notebook we just created
103 model2 = nm.get_notebook_model(name, path)
103 model2 = nm.get_notebook_model(name, path)
104 assert isinstance(model2, dict)
104 assert isinstance(model2, dict)
105 self.assertIn('name', model2)
105 self.assertIn('name', model2)
106 self.assertIn('path', model2)
106 self.assertIn('path', model2)
107 self.assertEqual(model['name'], name)
107 self.assertEqual(model['name'], name)
108 self.assertEqual(model['path'], path)
108 self.assertEqual(model['path'], path)
109
109
110 # Test in sub-directory
110 # Test in sub-directory
111 sub_dir = '/foo/'
111 sub_dir = '/foo/'
112 self.make_dir(nm.notebook_dir, 'foo')
112 self.make_dir(nm.notebook_dir, 'foo')
113 model = nm.create_notebook_model(None, sub_dir)
113 model = nm.create_notebook_model(None, sub_dir)
114 model2 = nm.get_notebook_model(name, sub_dir)
114 model2 = nm.get_notebook_model(name, sub_dir)
115 assert isinstance(model2, dict)
115 assert isinstance(model2, dict)
116 self.assertIn('name', model2)
116 self.assertIn('name', model2)
117 self.assertIn('path', model2)
117 self.assertIn('path', model2)
118 self.assertIn('content', model2)
118 self.assertIn('content', model2)
119 self.assertEqual(model2['name'], 'Untitled0.ipynb')
119 self.assertEqual(model2['name'], 'Untitled0.ipynb')
120 self.assertEqual(model2['path'], sub_dir.strip('/'))
120 self.assertEqual(model2['path'], sub_dir.strip('/'))
121
121
122 def test_update_notebook_model(self):
122 def test_update_notebook_model(self):
123 with TemporaryDirectory() as td:
123 with TemporaryDirectory() as td:
124 # Test in root directory
124 # Test in root directory
125 # Create a notebook
125 # Create a notebook
126 nm = FileNotebookManager(notebook_dir=td)
126 nm = FileNotebookManager(notebook_dir=td)
127 model = nm.create_notebook_model()
127 model = nm.create_notebook_model()
128 name = model['name']
128 name = model['name']
129 path = model['path']
129 path = model['path']
130
130
131 # Change the name in the model for rename
131 # Change the name in the model for rename
132 model['name'] = 'test.ipynb'
132 model['name'] = 'test.ipynb'
133 model = nm.update_notebook_model(model, name, path)
133 model = nm.update_notebook_model(model, name, path)
134 assert isinstance(model, dict)
134 assert isinstance(model, dict)
135 self.assertIn('name', model)
135 self.assertIn('name', model)
136 self.assertIn('path', model)
136 self.assertIn('path', model)
137 self.assertEqual(model['name'], 'test.ipynb')
137 self.assertEqual(model['name'], 'test.ipynb')
138
138
139 # Make sure the old name is gone
139 # Make sure the old name is gone
140 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
140 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
141
141
142 # Test in sub-directory
142 # Test in sub-directory
143 # Create a directory and notebook in that directory
143 # Create a directory and notebook in that directory
144 sub_dir = '/foo/'
144 sub_dir = '/foo/'
145 self.make_dir(nm.notebook_dir, 'foo')
145 self.make_dir(nm.notebook_dir, 'foo')
146 model = nm.create_notebook_model(None, sub_dir)
146 model = nm.create_notebook_model(None, sub_dir)
147 name = model['name']
147 name = model['name']
148 path = model['path']
148 path = model['path']
149
149
150 # Change the name in the model for rename
150 # Change the name in the model for rename
151 model['name'] = 'test_in_sub.ipynb'
151 model['name'] = 'test_in_sub.ipynb'
152 model = nm.update_notebook_model(model, name, path)
152 model = nm.update_notebook_model(model, name, path)
153 assert isinstance(model, dict)
153 assert isinstance(model, dict)
154 self.assertIn('name', model)
154 self.assertIn('name', model)
155 self.assertIn('path', model)
155 self.assertIn('path', model)
156 self.assertEqual(model['name'], 'test_in_sub.ipynb')
156 self.assertEqual(model['name'], 'test_in_sub.ipynb')
157 self.assertEqual(model['path'], sub_dir.strip('/'))
157 self.assertEqual(model['path'], sub_dir.strip('/'))
158
158
159 # Make sure the old name is gone
159 # Make sure the old name is gone
160 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
160 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
161
161
162 def test_save_notebook_model(self):
162 def test_save_notebook_model(self):
163 with TemporaryDirectory() as td:
163 with TemporaryDirectory() as td:
164 # Test in the root directory
164 # Test in the root directory
165 # Create a notebook
165 # Create a notebook
166 nm = FileNotebookManager(notebook_dir=td)
166 nm = FileNotebookManager(notebook_dir=td)
167 model = nm.create_notebook_model()
167 model = nm.create_notebook_model()
168 name = model['name']
168 name = model['name']
169 path = model['path']
169 path = model['path']
170
170
171 # Get the model with 'content'
171 # Get the model with 'content'
172 full_model = nm.get_notebook_model(name, path)
172 full_model = nm.get_notebook_model(name, path)
173
173
174 # Save the notebook
174 # Save the notebook
175 model = nm.save_notebook_model(full_model, name, path)
175 model = nm.save_notebook_model(full_model, name, path)
176 assert isinstance(model, dict)
176 assert isinstance(model, dict)
177 self.assertIn('name', model)
177 self.assertIn('name', model)
178 self.assertIn('path', model)
178 self.assertIn('path', model)
179 self.assertEqual(model['name'], name)
179 self.assertEqual(model['name'], name)
180 self.assertEqual(model['path'], path)
180 self.assertEqual(model['path'], path)
181
181
182 # Test in sub-directory
182 # Test in sub-directory
183 # Create a directory and notebook in that directory
183 # Create a directory and notebook in that directory
184 sub_dir = '/foo/'
184 sub_dir = '/foo/'
185 self.make_dir(nm.notebook_dir, 'foo')
185 self.make_dir(nm.notebook_dir, 'foo')
186 model = nm.create_notebook_model(None, sub_dir)
186 model = nm.create_notebook_model(None, sub_dir)
187 name = model['name']
187 name = model['name']
188 path = model['path']
188 path = model['path']
189 model = nm.get_notebook_model(name, path)
189 model = nm.get_notebook_model(name, path)
190
190
191 # Change the name in the model for rename
191 # Change the name in the model for rename
192 model = nm.save_notebook_model(model, name, path)
192 model = nm.save_notebook_model(model, name, path)
193 assert isinstance(model, dict)
193 assert isinstance(model, dict)
194 self.assertIn('name', model)
194 self.assertIn('name', model)
195 self.assertIn('path', model)
195 self.assertIn('path', model)
196 self.assertEqual(model['name'], 'Untitled0.ipynb')
196 self.assertEqual(model['name'], 'Untitled0.ipynb')
197 self.assertEqual(model['path'], sub_dir.strip('/'))
197 self.assertEqual(model['path'], sub_dir.strip('/'))
198
198
199 def test_save_notebook_with_script(self):
200 with TemporaryDirectory() as td:
201 # Create a notebook
202 nm = FileNotebookManager(notebook_dir=td)
203 nm.save_script = True
204 model = nm.create_notebook_model()
205 name = model['name']
206 path = model['path']
207
208 # Get the model with 'content'
209 full_model = nm.get_notebook_model(name, path)
210
211 # Save the notebook
212 model = nm.save_notebook_model(full_model, name, path)
213
214 # Check that the script was created
215 py_path = os.path.join(td, os.path.splitext(name)[0]+'.py')
216 assert os.path.exists(py_path), py_path
217
199 def test_delete_notebook_model(self):
218 def test_delete_notebook_model(self):
200 with TemporaryDirectory() as td:
219 with TemporaryDirectory() as td:
201 # Test in the root directory
220 # Test in the root directory
202 # Create a notebook
221 # Create a notebook
203 nm = FileNotebookManager(notebook_dir=td)
222 nm = FileNotebookManager(notebook_dir=td)
204 model = nm.create_notebook_model()
223 model = nm.create_notebook_model()
205 name = model['name']
224 name = model['name']
206 path = model['path']
225 path = model['path']
207
226
208 # Delete the notebook
227 # Delete the notebook
209 nm.delete_notebook_model(name, path)
228 nm.delete_notebook_model(name, path)
210
229
211 # Check that a 'get' on the deleted notebook raises and error
230 # Check that a 'get' on the deleted notebook raises and error
212 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
231 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
213
232
214 def test_copy_notebook(self):
233 def test_copy_notebook(self):
215 with TemporaryDirectory() as td:
234 with TemporaryDirectory() as td:
216 # Test in the root directory
235 # Test in the root directory
217 # Create a notebook
236 # Create a notebook
218 nm = FileNotebookManager(notebook_dir=td)
237 nm = FileNotebookManager(notebook_dir=td)
219 path = u'å b'
238 path = u'å b'
220 name = u'nb √.ipynb'
239 name = u'nb √.ipynb'
221 os.mkdir(os.path.join(td, path))
240 os.mkdir(os.path.join(td, path))
222 orig = nm.create_notebook_model({'name' : name}, path=path)
241 orig = nm.create_notebook_model({'name' : name}, path=path)
223
242
224 # copy with unspecified name
243 # copy with unspecified name
225 copy = nm.copy_notebook(name, path=path)
244 copy = nm.copy_notebook(name, path=path)
226 self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy0.ipynb'))
245 self.assertEqual(copy['name'], orig['name'].replace('.ipynb', '-Copy0.ipynb'))
227
246
228 # copy with specified name
247 # copy with specified name
229 copy2 = nm.copy_notebook(name, u'copy 2.ipynb', path=path)
248 copy2 = nm.copy_notebook(name, u'copy 2.ipynb', path=path)
230 self.assertEqual(copy2['name'], u'copy 2.ipynb')
249 self.assertEqual(copy2['name'], u'copy 2.ipynb')
231
250
General Comments 0
You need to be logged in to leave comments. Login now