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