##// END OF EJS Templates
Test notebook checkpoint APIs
Thomas Kluyver -
Show More
@@ -1,213 +1,261 b''
1 # coding: utf-8
1 # coding: utf-8
2 """Test the notebooks webservice API."""
2 """Test the notebooks webservice API."""
3
3
4 import io
4 import io
5 import os
5 import os
6 import shutil
6 import shutil
7 from unicodedata import normalize
7 from unicodedata import normalize
8
8
9 from zmq.utils import jsonapi
9 from zmq.utils import jsonapi
10
10
11 pjoin = os.path.join
11 pjoin = os.path.join
12
12
13 import requests
13 import requests
14
14
15 from IPython.html.utils import url_path_join
15 from IPython.html.utils import url_path_join
16 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
16 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
17 from IPython.nbformat.current import (new_notebook, write, read, new_worksheet,
17 from IPython.nbformat.current import (new_notebook, write, read, new_worksheet,
18 new_heading_cell, to_notebook_json)
18 new_heading_cell, to_notebook_json)
19 from IPython.utils.data import uniq_stable
19 from IPython.utils.data import uniq_stable
20
20
21 class NBAPI(object):
21 class NBAPI(object):
22 """Wrapper for notebook API calls."""
22 """Wrapper for notebook API calls."""
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)
33 response.raise_for_status()
29 response.raise_for_status()
34 return response
30 return response
35
31
36 def list(self, path='/'):
32 def list(self, path='/'):
37 return self._req('GET', path)
33 return self._req('GET', path)
38
34
39 def read(self, name, path='/'):
35 def read(self, name, path='/'):
40 return self._req('GET', url_path_join(path, name))
36 return self._req('GET', url_path_join(path, name))
41
37
42 def create_untitled(self, path='/'):
38 def create_untitled(self, path='/'):
43 return self._req('POST', path)
39 return self._req('POST', path)
44
40
45 def upload(self, name, body, path='/'):
41 def upload(self, name, body, path='/'):
46 return self._req('POST', url_path_join(path, name), body)
42 return self._req('POST', url_path_join(path, name), body)
47
43
48 def copy(self, name, path='/'):
44 def copy(self, name, path='/'):
49 return self._req('POST', url_path_join(path, name, 'copy'))
45 return self._req('POST', url_path_join(path, name, 'copy'))
50
46
51 def save(self, name, body, path='/'):
47 def save(self, name, body, path='/'):
52 return self._req('PUT', url_path_join(path, name), body)
48 return self._req('PUT', url_path_join(path, name), body)
53
49
54 def delete(self, name, path='/'):
50 def delete(self, name, path='/'):
55 return self._req('DELETE', url_path_join(path, name))
51 return self._req('DELETE', url_path_join(path, name))
56
52
57 def rename(self, name, path, new_name):
53 def rename(self, name, path, new_name):
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'),
64 ('Directory with spaces in', 'inspace'),
72 ('Directory with spaces in', 'inspace'),
65 (u'unicodΓ©', 'innonascii'),
73 (u'unicodΓ©', 'innonascii'),
66 ('foo', 'a'),
74 ('foo', 'a'),
67 ('foo', 'b'),
75 ('foo', 'b'),
68 ('foo', 'name with spaces'),
76 ('foo', 'name with spaces'),
69 ('foo', u'unicodΓ©'),
77 ('foo', u'unicodΓ©'),
70 ('foo/bar', 'baz'),
78 ('foo/bar', 'baz'),
71 ]
79 ]
72
80
73 dirs = uniq_stable([d for (d,n) in dirs_nbs])
81 dirs = uniq_stable([d for (d,n) in dirs_nbs])
74 del dirs[0] # remove ''
82 del dirs[0] # remove ''
75
83
76 def setUp(self):
84 def setUp(self):
77 nbdir = self.notebook_dir.name
85 nbdir = self.notebook_dir.name
78
86
79 for d in self.dirs:
87 for d in self.dirs:
80 os.mkdir(pjoin(nbdir, d))
88 os.mkdir(pjoin(nbdir, d))
81
89
82 for d, name in self.dirs_nbs:
90 for d, name in self.dirs_nbs:
83 with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w') as f:
91 with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w') as f:
84 nb = new_notebook(name=name)
92 nb = new_notebook(name=name)
85 write(nb, f, format='ipynb')
93 write(nb, f, format='ipynb')
86
94
87 self.nb_api = NBAPI(self.base_url())
95 self.nb_api = NBAPI(self.base_url())
88
96
89 def tearDown(self):
97 def tearDown(self):
90 nbdir = self.notebook_dir.name
98 nbdir = self.notebook_dir.name
91
99
92 for dname in ['foo', 'Directory with spaces in', u'unicodΓ©']:
100 for dname in ['foo', 'Directory with spaces in', u'unicodΓ©']:
93 shutil.rmtree(pjoin(nbdir, dname), ignore_errors=True)
101 shutil.rmtree(pjoin(nbdir, dname), ignore_errors=True)
94
102
95 if os.path.isfile(pjoin(nbdir, 'inroot.ipynb')):
103 if os.path.isfile(pjoin(nbdir, 'inroot.ipynb')):
96 os.unlink(pjoin(nbdir, 'inroot.ipynb'))
104 os.unlink(pjoin(nbdir, 'inroot.ipynb'))
97
105
98 def test_list_notebooks(self):
106 def test_list_notebooks(self):
99 nbs = self.nb_api.list().json()
107 nbs = self.nb_api.list().json()
100 self.assertEqual(len(nbs), 1)
108 self.assertEqual(len(nbs), 1)
101 self.assertEqual(nbs[0]['name'], 'inroot.ipynb')
109 self.assertEqual(nbs[0]['name'], 'inroot.ipynb')
102
110
103 nbs = self.nb_api.list('/Directory with spaces in/').json()
111 nbs = self.nb_api.list('/Directory with spaces in/').json()
104 self.assertEqual(len(nbs), 1)
112 self.assertEqual(len(nbs), 1)
105 self.assertEqual(nbs[0]['name'], 'inspace.ipynb')
113 self.assertEqual(nbs[0]['name'], 'inspace.ipynb')
106
114
107 nbs = self.nb_api.list(u'/unicodΓ©/').json()
115 nbs = self.nb_api.list(u'/unicodΓ©/').json()
108 self.assertEqual(len(nbs), 1)
116 self.assertEqual(len(nbs), 1)
109 self.assertEqual(nbs[0]['name'], 'innonascii.ipynb')
117 self.assertEqual(nbs[0]['name'], 'innonascii.ipynb')
110
118
111 nbs = self.nb_api.list('/foo/bar/').json()
119 nbs = self.nb_api.list('/foo/bar/').json()
112 self.assertEqual(len(nbs), 1)
120 self.assertEqual(len(nbs), 1)
113 self.assertEqual(nbs[0]['name'], 'baz.ipynb')
121 self.assertEqual(nbs[0]['name'], 'baz.ipynb')
114
122
115 nbs = self.nb_api.list('foo').json()
123 nbs = self.nb_api.list('foo').json()
116 self.assertEqual(len(nbs), 4)
124 self.assertEqual(len(nbs), 4)
117 nbnames = { normalize('NFC', n['name']) for n in nbs }
125 nbnames = { normalize('NFC', n['name']) for n in nbs }
118 expected = [ u'a.ipynb', u'b.ipynb', u'name with spaces.ipynb', u'unicodΓ©.ipynb']
126 expected = [ u'a.ipynb', u'b.ipynb', u'name with spaces.ipynb', u'unicodΓ©.ipynb']
119 expected = { normalize('NFC', name) for name in expected }
127 expected = { normalize('NFC', name) for name in expected }
120 self.assertEqual(nbnames, expected)
128 self.assertEqual(nbnames, expected)
121
129
122 def test_list_nonexistant_dir(self):
130 def test_list_nonexistant_dir(self):
123 with assert_http_error(404):
131 with assert_http_error(404):
124 self.nb_api.list('nonexistant')
132 self.nb_api.list('nonexistant')
125
133
126 def test_get_contents(self):
134 def test_get_contents(self):
127 for d, name in self.dirs_nbs:
135 for d, name in self.dirs_nbs:
128 nb = self.nb_api.read('%s.ipynb' % name, d+'/').json()
136 nb = self.nb_api.read('%s.ipynb' % name, d+'/').json()
129 self.assertEqual(nb['name'], '%s.ipynb' % name)
137 self.assertEqual(nb['name'], '%s.ipynb' % name)
130 self.assertIn('content', nb)
138 self.assertIn('content', nb)
131 self.assertIn('metadata', nb['content'])
139 self.assertIn('metadata', nb['content'])
132 self.assertIsInstance(nb['content']['metadata'], dict)
140 self.assertIsInstance(nb['content']['metadata'], dict)
133
141
134 # Name that doesn't exist - should be a 404
142 # Name that doesn't exist - should be a 404
135 with assert_http_error(404):
143 with assert_http_error(404):
136 self.nb_api.read('q.ipynb', 'foo')
144 self.nb_api.read('q.ipynb', 'foo')
137
145
138 def _check_nb_created(self, resp, name, path):
146 def _check_nb_created(self, resp, name, path):
139 self.assertEqual(resp.status_code, 201)
147 self.assertEqual(resp.status_code, 201)
140 self.assertEqual(resp.headers['Location'].split('/')[-1], name)
148 self.assertEqual(resp.headers['Location'].split('/')[-1], name)
141 self.assertEqual(resp.json()['name'], name)
149 self.assertEqual(resp.json()['name'], name)
142 assert os.path.isfile(pjoin(self.notebook_dir.name, path, name))
150 assert os.path.isfile(pjoin(self.notebook_dir.name, path, name))
143
151
144 def test_create_untitled(self):
152 def test_create_untitled(self):
145 resp = self.nb_api.create_untitled(path='foo')
153 resp = self.nb_api.create_untitled(path='foo')
146 self._check_nb_created(resp, 'Untitled0.ipynb', 'foo')
154 self._check_nb_created(resp, 'Untitled0.ipynb', 'foo')
147
155
148 # Second time
156 # Second time
149 resp = self.nb_api.create_untitled(path='foo')
157 resp = self.nb_api.create_untitled(path='foo')
150 self._check_nb_created(resp, 'Untitled1.ipynb', 'foo')
158 self._check_nb_created(resp, 'Untitled1.ipynb', 'foo')
151
159
152 # And two directories down
160 # And two directories down
153 resp = self.nb_api.create_untitled(path='foo/bar')
161 resp = self.nb_api.create_untitled(path='foo/bar')
154 self._check_nb_created(resp, 'Untitled0.ipynb', pjoin('foo', 'bar'))
162 self._check_nb_created(resp, 'Untitled0.ipynb', pjoin('foo', 'bar'))
155
163
156 def test_upload(self):
164 def test_upload(self):
157 nb = new_notebook(name='Upload test')
165 nb = new_notebook(name='Upload test')
158 nbmodel = {'content': nb}
166 nbmodel = {'content': nb}
159 resp = self.nb_api.upload('Upload test.ipynb', path='foo',
167 resp = self.nb_api.upload('Upload test.ipynb', path='foo',
160 body=jsonapi.dumps(nbmodel))
168 body=jsonapi.dumps(nbmodel))
161 self._check_nb_created(resp, 'Upload test.ipynb', 'foo')
169 self._check_nb_created(resp, 'Upload test.ipynb', 'foo')
162
170
163 def test_copy(self):
171 def test_copy(self):
164 resp = self.nb_api.copy('a.ipynb', path='foo')
172 resp = self.nb_api.copy('a.ipynb', path='foo')
165 self._check_nb_created(resp, 'a-Copy0.ipynb', 'foo')
173 self._check_nb_created(resp, 'a-Copy0.ipynb', 'foo')
166
174
167 def test_delete(self):
175 def test_delete(self):
168 for d, name in self.dirs_nbs:
176 for d, name in self.dirs_nbs:
169 resp = self.nb_api.delete('%s.ipynb' % name, d)
177 resp = self.nb_api.delete('%s.ipynb' % name, d)
170 self.assertEqual(resp.status_code, 204)
178 self.assertEqual(resp.status_code, 204)
171
179
172 for d in self.dirs + ['/']:
180 for d in self.dirs + ['/']:
173 nbs = self.nb_api.list(d).json()
181 nbs = self.nb_api.list(d).json()
174 self.assertEqual(len(nbs), 0)
182 self.assertEqual(len(nbs), 0)
175
183
176 def test_rename(self):
184 def test_rename(self):
177 resp = self.nb_api.rename('a.ipynb', 'foo', 'z.ipynb')
185 resp = self.nb_api.rename('a.ipynb', 'foo', 'z.ipynb')
178 self.assertEqual(resp.headers['Location'].split('/')[-1], 'z.ipynb')
186 self.assertEqual(resp.headers['Location'].split('/')[-1], 'z.ipynb')
179 self.assertEqual(resp.json()['name'], 'z.ipynb')
187 self.assertEqual(resp.json()['name'], 'z.ipynb')
180 assert os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'z.ipynb'))
188 assert os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'z.ipynb'))
181
189
182 nbs = self.nb_api.list('foo').json()
190 nbs = self.nb_api.list('foo').json()
183 nbnames = set(n['name'] for n in nbs)
191 nbnames = set(n['name'] for n in nbs)
184 self.assertIn('z.ipynb', nbnames)
192 self.assertIn('z.ipynb', nbnames)
185 self.assertNotIn('a.ipynb', nbnames)
193 self.assertNotIn('a.ipynb', nbnames)
186
194
187 def test_save(self):
195 def test_save(self):
188 resp = self.nb_api.read('a.ipynb', 'foo')
196 resp = self.nb_api.read('a.ipynb', 'foo')
189 nbcontent = jsonapi.loads(resp.text)['content']
197 nbcontent = jsonapi.loads(resp.text)['content']
190 nb = to_notebook_json(nbcontent)
198 nb = to_notebook_json(nbcontent)
191 ws = new_worksheet()
199 ws = new_worksheet()
192 nb.worksheets = [ws]
200 nb.worksheets = [ws]
193 ws.cells.append(new_heading_cell('Created by test'))
201 ws.cells.append(new_heading_cell('Created by test'))
194
202
195 nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb}
203 nbmodel= {'name': 'a.ipynb', 'path':'foo', 'content': nb}
196 resp = self.nb_api.save('a.ipynb', path='foo', body=jsonapi.dumps(nbmodel))
204 resp = self.nb_api.save('a.ipynb', path='foo', body=jsonapi.dumps(nbmodel))
197
205
198 nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')
206 nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')
199 with open(nbfile, 'r') as f:
207 with open(nbfile, 'r') as f:
200 newnb = read(f, format='ipynb')
208 newnb = read(f, format='ipynb')
201 self.assertEqual(newnb.worksheets[0].cells[0].source,
209 self.assertEqual(newnb.worksheets[0].cells[0].source,
202 'Created by test')
210 'Created by test')
203
211
204 # Save and rename
212 # Save and rename
205 nbmodel= {'name': 'a2.ipynb', 'path':'foo/bar', 'content': nb}
213 nbmodel= {'name': 'a2.ipynb', 'path':'foo/bar', 'content': nb}
206 resp = self.nb_api.save('a.ipynb', path='foo', body=jsonapi.dumps(nbmodel))
214 resp = self.nb_api.save('a.ipynb', path='foo', body=jsonapi.dumps(nbmodel))
207 saved = resp.json()
215 saved = resp.json()
208 self.assertEqual(saved['name'], 'a2.ipynb')
216 self.assertEqual(saved['name'], 'a2.ipynb')
209 self.assertEqual(saved['path'], 'foo/bar')
217 self.assertEqual(saved['path'], 'foo/bar')
210 assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb'))
218 assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb'))
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') No newline at end of file
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