Show More
@@ -1,542 +1,542 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | """Test the contents webservice API.""" |
|
3 | 3 | |
|
4 | 4 | import base64 |
|
5 | 5 | import io |
|
6 | 6 | import json |
|
7 | 7 | import os |
|
8 | 8 | import shutil |
|
9 | 9 | from unicodedata import normalize |
|
10 | 10 | |
|
11 | 11 | pjoin = os.path.join |
|
12 | 12 | |
|
13 | 13 | import requests |
|
14 | 14 | |
|
15 | 15 | from IPython.html.utils import url_path_join, url_escape, to_os_path |
|
16 | 16 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error |
|
17 | 17 | from IPython.nbformat import read, write, from_dict |
|
18 | 18 | from IPython.nbformat.v4 import ( |
|
19 | 19 | new_notebook, new_markdown_cell, |
|
20 | 20 | ) |
|
21 | 21 | from IPython.nbformat import v2 |
|
22 | 22 | from IPython.utils import py3compat |
|
23 | 23 | from IPython.utils.data import uniq_stable |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | def notebooks_only(dir_model): |
|
27 | 27 | return [nb for nb in dir_model['content'] if nb['type']=='notebook'] |
|
28 | 28 | |
|
29 | 29 | def dirs_only(dir_model): |
|
30 | 30 | return [x for x in dir_model['content'] if x['type']=='directory'] |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class API(object): |
|
34 | 34 | """Wrapper for contents API calls.""" |
|
35 | 35 | def __init__(self, base_url): |
|
36 | 36 | self.base_url = base_url |
|
37 | 37 | |
|
38 | 38 | def _req(self, verb, path, body=None, params=None): |
|
39 | 39 | response = requests.request(verb, |
|
40 | 40 | url_path_join(self.base_url, 'api/contents', path), |
|
41 | 41 | data=body, params=params, |
|
42 | 42 | ) |
|
43 | 43 | response.raise_for_status() |
|
44 | 44 | return response |
|
45 | 45 | |
|
46 | 46 | def list(self, path='/'): |
|
47 | 47 | return self._req('GET', path) |
|
48 | 48 | |
|
49 | 49 | def read(self, path, type=None, format=None): |
|
50 | 50 | params = {} |
|
51 | 51 | if type is not None: |
|
52 | 52 | params['type'] = type |
|
53 | 53 | if format is not None: |
|
54 | 54 | params['format'] = format |
|
55 | 55 | return self._req('GET', path, params=params) |
|
56 | 56 | |
|
57 | 57 | def create_untitled(self, path='/', ext='.ipynb'): |
|
58 | 58 | body = None |
|
59 | 59 | if ext: |
|
60 | 60 | body = json.dumps({'ext': ext}) |
|
61 | 61 | return self._req('POST', path, body) |
|
62 | 62 | |
|
63 | 63 | def mkdir_untitled(self, path='/'): |
|
64 | 64 | return self._req('POST', path, json.dumps({'type': 'directory'})) |
|
65 | 65 | |
|
66 | 66 | def copy(self, copy_from, path='/'): |
|
67 | 67 | body = json.dumps({'copy_from':copy_from}) |
|
68 | 68 | return self._req('POST', path, body) |
|
69 | 69 | |
|
70 | 70 | def create(self, path='/'): |
|
71 | 71 | return self._req('PUT', path) |
|
72 | 72 | |
|
73 | 73 | def upload(self, path, body): |
|
74 | 74 | return self._req('PUT', path, body) |
|
75 | 75 | |
|
76 | 76 | def mkdir(self, path='/'): |
|
77 | 77 | return self._req('PUT', path, json.dumps({'type': 'directory'})) |
|
78 | 78 | |
|
79 | 79 | def copy_put(self, copy_from, path='/'): |
|
80 | 80 | body = json.dumps({'copy_from':copy_from}) |
|
81 | 81 | return self._req('PUT', path, body) |
|
82 | 82 | |
|
83 | 83 | def save(self, path, body): |
|
84 | 84 | return self._req('PUT', path, body) |
|
85 | 85 | |
|
86 | 86 | def delete(self, path='/'): |
|
87 | 87 | return self._req('DELETE', path) |
|
88 | 88 | |
|
89 | 89 | def rename(self, path, new_path): |
|
90 | 90 | body = json.dumps({'path': new_path}) |
|
91 | 91 | return self._req('PATCH', path, body) |
|
92 | 92 | |
|
93 | 93 | def get_checkpoints(self, path): |
|
94 | 94 | return self._req('GET', url_path_join(path, 'checkpoints')) |
|
95 | 95 | |
|
96 | 96 | def new_checkpoint(self, path): |
|
97 | 97 | return self._req('POST', url_path_join(path, 'checkpoints')) |
|
98 | 98 | |
|
99 | 99 | def restore_checkpoint(self, path, checkpoint_id): |
|
100 | 100 | return self._req('POST', url_path_join(path, 'checkpoints', checkpoint_id)) |
|
101 | 101 | |
|
102 | 102 | def delete_checkpoint(self, path, checkpoint_id): |
|
103 | 103 | return self._req('DELETE', url_path_join(path, 'checkpoints', checkpoint_id)) |
|
104 | 104 | |
|
105 | 105 | class APITest(NotebookTestBase): |
|
106 | 106 | """Test the kernels web service API""" |
|
107 | 107 | dirs_nbs = [('', 'inroot'), |
|
108 | 108 | ('Directory with spaces in', 'inspace'), |
|
109 | 109 | (u'unicodΓ©', 'innonascii'), |
|
110 | 110 | ('foo', 'a'), |
|
111 | 111 | ('foo', 'b'), |
|
112 | 112 | ('foo', 'name with spaces'), |
|
113 | 113 | ('foo', u'unicodΓ©'), |
|
114 | 114 | ('foo/bar', 'baz'), |
|
115 | 115 | ('ordering', 'A'), |
|
116 | 116 | ('ordering', 'b'), |
|
117 | 117 | ('ordering', 'C'), |
|
118 | 118 | (u'Γ₯ b', u'Γ§ d'), |
|
119 | 119 | ] |
|
120 | 120 | hidden_dirs = ['.hidden', '__pycache__'] |
|
121 | 121 | |
|
122 | dirs = uniq_stable([py3compat.cast_unicode(d) for (d,n) in dirs_nbs]) | |
|
123 | del dirs[0] # remove '' | |
|
122 | # Don't include root dir. | |
|
123 | dirs = uniq_stable([py3compat.cast_unicode(d) for (d,n) in dirs_nbs[1:]]) | |
|
124 | 124 | top_level_dirs = {normalize('NFC', d.split('/')[0]) for d in dirs} |
|
125 | 125 | |
|
126 | 126 | @staticmethod |
|
127 | 127 | def _blob_for_name(name): |
|
128 | 128 | return name.encode('utf-8') + b'\xFF' |
|
129 | 129 | |
|
130 | 130 | @staticmethod |
|
131 | 131 | def _txt_for_name(name): |
|
132 | 132 | return u'%s text file' % name |
|
133 | 133 | |
|
134 | 134 | def to_os_path(self, api_path): |
|
135 | 135 | return to_os_path(api_path, root=self.notebook_dir.name) |
|
136 | 136 | |
|
137 | 137 | def make_dir(self, api_path): |
|
138 | 138 | """Create a directory at api_path""" |
|
139 | 139 | os_path = self.to_os_path(api_path) |
|
140 | 140 | try: |
|
141 | 141 | os.makedirs(os_path) |
|
142 | 142 | except OSError: |
|
143 | 143 | print("Directory already exists: %r" % os_path) |
|
144 | 144 | |
|
145 | 145 | def make_txt(self, api_path, txt): |
|
146 | 146 | """Make a text file at a given api_path""" |
|
147 | 147 | os_path = self.to_os_path(api_path) |
|
148 | 148 | with io.open(os_path, 'w', encoding='utf-8') as f: |
|
149 | 149 | f.write(txt) |
|
150 | 150 | |
|
151 | 151 | def make_blob(self, api_path, blob): |
|
152 | 152 | """Make a binary file at a given api_path""" |
|
153 | 153 | os_path = self.to_os_path(api_path) |
|
154 | 154 | with io.open(os_path, 'wb') as f: |
|
155 | 155 | f.write(blob) |
|
156 | 156 | |
|
157 | 157 | def make_nb(self, api_path, nb): |
|
158 | 158 | """Make a notebook file at a given api_path""" |
|
159 | 159 | os_path = self.to_os_path(api_path) |
|
160 | 160 | |
|
161 | 161 | with io.open(os_path, 'w', encoding='utf-8') as f: |
|
162 | 162 | write(nb, f, version=4) |
|
163 | 163 | |
|
164 | 164 | def delete_dir(self, api_path): |
|
165 | 165 | """Delete a directory at api_path, removing any contents.""" |
|
166 | 166 | os_path = self.to_os_path(api_path) |
|
167 | 167 | shutil.rmtree(os_path, ignore_errors=True) |
|
168 | 168 | |
|
169 | 169 | def delete_file(self, api_path): |
|
170 | 170 | """Delete a file at the given path if it exists.""" |
|
171 | 171 | if self.isfile(api_path): |
|
172 | 172 | os.unlink(self.to_os_path(api_path)) |
|
173 | 173 | |
|
174 | 174 | def isfile(self, api_path): |
|
175 | 175 | return os.path.isfile(self.to_os_path(api_path)) |
|
176 | 176 | |
|
177 | 177 | def isdir(self, api_path): |
|
178 | 178 | return os.path.isdir(self.to_os_path(api_path)) |
|
179 | 179 | |
|
180 | 180 | def setUp(self): |
|
181 | 181 | |
|
182 | 182 | for d in (self.dirs + self.hidden_dirs): |
|
183 | 183 | self.make_dir(d) |
|
184 | 184 | |
|
185 | 185 | for d, name in self.dirs_nbs: |
|
186 | 186 | # create a notebook |
|
187 | 187 | nb = new_notebook() |
|
188 | 188 | self.make_nb(u'{}/{}.ipynb'.format(d, name), nb) |
|
189 | 189 | |
|
190 | 190 | # create a text file |
|
191 | 191 | txt = self._txt_for_name(name) |
|
192 | 192 | self.make_txt(u'{}/{}.txt'.format(d, name), txt) |
|
193 | 193 | |
|
194 | 194 | # create a binary file |
|
195 | 195 | blob = self._blob_for_name(name) |
|
196 | 196 | self.make_blob(u'{}/{}.blob'.format(d, name), blob) |
|
197 | 197 | |
|
198 | 198 | self.api = API(self.base_url()) |
|
199 | 199 | |
|
200 | 200 | def tearDown(self): |
|
201 | 201 | for dname in (list(self.top_level_dirs) + self.hidden_dirs): |
|
202 | 202 | self.delete_dir(dname) |
|
203 | 203 | self.delete_file('inroot.ipynb') |
|
204 | 204 | |
|
205 | 205 | def test_list_notebooks(self): |
|
206 | 206 | nbs = notebooks_only(self.api.list().json()) |
|
207 | 207 | self.assertEqual(len(nbs), 1) |
|
208 | 208 | self.assertEqual(nbs[0]['name'], 'inroot.ipynb') |
|
209 | 209 | |
|
210 | 210 | nbs = notebooks_only(self.api.list('/Directory with spaces in/').json()) |
|
211 | 211 | self.assertEqual(len(nbs), 1) |
|
212 | 212 | self.assertEqual(nbs[0]['name'], 'inspace.ipynb') |
|
213 | 213 | |
|
214 | 214 | nbs = notebooks_only(self.api.list(u'/unicodΓ©/').json()) |
|
215 | 215 | self.assertEqual(len(nbs), 1) |
|
216 | 216 | self.assertEqual(nbs[0]['name'], 'innonascii.ipynb') |
|
217 | 217 | self.assertEqual(nbs[0]['path'], u'unicodΓ©/innonascii.ipynb') |
|
218 | 218 | |
|
219 | 219 | nbs = notebooks_only(self.api.list('/foo/bar/').json()) |
|
220 | 220 | self.assertEqual(len(nbs), 1) |
|
221 | 221 | self.assertEqual(nbs[0]['name'], 'baz.ipynb') |
|
222 | 222 | self.assertEqual(nbs[0]['path'], 'foo/bar/baz.ipynb') |
|
223 | 223 | |
|
224 | 224 | nbs = notebooks_only(self.api.list('foo').json()) |
|
225 | 225 | self.assertEqual(len(nbs), 4) |
|
226 | 226 | nbnames = { normalize('NFC', n['name']) for n in nbs } |
|
227 | 227 | expected = [ u'a.ipynb', u'b.ipynb', u'name with spaces.ipynb', u'unicodΓ©.ipynb'] |
|
228 | 228 | expected = { normalize('NFC', name) for name in expected } |
|
229 | 229 | self.assertEqual(nbnames, expected) |
|
230 | 230 | |
|
231 | 231 | nbs = notebooks_only(self.api.list('ordering').json()) |
|
232 | 232 | nbnames = [n['name'] for n in nbs] |
|
233 | 233 | expected = ['A.ipynb', 'b.ipynb', 'C.ipynb'] |
|
234 | 234 | self.assertEqual(nbnames, expected) |
|
235 | 235 | |
|
236 | 236 | def test_list_dirs(self): |
|
237 | 237 | dirs = dirs_only(self.api.list().json()) |
|
238 | 238 | dir_names = {normalize('NFC', d['name']) for d in dirs} |
|
239 | 239 | self.assertEqual(dir_names, self.top_level_dirs) # Excluding hidden dirs |
|
240 | 240 | |
|
241 | 241 | def test_list_nonexistant_dir(self): |
|
242 | 242 | with assert_http_error(404): |
|
243 | 243 | self.api.list('nonexistant') |
|
244 | 244 | |
|
245 | 245 | def test_get_nb_contents(self): |
|
246 | 246 | for d, name in self.dirs_nbs: |
|
247 | 247 | path = url_path_join(d, name + '.ipynb') |
|
248 | 248 | nb = self.api.read(path).json() |
|
249 | 249 | self.assertEqual(nb['name'], u'%s.ipynb' % name) |
|
250 | 250 | self.assertEqual(nb['path'], path) |
|
251 | 251 | self.assertEqual(nb['type'], 'notebook') |
|
252 | 252 | self.assertIn('content', nb) |
|
253 | 253 | self.assertEqual(nb['format'], 'json') |
|
254 | 254 | self.assertIn('content', nb) |
|
255 | 255 | self.assertIn('metadata', nb['content']) |
|
256 | 256 | self.assertIsInstance(nb['content']['metadata'], dict) |
|
257 | 257 | |
|
258 | 258 | def test_get_contents_no_such_file(self): |
|
259 | 259 | # Name that doesn't exist - should be a 404 |
|
260 | 260 | with assert_http_error(404): |
|
261 | 261 | self.api.read('foo/q.ipynb') |
|
262 | 262 | |
|
263 | 263 | def test_get_text_file_contents(self): |
|
264 | 264 | for d, name in self.dirs_nbs: |
|
265 | 265 | path = url_path_join(d, name + '.txt') |
|
266 | 266 | model = self.api.read(path).json() |
|
267 | 267 | self.assertEqual(model['name'], u'%s.txt' % name) |
|
268 | 268 | self.assertEqual(model['path'], path) |
|
269 | 269 | self.assertIn('content', model) |
|
270 | 270 | self.assertEqual(model['format'], 'text') |
|
271 | 271 | self.assertEqual(model['type'], 'file') |
|
272 | 272 | self.assertEqual(model['content'], self._txt_for_name(name)) |
|
273 | 273 | |
|
274 | 274 | # Name that doesn't exist - should be a 404 |
|
275 | 275 | with assert_http_error(404): |
|
276 | 276 | self.api.read('foo/q.txt') |
|
277 | 277 | |
|
278 | 278 | # Specifying format=text should fail on a non-UTF-8 file |
|
279 | 279 | with assert_http_error(400): |
|
280 | 280 | self.api.read('foo/bar/baz.blob', type='file', format='text') |
|
281 | 281 | |
|
282 | 282 | def test_get_binary_file_contents(self): |
|
283 | 283 | for d, name in self.dirs_nbs: |
|
284 | 284 | path = url_path_join(d, name + '.blob') |
|
285 | 285 | model = self.api.read(path).json() |
|
286 | 286 | self.assertEqual(model['name'], u'%s.blob' % name) |
|
287 | 287 | self.assertEqual(model['path'], path) |
|
288 | 288 | self.assertIn('content', model) |
|
289 | 289 | self.assertEqual(model['format'], 'base64') |
|
290 | 290 | self.assertEqual(model['type'], 'file') |
|
291 | 291 | self.assertEqual( |
|
292 | 292 | base64.decodestring(model['content'].encode('ascii')), |
|
293 | 293 | self._blob_for_name(name), |
|
294 | 294 | ) |
|
295 | 295 | |
|
296 | 296 | # Name that doesn't exist - should be a 404 |
|
297 | 297 | with assert_http_error(404): |
|
298 | 298 | self.api.read('foo/q.txt') |
|
299 | 299 | |
|
300 | 300 | def test_get_bad_type(self): |
|
301 | 301 | with assert_http_error(400): |
|
302 | 302 | self.api.read(u'unicodΓ©', type='file') # this is a directory |
|
303 | 303 | |
|
304 | 304 | with assert_http_error(400): |
|
305 | 305 | self.api.read(u'unicodΓ©/innonascii.ipynb', type='directory') |
|
306 | 306 | |
|
307 | 307 | def _check_created(self, resp, path, type='notebook'): |
|
308 | 308 | self.assertEqual(resp.status_code, 201) |
|
309 | 309 | location_header = py3compat.str_to_unicode(resp.headers['Location']) |
|
310 | 310 | self.assertEqual(location_header, url_escape(url_path_join(u'/api/contents', path))) |
|
311 | 311 | rjson = resp.json() |
|
312 | 312 | self.assertEqual(rjson['name'], path.rsplit('/', 1)[-1]) |
|
313 | 313 | self.assertEqual(rjson['path'], path) |
|
314 | 314 | self.assertEqual(rjson['type'], type) |
|
315 | 315 | isright = self.isdir if type == 'directory' else self.isfile |
|
316 | 316 | assert isright(path) |
|
317 | 317 | |
|
318 | 318 | def test_create_untitled(self): |
|
319 | 319 | resp = self.api.create_untitled(path=u'Γ₯ b') |
|
320 | 320 | self._check_created(resp, u'Γ₯ b/Untitled.ipynb') |
|
321 | 321 | |
|
322 | 322 | # Second time |
|
323 | 323 | resp = self.api.create_untitled(path=u'Γ₯ b') |
|
324 | 324 | self._check_created(resp, u'Γ₯ b/Untitled1.ipynb') |
|
325 | 325 | |
|
326 | 326 | # And two directories down |
|
327 | 327 | resp = self.api.create_untitled(path='foo/bar') |
|
328 | 328 | self._check_created(resp, 'foo/bar/Untitled.ipynb') |
|
329 | 329 | |
|
330 | 330 | def test_create_untitled_txt(self): |
|
331 | 331 | resp = self.api.create_untitled(path='foo/bar', ext='.txt') |
|
332 | 332 | self._check_created(resp, 'foo/bar/untitled.txt', type='file') |
|
333 | 333 | |
|
334 | 334 | resp = self.api.read(path='foo/bar/untitled.txt') |
|
335 | 335 | model = resp.json() |
|
336 | 336 | self.assertEqual(model['type'], 'file') |
|
337 | 337 | self.assertEqual(model['format'], 'text') |
|
338 | 338 | self.assertEqual(model['content'], '') |
|
339 | 339 | |
|
340 | 340 | def test_upload(self): |
|
341 | 341 | nb = new_notebook() |
|
342 | 342 | nbmodel = {'content': nb, 'type': 'notebook'} |
|
343 | 343 | path = u'Γ₯ b/Upload tΓ©st.ipynb' |
|
344 | 344 | resp = self.api.upload(path, body=json.dumps(nbmodel)) |
|
345 | 345 | self._check_created(resp, path) |
|
346 | 346 | |
|
347 | 347 | def test_mkdir_untitled(self): |
|
348 | 348 | resp = self.api.mkdir_untitled(path=u'Γ₯ b') |
|
349 | 349 | self._check_created(resp, u'Γ₯ b/Untitled Folder', type='directory') |
|
350 | 350 | |
|
351 | 351 | # Second time |
|
352 | 352 | resp = self.api.mkdir_untitled(path=u'Γ₯ b') |
|
353 | 353 | self._check_created(resp, u'Γ₯ b/Untitled Folder 1', type='directory') |
|
354 | 354 | |
|
355 | 355 | # And two directories down |
|
356 | 356 | resp = self.api.mkdir_untitled(path='foo/bar') |
|
357 | 357 | self._check_created(resp, 'foo/bar/Untitled Folder', type='directory') |
|
358 | 358 | |
|
359 | 359 | def test_mkdir(self): |
|
360 | 360 | path = u'Γ₯ b/New βir' |
|
361 | 361 | resp = self.api.mkdir(path) |
|
362 | 362 | self._check_created(resp, path, type='directory') |
|
363 | 363 | |
|
364 | 364 | def test_mkdir_hidden_400(self): |
|
365 | 365 | with assert_http_error(400): |
|
366 | 366 | resp = self.api.mkdir(u'Γ₯ b/.hidden') |
|
367 | 367 | |
|
368 | 368 | def test_upload_txt(self): |
|
369 | 369 | body = u'ΓΌnicode tΓ©xt' |
|
370 | 370 | model = { |
|
371 | 371 | 'content' : body, |
|
372 | 372 | 'format' : 'text', |
|
373 | 373 | 'type' : 'file', |
|
374 | 374 | } |
|
375 | 375 | path = u'Γ₯ b/Upload tΓ©st.txt' |
|
376 | 376 | resp = self.api.upload(path, body=json.dumps(model)) |
|
377 | 377 | |
|
378 | 378 | # check roundtrip |
|
379 | 379 | resp = self.api.read(path) |
|
380 | 380 | model = resp.json() |
|
381 | 381 | self.assertEqual(model['type'], 'file') |
|
382 | 382 | self.assertEqual(model['format'], 'text') |
|
383 | 383 | self.assertEqual(model['content'], body) |
|
384 | 384 | |
|
385 | 385 | def test_upload_b64(self): |
|
386 | 386 | body = b'\xFFblob' |
|
387 | 387 | b64body = base64.encodestring(body).decode('ascii') |
|
388 | 388 | model = { |
|
389 | 389 | 'content' : b64body, |
|
390 | 390 | 'format' : 'base64', |
|
391 | 391 | 'type' : 'file', |
|
392 | 392 | } |
|
393 | 393 | path = u'Γ₯ b/Upload tΓ©st.blob' |
|
394 | 394 | resp = self.api.upload(path, body=json.dumps(model)) |
|
395 | 395 | |
|
396 | 396 | # check roundtrip |
|
397 | 397 | resp = self.api.read(path) |
|
398 | 398 | model = resp.json() |
|
399 | 399 | self.assertEqual(model['type'], 'file') |
|
400 | 400 | self.assertEqual(model['path'], path) |
|
401 | 401 | self.assertEqual(model['format'], 'base64') |
|
402 | 402 | decoded = base64.decodestring(model['content'].encode('ascii')) |
|
403 | 403 | self.assertEqual(decoded, body) |
|
404 | 404 | |
|
405 | 405 | def test_upload_v2(self): |
|
406 | 406 | nb = v2.new_notebook() |
|
407 | 407 | ws = v2.new_worksheet() |
|
408 | 408 | nb.worksheets.append(ws) |
|
409 | 409 | ws.cells.append(v2.new_code_cell(input='print("hi")')) |
|
410 | 410 | nbmodel = {'content': nb, 'type': 'notebook'} |
|
411 | 411 | path = u'Γ₯ b/Upload tΓ©st.ipynb' |
|
412 | 412 | resp = self.api.upload(path, body=json.dumps(nbmodel)) |
|
413 | 413 | self._check_created(resp, path) |
|
414 | 414 | resp = self.api.read(path) |
|
415 | 415 | data = resp.json() |
|
416 | 416 | self.assertEqual(data['content']['nbformat'], 4) |
|
417 | 417 | |
|
418 | 418 | def test_copy(self): |
|
419 | 419 | resp = self.api.copy(u'Γ₯ b/Γ§ d.ipynb', u'Γ₯ b') |
|
420 | 420 | self._check_created(resp, u'Γ₯ b/Γ§ d-Copy1.ipynb') |
|
421 | 421 | |
|
422 | 422 | resp = self.api.copy(u'Γ₯ b/Γ§ d.ipynb', u'Γ₯ b') |
|
423 | 423 | self._check_created(resp, u'Γ₯ b/Γ§ d-Copy2.ipynb') |
|
424 | 424 | |
|
425 | 425 | def test_copy_copy(self): |
|
426 | 426 | resp = self.api.copy(u'Γ₯ b/Γ§ d.ipynb', u'Γ₯ b') |
|
427 | 427 | self._check_created(resp, u'Γ₯ b/Γ§ d-Copy1.ipynb') |
|
428 | 428 | |
|
429 | 429 | resp = self.api.copy(u'Γ₯ b/Γ§ d-Copy1.ipynb', u'Γ₯ b') |
|
430 | 430 | self._check_created(resp, u'Γ₯ b/Γ§ d-Copy2.ipynb') |
|
431 | 431 | |
|
432 | 432 | def test_copy_path(self): |
|
433 | 433 | resp = self.api.copy(u'foo/a.ipynb', u'Γ₯ b') |
|
434 | 434 | self._check_created(resp, u'Γ₯ b/a.ipynb') |
|
435 | 435 | |
|
436 | 436 | resp = self.api.copy(u'foo/a.ipynb', u'Γ₯ b') |
|
437 | 437 | self._check_created(resp, u'Γ₯ b/a-Copy1.ipynb') |
|
438 | 438 | |
|
439 | 439 | def test_copy_put_400(self): |
|
440 | 440 | with assert_http_error(400): |
|
441 | 441 | resp = self.api.copy_put(u'Γ₯ b/Γ§ d.ipynb', u'Γ₯ b/cΓΈpy.ipynb') |
|
442 | 442 | |
|
443 | 443 | def test_copy_dir_400(self): |
|
444 | 444 | # can't copy directories |
|
445 | 445 | with assert_http_error(400): |
|
446 | 446 | resp = self.api.copy(u'Γ₯ b', u'foo') |
|
447 | 447 | |
|
448 | 448 | def test_delete(self): |
|
449 | 449 | for d, name in self.dirs_nbs: |
|
450 | 450 | print('%r, %r' % (d, name)) |
|
451 | 451 | resp = self.api.delete(url_path_join(d, name + '.ipynb')) |
|
452 | 452 | self.assertEqual(resp.status_code, 204) |
|
453 | 453 | |
|
454 | 454 | for d in self.dirs + ['/']: |
|
455 | 455 | nbs = notebooks_only(self.api.list(d).json()) |
|
456 | 456 | print('------') |
|
457 | 457 | print(d) |
|
458 | 458 | print(nbs) |
|
459 | 459 | self.assertEqual(nbs, []) |
|
460 | 460 | |
|
461 | 461 | def test_delete_dirs(self): |
|
462 | 462 | # depth-first delete everything, so we don't try to delete empty directories |
|
463 | 463 | for name in sorted(self.dirs + ['/'], key=len, reverse=True): |
|
464 | 464 | listing = self.api.list(name).json()['content'] |
|
465 | 465 | for model in listing: |
|
466 | 466 | self.api.delete(model['path']) |
|
467 | 467 | listing = self.api.list('/').json()['content'] |
|
468 | 468 | self.assertEqual(listing, []) |
|
469 | 469 | |
|
470 | 470 | def test_delete_non_empty_dir(self): |
|
471 | 471 | """delete non-empty dir raises 400""" |
|
472 | 472 | with assert_http_error(400): |
|
473 | 473 | self.api.delete(u'Γ₯ b') |
|
474 | 474 | |
|
475 | 475 | def test_rename(self): |
|
476 | 476 | resp = self.api.rename('foo/a.ipynb', 'foo/z.ipynb') |
|
477 | 477 | self.assertEqual(resp.headers['Location'].split('/')[-1], 'z.ipynb') |
|
478 | 478 | self.assertEqual(resp.json()['name'], 'z.ipynb') |
|
479 | 479 | self.assertEqual(resp.json()['path'], 'foo/z.ipynb') |
|
480 | 480 | assert self.isfile('foo/z.ipynb') |
|
481 | 481 | |
|
482 | 482 | nbs = notebooks_only(self.api.list('foo').json()) |
|
483 | 483 | nbnames = set(n['name'] for n in nbs) |
|
484 | 484 | self.assertIn('z.ipynb', nbnames) |
|
485 | 485 | self.assertNotIn('a.ipynb', nbnames) |
|
486 | 486 | |
|
487 | 487 | def test_rename_existing(self): |
|
488 | 488 | with assert_http_error(409): |
|
489 | 489 | self.api.rename('foo/a.ipynb', 'foo/b.ipynb') |
|
490 | 490 | |
|
491 | 491 | def test_save(self): |
|
492 | 492 | resp = self.api.read('foo/a.ipynb') |
|
493 | 493 | nbcontent = json.loads(resp.text)['content'] |
|
494 | 494 | nb = from_dict(nbcontent) |
|
495 | 495 | nb.cells.append(new_markdown_cell(u'Created by test Β³')) |
|
496 | 496 | |
|
497 | 497 | nbmodel= {'content': nb, 'type': 'notebook'} |
|
498 | 498 | resp = self.api.save('foo/a.ipynb', body=json.dumps(nbmodel)) |
|
499 | 499 | |
|
500 | 500 | nbcontent = self.api.read('foo/a.ipynb').json()['content'] |
|
501 | 501 | newnb = from_dict(nbcontent) |
|
502 | 502 | self.assertEqual(newnb.cells[0].source, |
|
503 | 503 | u'Created by test Β³') |
|
504 | 504 | |
|
505 | 505 | |
|
506 | 506 | def test_checkpoints(self): |
|
507 | 507 | resp = self.api.read('foo/a.ipynb') |
|
508 | 508 | r = self.api.new_checkpoint('foo/a.ipynb') |
|
509 | 509 | self.assertEqual(r.status_code, 201) |
|
510 | 510 | cp1 = r.json() |
|
511 | 511 | self.assertEqual(set(cp1), {'id', 'last_modified'}) |
|
512 | 512 | self.assertEqual(r.headers['Location'].split('/')[-1], cp1['id']) |
|
513 | 513 | |
|
514 | 514 | # Modify it |
|
515 | 515 | nbcontent = json.loads(resp.text)['content'] |
|
516 | 516 | nb = from_dict(nbcontent) |
|
517 | 517 | hcell = new_markdown_cell('Created by test') |
|
518 | 518 | nb.cells.append(hcell) |
|
519 | 519 | # Save |
|
520 | 520 | nbmodel= {'content': nb, 'type': 'notebook'} |
|
521 | 521 | resp = self.api.save('foo/a.ipynb', body=json.dumps(nbmodel)) |
|
522 | 522 | |
|
523 | 523 | # List checkpoints |
|
524 | 524 | cps = self.api.get_checkpoints('foo/a.ipynb').json() |
|
525 | 525 | self.assertEqual(cps, [cp1]) |
|
526 | 526 | |
|
527 | 527 | nbcontent = self.api.read('foo/a.ipynb').json()['content'] |
|
528 | 528 | nb = from_dict(nbcontent) |
|
529 | 529 | self.assertEqual(nb.cells[0].source, 'Created by test') |
|
530 | 530 | |
|
531 | 531 | # Restore cp1 |
|
532 | 532 | r = self.api.restore_checkpoint('foo/a.ipynb', cp1['id']) |
|
533 | 533 | self.assertEqual(r.status_code, 204) |
|
534 | 534 | nbcontent = self.api.read('foo/a.ipynb').json()['content'] |
|
535 | 535 | nb = from_dict(nbcontent) |
|
536 | 536 | self.assertEqual(nb.cells, []) |
|
537 | 537 | |
|
538 | 538 | # Delete cp1 |
|
539 | 539 | r = self.api.delete_checkpoint('foo/a.ipynb', cp1['id']) |
|
540 | 540 | self.assertEqual(r.status_code, 204) |
|
541 | 541 | cps = self.api.get_checkpoints('foo/a.ipynb').json() |
|
542 | 542 | self.assertEqual(cps, []) |
General Comments 0
You need to be logged in to leave comments.
Login now