##// END OF EJS Templates
Merge pull request #6026 from takluyver/kernelspec-rest-launching...
Merge pull request #6026 from takluyver/kernelspec-rest-launching Kernelspecs in REST API for kernels and sessions

File last commit:

r17222:c1223d48
r17234:b537531d merge
Show More
test_sessions_api.py
116 lines | 3.7 KiB | text/x-python | PythonLexer
Zachary Sailer
add tests for session api
r13044 """Test the sessions web service API."""
Thomas Kluyver
Make sessions REST API test more robust....
r13401 import errno
Thomas Kluyver
Improve Session REST API tests
r13093 import io
Zachary Sailer
add tests for session api
r13044 import os
import json
import requests
Thomas Kluyver
Improve Session REST API tests
r13093 import shutil
pjoin = os.path.join
Zachary Sailer
add tests for session api
r13044
Zachary Sailer
Code review changes....
r13048 from IPython.html.utils import url_path_join
Thomas Kluyver
Add failing test for listing nonexistant directory
r13099 from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
Thomas Kluyver
Improve Session REST API tests
r13093 from IPython.nbformat.current import new_notebook, write
class SessionAPI(object):
"""Wrapper for notebook API calls."""
def __init__(self, base_url):
self.base_url = base_url
def _req(self, verb, path, body=None):
response = requests.request(verb,
url_path_join(self.base_url, 'api/sessions', path), data=body)
if 400 <= response.status_code < 600:
try:
response.reason = response.json()['message']
except:
pass
response.raise_for_status()
return response
def list(self):
return self._req('GET', '')
def get(self, id):
return self._req('GET', id)
Thomas Kluyver
Add kernel name to sessions REST API...
r17222 def create(self, name, path, kernel_name='python'):
body = json.dumps({'notebook': {'name':name, 'path':path},
'kernel': {'name': kernel_name}})
Thomas Kluyver
Improve Session REST API tests
r13093 return self._req('POST', '', body)
def modify(self, id, name, path):
Thomas Kluyver
Fix session API tests
r13094 body = json.dumps({'notebook': {'name':name, 'path':path}})
Thomas Kluyver
Improve Session REST API tests
r13093 return self._req('PATCH', id, body)
def delete(self, id):
return self._req('DELETE', id)
Zachary Sailer
add tests for session api
r13044
class SessionAPITest(NotebookTestBase):
"""Test the sessions web service API"""
Thomas Kluyver
Improve Session REST API tests
r13093 def setUp(self):
nbdir = self.notebook_dir.name
Thomas Kluyver
Make sessions REST API test more robust....
r13401 try:
os.mkdir(pjoin(nbdir, 'foo'))
except OSError as e:
# Deleting the folder in an earlier test may have failed
if e.errno != errno.EEXIST:
raise
Thomas Kluyver
Improve Session REST API tests
r13093
Thomas Kluyver
Specify encoding in remainining instances of io.open
r13657 with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w',
encoding='utf-8') as f:
Thomas Kluyver
Improve Session REST API tests
r13093 nb = new_notebook(name='nb1')
write(nb, f, format='ipynb')
self.sess_api = SessionAPI(self.base_url())
def tearDown(self):
for session in self.sess_api.list().json():
self.sess_api.delete(session['id'])
Thomas Kluyver
Make sessions REST API test more robust....
r13401 shutil.rmtree(pjoin(self.notebook_dir.name, 'foo'),
ignore_errors=True)
Thomas Kluyver
Improve Session REST API tests
r13093
def test_create(self):
sessions = self.sess_api.list().json()
self.assertEqual(len(sessions), 0)
resp = self.sess_api.create('nb1.ipynb', 'foo')
self.assertEqual(resp.status_code, 201)
newsession = resp.json()
self.assertIn('id', newsession)
self.assertEqual(newsession['notebook']['name'], 'nb1.ipynb')
self.assertEqual(newsession['notebook']['path'], 'foo')
Brian E. Granger
Improving tests and setting of Location header.
r13116 self.assertEqual(resp.headers['Location'], '/api/sessions/{0}'.format(newsession['id']))
Thomas Kluyver
Improve Session REST API tests
r13093
sessions = self.sess_api.list().json()
self.assertEqual(sessions, [newsession])
# Retrieve it
sid = newsession['id']
Thomas Kluyver
Fix session API tests
r13094 got = self.sess_api.get(sid).json()
Thomas Kluyver
Improve Session REST API tests
r13093 self.assertEqual(got, newsession)
def test_delete(self):
newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
sid = newsession['id']
resp = self.sess_api.delete(sid)
self.assertEqual(resp.status_code, 204)
sessions = self.sess_api.list().json()
self.assertEqual(sessions, [])
Thomas Kluyver
Add failing test for listing nonexistant directory
r13099 with assert_http_error(404):
self.sess_api.get(sid)
Thomas Kluyver
Improve Session REST API tests
r13093
def test_modify(self):
newsession = self.sess_api.create('nb1.ipynb', 'foo').json()
sid = newsession['id']
changed = self.sess_api.modify(sid, 'nb2.ipynb', '').json()
self.assertEqual(changed['id'], sid)
Thomas Kluyver
Fix session API tests
r13094 self.assertEqual(changed['notebook']['name'], 'nb2.ipynb')
self.assertEqual(changed['notebook']['path'], '')