##// END OF EJS Templates
Test notebook checkpoint APIs
Thomas Kluyver -
Show More
@@ -23,10 +23,6 b' class NBAPI(object):'
23 def __init__(self, base_url):
23 def __init__(self, base_url):
24 self.base_url = base_url
24 self.base_url = base_url
25
25
26 @property
27 def nb_url(self):
28 return url_path_join(self.base_url, 'api/notebooks')
29
30 def _req(self, verb, path, body=None):
26 def _req(self, verb, path, body=None):
31 response = requests.request(verb,
27 response = requests.request(verb,
32 url_path_join(self.base_url, 'api/notebooks', path), data=body)
28 url_path_join(self.base_url, 'api/notebooks', path), data=body)
@@ -58,6 +54,18 b' class NBAPI(object):'
58 body = jsonapi.dumps({'name': new_name})
54 body = jsonapi.dumps({'name': new_name})
59 return self._req('PATCH', url_path_join(path, name), body)
55 return self._req('PATCH', url_path_join(path, name), body)
60
56
57 def get_checkpoints(self, name, path):
58 return self._req('GET', url_path_join(path, name, 'checkpoints'))
59
60 def new_checkpoint(self, name, path):
61 return self._req('POST', url_path_join(path, name, 'checkpoints'))
62
63 def restore_checkpoint(self, name, path, checkpoint_id):
64 return self._req('POST', url_path_join(path, name, 'checkpoints', checkpoint_id))
65
66 def delete_checkpoint(self, name, path, checkpoint_id):
67 return self._req('DELETE', url_path_join(path, name, 'checkpoints', checkpoint_id))
68
61 class APITest(NotebookTestBase):
69 class APITest(NotebookTestBase):
62 """Test the kernels web service API"""
70 """Test the kernels web service API"""
63 dirs_nbs = [('', 'inroot'),
71 dirs_nbs = [('', 'inroot'),
@@ -211,3 +219,43 b' class APITest(NotebookTestBase):'
211 assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb'))
219 assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb'))
212 with assert_http_error(404):
220 with assert_http_error(404):
213 self.nb_api.read('a.ipynb', 'foo')
221 self.nb_api.read('a.ipynb', 'foo')
222
223 def test_checkpoints(self):
224 resp = self.nb_api.read('a.ipynb', 'foo')
225 r = self.nb_api.new_checkpoint('a.ipynb', 'foo')
226 self.assertEqual(r.status_code, 201)
227 cp1 = r.json()
228 self.assertEqual(set(cp1), {'checkpoint_id', 'last_modified'})
229 self.assertEqual(r.headers['Location'].split('/')[-1], cp1['checkpoint_id'])
230
231 # Modify it
232 nbcontent = jsonapi.loads(resp.text)['content']
233 nb = to_notebook_json(nbcontent)
234 ws = new_worksheet()
235 nb.worksheets = [ws]
236 hcell = new_heading_cell('Created by test')
237 ws.cells.append(hcell)
238 # Save
239 nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb}
240 resp = self.nb_api.save('a.ipynb', path='foo', body=jsonapi.dumps(nbmodel))
241
242 # List checkpoints
243 cps = self.nb_api.get_checkpoints('a.ipynb', 'foo').json()
244 self.assertEqual(cps, [cp1])
245
246 nbcontent = self.nb_api.read('a.ipynb', 'foo').json()['content']
247 nb = to_notebook_json(nbcontent)
248 self.assertEqual(nb.worksheets[0].cells[0].source, 'Created by test')
249
250 # Restore cp1
251 r = self.nb_api.restore_checkpoint('a.ipynb', 'foo', cp1['checkpoint_id'])
252 self.assertEqual(r.status_code, 204)
253 nbcontent = self.nb_api.read('a.ipynb', 'foo').json()['content']
254 nb = to_notebook_json(nbcontent)
255 self.assertEqual(nb.worksheets, [])
256
257 # Delete cp1
258 r = self.nb_api.delete_checkpoint('a.ipynb', 'foo', cp1['checkpoint_id'])
259 self.assertEqual(r.status_code, 204)
260 cps = self.nb_api.get_checkpoints('a.ipynb', 'foo').json()
261 self.assertEqual(cps, [])
General Comments 0
You need to be logged in to leave comments. Login now