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