Show More
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,83 b'' | |||||
|
1 | import io | |||
|
2 | import json | |||
|
3 | import os | |||
|
4 | from os.path import join as pjoin | |||
|
5 | import shutil | |||
|
6 | ||||
|
7 | import requests | |||
|
8 | ||||
|
9 | from IPython.html.utils import url_path_join | |||
|
10 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |||
|
11 | from IPython.nbformat.current import (new_notebook, write, new_worksheet, | |||
|
12 | new_heading_cell, new_code_cell) | |||
|
13 | ||||
|
14 | class NbconvertAPI(object): | |||
|
15 | """Wrapper for nbconvert API calls.""" | |||
|
16 | def __init__(self, base_url): | |||
|
17 | self.base_url = base_url | |||
|
18 | ||||
|
19 | def _req(self, verb, path, body=None): | |||
|
20 | response = requests.request(verb, | |||
|
21 | url_path_join(self.base_url, 'nbconvert', path), | |||
|
22 | data=body, | |||
|
23 | ) | |||
|
24 | response.raise_for_status() | |||
|
25 | return response | |||
|
26 | ||||
|
27 | def from_file(self, format, path, name): | |||
|
28 | return self._req('GET', url_path_join(format, path, name)) | |||
|
29 | ||||
|
30 | def from_post(self, format, nbmodel): | |||
|
31 | body = json.dumps(nbmodel) | |||
|
32 | return self._req('POST', format, body) | |||
|
33 | ||||
|
34 | class APITest(NotebookTestBase): | |||
|
35 | def setUp(self): | |||
|
36 | nbdir = self.notebook_dir.name | |||
|
37 | ||||
|
38 | if not os.path.isdir(pjoin(nbdir, 'foo')): | |||
|
39 | os.mkdir(pjoin(nbdir, 'foo')) | |||
|
40 | ||||
|
41 | nb = new_notebook(name='testnb') | |||
|
42 | ||||
|
43 | ws = new_worksheet() | |||
|
44 | nb.worksheets = [ws] | |||
|
45 | ws.cells.append(new_heading_cell(u'Created by test ³')) | |||
|
46 | ws.cells.append(new_code_cell(input=u'print(2*6)')) | |||
|
47 | ||||
|
48 | with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w', | |||
|
49 | encoding='utf-8') as f: | |||
|
50 | write(nb, f, format='ipynb') | |||
|
51 | ||||
|
52 | self.nbconvert_api = NbconvertAPI(self.base_url()) | |||
|
53 | ||||
|
54 | def tearDown(self): | |||
|
55 | nbdir = self.notebook_dir.name | |||
|
56 | ||||
|
57 | for dname in ['foo']: | |||
|
58 | shutil.rmtree(pjoin(nbdir, dname), ignore_errors=True) | |||
|
59 | ||||
|
60 | def test_from_file(self): | |||
|
61 | r = self.nbconvert_api.from_file('html', 'foo', 'testnb.ipynb') | |||
|
62 | self.assertEqual(r.status_code, 200) | |||
|
63 | self.assertIn(u'Created by test', r.text) | |||
|
64 | self.assertIn(u'print', r.text) | |||
|
65 | ||||
|
66 | r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb') | |||
|
67 | self.assertIn(u'print(2*6)', r.text) | |||
|
68 | ||||
|
69 | def test_from_file_404(self): | |||
|
70 | with assert_http_error(404): | |||
|
71 | self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb') | |||
|
72 | ||||
|
73 | def test_from_post(self): | |||
|
74 | nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb') | |||
|
75 | nbmodel = requests.get(nbmodel_url).json() | |||
|
76 | ||||
|
77 | r = self.nbconvert_api.from_post(format='html', nbmodel=nbmodel) | |||
|
78 | self.assertEqual(r.status_code, 200) | |||
|
79 | self.assertIn(u'Created by test', r.text) | |||
|
80 | self.assertIn(u'print', r.text) | |||
|
81 | ||||
|
82 | r = self.nbconvert_api.from_post(format='python', nbmodel=nbmodel) | |||
|
83 | self.assertIn(u'print(2*6)', r.text) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now