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