test_contents_api.py
481 lines
| 18.1 KiB
| text/x-python
|
PythonLexer
MinRK
|
r13091 | # coding: utf-8 | ||
MinRK
|
r17524 | """Test the contents webservice API.""" | ||
Zachary Sailer
|
r13041 | |||
MinRK
|
r17525 | import base64 | ||
Thomas Kluyver
|
r13083 | import io | ||
MinRK
|
r13137 | import json | ||
Zachary Sailer
|
r13041 | import os | ||
Thomas Kluyver
|
r13083 | import shutil | ||
MinRK
|
r13091 | from unicodedata import normalize | ||
Thomas Kluyver
|
r13083 | pjoin = os.path.join | ||
Zachary Sailer
|
r13041 | import requests | ||
MinRK
|
r13132 | from IPython.html.utils import url_path_join, url_escape | ||
Thomas Kluyver
|
r13099 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | ||
MinRK
|
r18607 | from IPython.nbformat import read, write, from_dict | ||
from IPython.nbformat.v4 import ( | ||||
new_notebook, new_markdown_cell, | ||||
) | ||||
MinRK
|
r13141 | from IPython.nbformat import v2 | ||
MinRK
|
r13130 | from IPython.utils import py3compat | ||
Thomas Kluyver
|
r13083 | from IPython.utils.data import uniq_stable | ||
MinRK
|
r13130 | |||
MinRK
|
r17525 | def notebooks_only(dir_model): | ||
return [nb for nb in dir_model['content'] if nb['type']=='notebook'] | ||||
Brian E. Granger
|
r15079 | |||
MinRK
|
r17525 | def dirs_only(dir_model): | ||
return [x for x in dir_model['content'] if x['type']=='directory'] | ||||
Thomas Kluyver
|
r15522 | |||
Brian E. Granger
|
r15079 | |||
MinRK
|
r17524 | class API(object): | ||
"""Wrapper for contents API calls.""" | ||||
Thomas Kluyver
|
r13083 | def __init__(self, base_url): | ||
self.base_url = base_url | ||||
MinRK
|
r13137 | def _req(self, verb, path, body=None): | ||
Thomas Kluyver
|
r13083 | response = requests.request(verb, | ||
MinRK
|
r17524 | url_path_join(self.base_url, 'api/contents', path), | ||
MinRK
|
r13130 | data=body, | ||
) | ||||
Thomas Kluyver
|
r13083 | response.raise_for_status() | ||
return response | ||||
def list(self, path='/'): | ||||
return self._req('GET', path) | ||||
def read(self, name, path='/'): | ||||
return self._req('GET', url_path_join(path, name)) | ||||
MinRK
|
r17527 | def create_untitled(self, path='/', ext=None): | ||
body = None | ||||
if ext: | ||||
body = json.dumps({'ext': ext}) | ||||
return self._req('POST', path, body) | ||||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r13130 | def upload_untitled(self, body, path='/'): | ||
return self._req('POST', path, body) | ||||
def copy_untitled(self, copy_from, path='/'): | ||||
MinRK
|
r13137 | body = json.dumps({'copy_from':copy_from}) | ||
return self._req('POST', path, body) | ||||
MinRK
|
r13130 | |||
def create(self, name, path='/'): | ||||
return self._req('PUT', url_path_join(path, name)) | ||||
Thomas Kluyver
|
r13084 | def upload(self, name, body, path='/'): | ||
MinRK
|
r13130 | return self._req('PUT', url_path_join(path, name), body) | ||
Thomas Kluyver
|
r13084 | |||
MinRK
|
r17530 | def mkdir(self, name, path='/'): | ||
return self._req('PUT', url_path_join(path, name), json.dumps({'type': 'directory'})) | ||||
MinRK
|
r13130 | def copy(self, copy_from, copy_to, path='/'): | ||
MinRK
|
r13137 | body = json.dumps({'copy_from':copy_from}) | ||
return self._req('PUT', url_path_join(path, copy_to), body) | ||||
Thomas Kluyver
|
r13086 | |||
Thomas Kluyver
|
r13085 | def save(self, name, body, path='/'): | ||
return self._req('PUT', url_path_join(path, name), body) | ||||
Thomas Kluyver
|
r13083 | def delete(self, name, path='/'): | ||
return self._req('DELETE', url_path_join(path, name)) | ||||
def rename(self, name, path, new_name): | ||||
MinRK
|
r13138 | body = json.dumps({'name': new_name}) | ||
Thomas Kluyver
|
r13083 | return self._req('PATCH', url_path_join(path, name), body) | ||
Zachary Sailer
|
r13041 | |||
Thomas Kluyver
|
r13109 | def get_checkpoints(self, name, path): | ||
return self._req('GET', url_path_join(path, name, 'checkpoints')) | ||||
def new_checkpoint(self, name, path): | ||||
return self._req('POST', url_path_join(path, name, 'checkpoints')) | ||||
def restore_checkpoint(self, name, path, checkpoint_id): | ||||
return self._req('POST', url_path_join(path, name, 'checkpoints', checkpoint_id)) | ||||
def delete_checkpoint(self, name, path, checkpoint_id): | ||||
return self._req('DELETE', url_path_join(path, name, 'checkpoints', checkpoint_id)) | ||||
Zachary Sailer
|
r13041 | class APITest(NotebookTestBase): | ||
"""Test the kernels web service API""" | ||||
Thomas Kluyver
|
r13083 | dirs_nbs = [('', 'inroot'), | ||
('Directory with spaces in', 'inspace'), | ||||
(u'unicodé', 'innonascii'), | ||||
('foo', 'a'), | ||||
('foo', 'b'), | ||||
('foo', 'name with spaces'), | ||||
('foo', u'unicodé'), | ||||
('foo/bar', 'baz'), | ||||
Thomas Kluyver
|
r15523 | ('ordering', 'A'), | ||
('ordering', 'b'), | ||||
('ordering', 'C'), | ||||
(u'å b', u'ç d'), | ||||
Thomas Kluyver
|
r13083 | ] | ||
Thomas Kluyver
|
r15522 | hidden_dirs = ['.hidden', '__pycache__'] | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r15623 | dirs = uniq_stable([py3compat.cast_unicode(d) for (d,n) in dirs_nbs]) | ||
Thomas Kluyver
|
r13083 | del dirs[0] # remove '' | ||
MinRK
|
r15623 | top_level_dirs = {normalize('NFC', d.split('/')[0]) for d in dirs} | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r17525 | @staticmethod | ||
def _blob_for_name(name): | ||||
return name.encode('utf-8') + b'\xFF' | ||||
@staticmethod | ||||
def _txt_for_name(name): | ||||
return u'%s text file' % name | ||||
Thomas Kluyver
|
r13083 | def setUp(self): | ||
nbdir = self.notebook_dir.name | ||||
MinRK
|
r17525 | self.blob = os.urandom(100) | ||
self.b64_blob = base64.encodestring(self.blob).decode('ascii') | ||||
Thomas Kluyver
|
r13084 | |||
Thomas Kluyver
|
r15522 | for d in (self.dirs + self.hidden_dirs): | ||
MinRK
|
r13182 | d.replace('/', os.sep) | ||
MinRK
|
r13130 | if not os.path.isdir(pjoin(nbdir, d)): | ||
os.mkdir(pjoin(nbdir, d)) | ||||
Thomas Kluyver
|
r13083 | |||
for d, name in self.dirs_nbs: | ||||
MinRK
|
r13182 | d = d.replace('/', os.sep) | ||
MinRK
|
r17525 | # create a notebook | ||
Thomas Kluyver
|
r13657 | with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w', | ||
encoding='utf-8') as f: | ||||
MinRK
|
r18584 | nb = new_notebook() | ||
Min RK
|
r18613 | write(nb, f, version=4) | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r17525 | # create a text file | ||
with io.open(pjoin(nbdir, d, '%s.txt' % name), 'w', | ||||
encoding='utf-8') as f: | ||||
f.write(self._txt_for_name(name)) | ||||
# create a binary file | ||||
with io.open(pjoin(nbdir, d, '%s.blob' % name), 'wb') as f: | ||||
f.write(self._blob_for_name(name)) | ||||
MinRK
|
r17524 | self.api = API(self.base_url()) | ||
Thomas Kluyver
|
r13083 | |||
def tearDown(self): | ||||
nbdir = self.notebook_dir.name | ||||
Thomas Kluyver
|
r15522 | for dname in (list(self.top_level_dirs) + self.hidden_dirs): | ||
Thomas Kluyver
|
r13083 | shutil.rmtree(pjoin(nbdir, dname), ignore_errors=True) | ||
if os.path.isfile(pjoin(nbdir, 'inroot.ipynb')): | ||||
os.unlink(pjoin(nbdir, 'inroot.ipynb')) | ||||
def test_list_notebooks(self): | ||||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list().json()) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(len(nbs), 1) | ||
self.assertEqual(nbs[0]['name'], 'inroot.ipynb') | ||||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list('/Directory with spaces in/').json()) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(len(nbs), 1) | ||
self.assertEqual(nbs[0]['name'], 'inspace.ipynb') | ||||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list(u'/unicodé/').json()) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(len(nbs), 1) | ||
self.assertEqual(nbs[0]['name'], 'innonascii.ipynb') | ||||
MinRK
|
r13130 | self.assertEqual(nbs[0]['path'], u'unicodé') | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list('/foo/bar/').json()) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(len(nbs), 1) | ||
self.assertEqual(nbs[0]['name'], 'baz.ipynb') | ||||
MinRK
|
r13130 | self.assertEqual(nbs[0]['path'], 'foo/bar') | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list('foo').json()) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(len(nbs), 4) | ||
MinRK
|
r13091 | nbnames = { normalize('NFC', n['name']) for n in nbs } | ||
expected = [ u'a.ipynb', u'b.ipynb', u'name with spaces.ipynb', u'unicodé.ipynb'] | ||||
expected = { normalize('NFC', name) for name in expected } | ||||
self.assertEqual(nbnames, expected) | ||||
MinRK
|
r17523 | |||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list('ordering').json()) | ||
Thomas Kluyver
|
r15523 | nbnames = [n['name'] for n in nbs] | ||
expected = ['A.ipynb', 'b.ipynb', 'C.ipynb'] | ||||
self.assertEqual(nbnames, expected) | ||||
Thomas Kluyver
|
r13083 | |||
Thomas Kluyver
|
r15522 | def test_list_dirs(self): | ||
MinRK
|
r17524 | dirs = dirs_only(self.api.list().json()) | ||
MinRK
|
r15623 | dir_names = {normalize('NFC', d['name']) for d in dirs} | ||
Thomas Kluyver
|
r15522 | self.assertEqual(dir_names, self.top_level_dirs) # Excluding hidden dirs | ||
Thomas Kluyver
|
r13099 | def test_list_nonexistant_dir(self): | ||
with assert_http_error(404): | ||||
MinRK
|
r17524 | self.api.list('nonexistant') | ||
Thomas Kluyver
|
r13087 | |||
MinRK
|
r17525 | def test_get_nb_contents(self): | ||
Thomas Kluyver
|
r13083 | for d, name in self.dirs_nbs: | ||
MinRK
|
r17524 | nb = self.api.read('%s.ipynb' % name, d+'/').json() | ||
MinRK
|
r13130 | self.assertEqual(nb['name'], u'%s.ipynb' % name) | ||
MinRK
|
r17525 | self.assertEqual(nb['type'], 'notebook') | ||
self.assertIn('content', nb) | ||||
self.assertEqual(nb['format'], 'json') | ||||
Thomas Kluyver
|
r13083 | self.assertIn('content', nb) | ||
self.assertIn('metadata', nb['content']) | ||||
self.assertIsInstance(nb['content']['metadata'], dict) | ||||
MinRK
|
r17525 | def test_get_contents_no_such_file(self): | ||
Thomas Kluyver
|
r13083 | # Name that doesn't exist - should be a 404 | ||
Thomas Kluyver
|
r13099 | with assert_http_error(404): | ||
MinRK
|
r17524 | self.api.read('q.ipynb', 'foo') | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r17525 | def test_get_text_file_contents(self): | ||
for d, name in self.dirs_nbs: | ||||
model = self.api.read(u'%s.txt' % name, d+'/').json() | ||||
self.assertEqual(model['name'], u'%s.txt' % name) | ||||
self.assertIn('content', model) | ||||
self.assertEqual(model['format'], 'text') | ||||
self.assertEqual(model['type'], 'file') | ||||
self.assertEqual(model['content'], self._txt_for_name(name)) | ||||
# Name that doesn't exist - should be a 404 | ||||
with assert_http_error(404): | ||||
self.api.read('q.txt', 'foo') | ||||
def test_get_binary_file_contents(self): | ||||
for d, name in self.dirs_nbs: | ||||
model = self.api.read(u'%s.blob' % name, d+'/').json() | ||||
self.assertEqual(model['name'], u'%s.blob' % name) | ||||
self.assertIn('content', model) | ||||
self.assertEqual(model['format'], 'base64') | ||||
self.assertEqual(model['type'], 'file') | ||||
b64_data = base64.encodestring(self._blob_for_name(name)).decode('ascii') | ||||
self.assertEqual(model['content'], b64_data) | ||||
# Name that doesn't exist - should be a 404 | ||||
with assert_http_error(404): | ||||
self.api.read('q.txt', 'foo') | ||||
MinRK
|
r17528 | def _check_created(self, resp, name, path, type='notebook'): | ||
Thomas Kluyver
|
r13084 | self.assertEqual(resp.status_code, 201) | ||
MinRK
|
r13130 | location_header = py3compat.str_to_unicode(resp.headers['Location']) | ||
MinRK
|
r17524 | self.assertEqual(location_header, url_escape(url_path_join(u'/api/contents', path, name))) | ||
MinRK
|
r17528 | rjson = resp.json() | ||
self.assertEqual(rjson['name'], name) | ||||
self.assertEqual(rjson['path'], path) | ||||
self.assertEqual(rjson['type'], type) | ||||
isright = os.path.isdir if type == 'directory' else os.path.isfile | ||||
assert isright(pjoin( | ||||
MinRK
|
r13178 | self.notebook_dir.name, | ||
MinRK
|
r13182 | path.replace('/', os.sep), | ||
MinRK
|
r13178 | name, | ||
)) | ||||
Thomas Kluyver
|
r13083 | |||
def test_create_untitled(self): | ||||
MinRK
|
r17524 | resp = self.api.create_untitled(path=u'Ã¥ b') | ||
MinRK
|
r17528 | self._check_created(resp, 'Untitled0.ipynb', u'Ã¥ b') | ||
Thomas Kluyver
|
r13083 | |||
# Second time | ||||
MinRK
|
r17524 | resp = self.api.create_untitled(path=u'Ã¥ b') | ||
MinRK
|
r17528 | self._check_created(resp, 'Untitled1.ipynb', u'Ã¥ b') | ||
Thomas Kluyver
|
r13083 | |||
# And two directories down | ||||
MinRK
|
r17524 | resp = self.api.create_untitled(path='foo/bar') | ||
MinRK
|
r17528 | self._check_created(resp, 'Untitled0.ipynb', 'foo/bar') | ||
Thomas Kluyver
|
r13083 | |||
MinRK
|
r17527 | def test_create_untitled_txt(self): | ||
resp = self.api.create_untitled(path='foo/bar', ext='.txt') | ||||
MinRK
|
r17535 | self._check_created(resp, 'untitled0.txt', 'foo/bar', type='file') | ||
MinRK
|
r17527 | |||
MinRK
|
r17535 | resp = self.api.read(path='foo/bar', name='untitled0.txt') | ||
MinRK
|
r17527 | model = resp.json() | ||
self.assertEqual(model['type'], 'file') | ||||
self.assertEqual(model['format'], 'text') | ||||
self.assertEqual(model['content'], '') | ||||
MinRK
|
r13130 | def test_upload_untitled(self): | ||
MinRK
|
r18584 | nb = new_notebook() | ||
MinRK
|
r17527 | nbmodel = {'content': nb, 'type': 'notebook'} | ||
MinRK
|
r17524 | resp = self.api.upload_untitled(path=u'Ã¥ b', | ||
MinRK
|
r13138 | body=json.dumps(nbmodel)) | ||
MinRK
|
r17528 | self._check_created(resp, 'Untitled0.ipynb', u'Ã¥ b') | ||
MinRK
|
r13130 | |||
def test_upload(self): | ||||
MinRK
|
r18584 | nb = new_notebook() | ||
MinRK
|
r17527 | nbmodel = {'content': nb, 'type': 'notebook'} | ||
MinRK
|
r17524 | resp = self.api.upload(u'Upload tést.ipynb', path=u'å b', | ||
MinRK
|
r13138 | body=json.dumps(nbmodel)) | ||
MinRK
|
r17528 | self._check_created(resp, u'Upload tést.ipynb', u'å b') | ||
def test_mkdir(self): | ||||
MinRK
|
r17530 | resp = self.api.mkdir(u'New ∂ir', path=u'å b') | ||
MinRK
|
r17528 | self._check_created(resp, u'New ∂ir', u'å b', type='directory') | ||
MinRK
|
r13130 | |||
MinRK
|
r17537 | def test_mkdir_hidden_400(self): | ||
with assert_http_error(400): | ||||
resp = self.api.mkdir(u'.hidden', path=u'Ã¥ b') | ||||
MinRK
|
r17527 | def test_upload_txt(self): | ||
body = u'ünicode téxt' | ||||
model = { | ||||
'content' : body, | ||||
'format' : 'text', | ||||
'type' : 'file', | ||||
} | ||||
resp = self.api.upload(u'Upload tést.txt', path=u'å b', | ||||
body=json.dumps(model)) | ||||
# check roundtrip | ||||
resp = self.api.read(path=u'å b', name=u'Upload tést.txt') | ||||
model = resp.json() | ||||
self.assertEqual(model['type'], 'file') | ||||
self.assertEqual(model['format'], 'text') | ||||
self.assertEqual(model['content'], body) | ||||
def test_upload_b64(self): | ||||
body = b'\xFFblob' | ||||
b64body = base64.encodestring(body).decode('ascii') | ||||
model = { | ||||
'content' : b64body, | ||||
'format' : 'base64', | ||||
'type' : 'file', | ||||
} | ||||
resp = self.api.upload(u'Upload tést.blob', path=u'å b', | ||||
body=json.dumps(model)) | ||||
# check roundtrip | ||||
resp = self.api.read(path=u'å b', name=u'Upload tést.blob') | ||||
model = resp.json() | ||||
self.assertEqual(model['type'], 'file') | ||||
self.assertEqual(model['format'], 'base64') | ||||
decoded = base64.decodestring(model['content'].encode('ascii')) | ||||
self.assertEqual(decoded, body) | ||||
MinRK
|
r13141 | def test_upload_v2(self): | ||
nb = v2.new_notebook() | ||||
ws = v2.new_worksheet() | ||||
nb.worksheets.append(ws) | ||||
ws.cells.append(v2.new_code_cell(input='print("hi")')) | ||||
MinRK
|
r17527 | nbmodel = {'content': nb, 'type': 'notebook'} | ||
MinRK
|
r17524 | resp = self.api.upload(u'Upload tést.ipynb', path=u'å b', | ||
MinRK
|
r13141 | body=json.dumps(nbmodel)) | ||
MinRK
|
r17528 | self._check_created(resp, u'Upload tést.ipynb', u'å b') | ||
MinRK
|
r17524 | resp = self.api.read(u'Upload tést.ipynb', u'å b') | ||
MinRK
|
r13141 | data = resp.json() | ||
MinRK
|
r18607 | self.assertEqual(data['content']['nbformat'], 4) | ||
MinRK
|
r13141 | |||
MinRK
|
r13130 | def test_copy_untitled(self): | ||
MinRK
|
r17524 | resp = self.api.copy_untitled(u'ç d.ipynb', path=u'å b') | ||
MinRK
|
r17528 | self._check_created(resp, u'ç d-Copy0.ipynb', u'å b') | ||
Thomas Kluyver
|
r13084 | |||
Thomas Kluyver
|
r13086 | def test_copy(self): | ||
MinRK
|
r17524 | resp = self.api.copy(u'ç d.ipynb', u'cøpy.ipynb', path=u'å b') | ||
MinRK
|
r17528 | self._check_created(resp, u'cøpy.ipynb', u'å b') | ||
Thomas Kluyver
|
r13086 | |||
MinRK
|
r17535 | def test_copy_path(self): | ||
resp = self.api.copy(u'foo/a.ipynb', u'cøpyfoo.ipynb', path=u'å b') | ||||
self._check_created(resp, u'cøpyfoo.ipynb', u'å b') | ||||
MinRK
|
r17530 | def test_copy_dir_400(self): | ||
# can't copy directories | ||||
with assert_http_error(400): | ||||
resp = self.api.copy(u'Ã¥ b', u'Ã¥ c') | ||||
Thomas Kluyver
|
r13083 | def test_delete(self): | ||
for d, name in self.dirs_nbs: | ||||
MinRK
|
r17524 | resp = self.api.delete('%s.ipynb' % name, d) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(resp.status_code, 204) | ||
for d in self.dirs + ['/']: | ||||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list(d).json()) | ||
Thomas Kluyver
|
r13083 | self.assertEqual(len(nbs), 0) | ||
MinRK
|
r17530 | def test_delete_dirs(self): | ||
# depth-first delete everything, so we don't try to delete empty directories | ||||
for name in sorted(self.dirs + ['/'], key=len, reverse=True): | ||||
listing = self.api.list(name).json()['content'] | ||||
for model in listing: | ||||
self.api.delete(model['name'], model['path']) | ||||
listing = self.api.list('/').json()['content'] | ||||
self.assertEqual(listing, []) | ||||
def test_delete_non_empty_dir(self): | ||||
"""delete non-empty dir raises 400""" | ||||
with assert_http_error(400): | ||||
self.api.delete(u'Ã¥ b') | ||||
Thomas Kluyver
|
r13083 | def test_rename(self): | ||
MinRK
|
r17524 | resp = self.api.rename('a.ipynb', 'foo', 'z.ipynb') | ||
Thomas Kluyver
|
r13089 | self.assertEqual(resp.headers['Location'].split('/')[-1], 'z.ipynb') | ||
Thomas Kluyver
|
r13083 | self.assertEqual(resp.json()['name'], 'z.ipynb') | ||
assert os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'z.ipynb')) | ||||
MinRK
|
r17524 | nbs = notebooks_only(self.api.list('foo').json()) | ||
Thomas Kluyver
|
r13083 | nbnames = set(n['name'] for n in nbs) | ||
self.assertIn('z.ipynb', nbnames) | ||||
self.assertNotIn('a.ipynb', nbnames) | ||||
Thomas Kluyver
|
r13085 | |||
MinRK
|
r13710 | def test_rename_existing(self): | ||
with assert_http_error(409): | ||||
MinRK
|
r17524 | self.api.rename('a.ipynb', 'foo', 'b.ipynb') | ||
MinRK
|
r13710 | |||
Thomas Kluyver
|
r13085 | def test_save(self): | ||
MinRK
|
r17524 | resp = self.api.read('a.ipynb', 'foo') | ||
MinRK
|
r13138 | nbcontent = json.loads(resp.text)['content'] | ||
MinRK
|
r18601 | nb = from_dict(nbcontent) | ||
MinRK
|
r18596 | nb.cells.append(new_markdown_cell(u'Created by test ³')) | ||
Thomas Kluyver
|
r13085 | |||
MinRK
|
r17527 | nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb, 'type': 'notebook'} | ||
MinRK
|
r17524 | resp = self.api.save('a.ipynb', path='foo', body=json.dumps(nbmodel)) | ||
Thomas Kluyver
|
r13085 | |||
nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb') | ||||
Thomas Kluyver
|
r13111 | with io.open(nbfile, 'r', encoding='utf-8') as f: | ||
MinRK
|
r18607 | newnb = read(f, as_version=4) | ||
MinRK
|
r18584 | self.assertEqual(newnb.cells[0].source, | ||
Thomas Kluyver
|
r13111 | u'Created by test ³') | ||
MinRK
|
r17524 | nbcontent = self.api.read('a.ipynb', 'foo').json()['content'] | ||
MinRK
|
r18601 | newnb = from_dict(nbcontent) | ||
MinRK
|
r18584 | self.assertEqual(newnb.cells[0].source, | ||
Thomas Kluyver
|
r13111 | u'Created by test ³') | ||
Thomas Kluyver
|
r13087 | |||
# Save and rename | ||||
MinRK
|
r17527 | nbmodel= {'name': 'a2.ipynb', 'path':'foo/bar', 'content': nb, 'type': 'notebook'} | ||
MinRK
|
r17524 | resp = self.api.save('a.ipynb', path='foo', body=json.dumps(nbmodel)) | ||
Thomas Kluyver
|
r13087 | saved = resp.json() | ||
self.assertEqual(saved['name'], 'a2.ipynb') | ||||
self.assertEqual(saved['path'], 'foo/bar') | ||||
assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb')) | ||||
assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')) | ||||
Thomas Kluyver
|
r13099 | with assert_http_error(404): | ||
MinRK
|
r17524 | self.api.read('a.ipynb', 'foo') | ||
Thomas Kluyver
|
r13109 | |||
def test_checkpoints(self): | ||||
MinRK
|
r17524 | resp = self.api.read('a.ipynb', 'foo') | ||
r = self.api.new_checkpoint('a.ipynb', 'foo') | ||||
Thomas Kluyver
|
r13109 | self.assertEqual(r.status_code, 201) | ||
cp1 = r.json() | ||||
MinRK
|
r13122 | self.assertEqual(set(cp1), {'id', 'last_modified'}) | ||
self.assertEqual(r.headers['Location'].split('/')[-1], cp1['id']) | ||||
Thomas Kluyver
|
r13109 | |||
# Modify it | ||||
MinRK
|
r13138 | nbcontent = json.loads(resp.text)['content'] | ||
MinRK
|
r18601 | nb = from_dict(nbcontent) | ||
MinRK
|
r18596 | hcell = new_markdown_cell('Created by test') | ||
MinRK
|
r18584 | nb.cells.append(hcell) | ||
Thomas Kluyver
|
r13109 | # Save | ||
MinRK
|
r17527 | nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb, 'type': 'notebook'} | ||
MinRK
|
r17524 | resp = self.api.save('a.ipynb', path='foo', body=json.dumps(nbmodel)) | ||
Thomas Kluyver
|
r13109 | |||
# List checkpoints | ||||
MinRK
|
r17524 | cps = self.api.get_checkpoints('a.ipynb', 'foo').json() | ||
Thomas Kluyver
|
r13109 | self.assertEqual(cps, [cp1]) | ||
MinRK
|
r17524 | nbcontent = self.api.read('a.ipynb', 'foo').json()['content'] | ||
MinRK
|
r18601 | nb = from_dict(nbcontent) | ||
MinRK
|
r18584 | self.assertEqual(nb.cells[0].source, 'Created by test') | ||
Thomas Kluyver
|
r13109 | |||
# Restore cp1 | ||||
MinRK
|
r17524 | r = self.api.restore_checkpoint('a.ipynb', 'foo', cp1['id']) | ||
Thomas Kluyver
|
r13109 | self.assertEqual(r.status_code, 204) | ||
MinRK
|
r17524 | nbcontent = self.api.read('a.ipynb', 'foo').json()['content'] | ||
MinRK
|
r18601 | nb = from_dict(nbcontent) | ||
MinRK
|
r18584 | self.assertEqual(nb.cells, []) | ||
Thomas Kluyver
|
r13109 | |||
# Delete cp1 | ||||
MinRK
|
r17524 | r = self.api.delete_checkpoint('a.ipynb', 'foo', cp1['id']) | ||
Thomas Kluyver
|
r13109 | self.assertEqual(r.status_code, 204) | ||
MinRK
|
r17524 | cps = self.api.get_checkpoints('a.ipynb', 'foo').json() | ||
Thomas Kluyver
|
r13109 | self.assertEqual(cps, []) | ||