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