Show More
@@ -13,7 +13,7 pjoin = os.path.join | |||||
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 |
|
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 | |
@@ -119,13 +119,9 class APITest(NotebookTestBase): | |||||
119 | expected = { normalize('NFC', name) for name in expected } |
|
119 | expected = { normalize('NFC', name) for name in expected } | |
120 | self.assertEqual(nbnames, expected) |
|
120 | self.assertEqual(nbnames, expected) | |
121 |
|
121 | |||
122 | def assert_404(self, name, path): |
|
122 | def test_list_nonexistant_dir(self): | |
123 | try: |
|
123 | with assert_http_error(404): | |
124 |
self.nb_api. |
|
124 | self.nb_api.list('nonexistant') | |
125 | except requests.HTTPError as e: |
|
|||
126 | self.assertEqual(e.response.status_code, 404) |
|
|||
127 | else: |
|
|||
128 | assert False, "Reading a non-existent notebook should fail" |
|
|||
129 |
|
125 | |||
130 | def test_get_contents(self): |
|
126 | def test_get_contents(self): | |
131 | for d, name in self.dirs_nbs: |
|
127 | for d, name in self.dirs_nbs: | |
@@ -136,7 +132,8 class APITest(NotebookTestBase): | |||||
136 | self.assertIsInstance(nb['content']['metadata'], dict) |
|
132 | self.assertIsInstance(nb['content']['metadata'], dict) | |
137 |
|
133 | |||
138 | # Name that doesn't exist - should be a 404 |
|
134 | # Name that doesn't exist - should be a 404 | |
139 | self.assert_404('q.ipynb', 'foo') |
|
135 | with assert_http_error(404): | |
|
136 | self.nb_api.read('q.ipynb', 'foo') | |||
140 |
|
137 | |||
141 | def _check_nb_created(self, resp, name, path): |
|
138 | def _check_nb_created(self, resp, name, path): | |
142 | self.assertEqual(resp.status_code, 201) |
|
139 | self.assertEqual(resp.status_code, 201) | |
@@ -212,4 +209,5 class APITest(NotebookTestBase): | |||||
212 | self.assertEqual(saved['path'], 'foo/bar') |
|
209 | self.assertEqual(saved['path'], 'foo/bar') | |
213 | assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb')) |
|
210 | assert os.path.isfile(pjoin(self.notebook_dir.name,'foo','bar','a2.ipynb')) | |
214 | assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')) |
|
211 | assert not os.path.isfile(pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')) | |
215 | self.assert_404('a.ipynb', 'foo') No newline at end of file |
|
212 | with assert_http_error(404): | |
|
213 | self.nb_api.read('a.ipynb', 'foo') No newline at end of file |
@@ -9,7 +9,7 import shutil | |||||
9 | pjoin = os.path.join |
|
9 | pjoin = os.path.join | |
10 |
|
10 | |||
11 | from IPython.html.utils import url_path_join |
|
11 | from IPython.html.utils import url_path_join | |
12 | from IPython.html.tests.launchnotebook import NotebookTestBase |
|
12 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |
13 | from IPython.nbformat.current import new_notebook, write |
|
13 | from IPython.nbformat.current import new_notebook, write | |
14 |
|
14 | |||
15 | class SessionAPI(object): |
|
15 | class SessionAPI(object): | |
@@ -64,14 +64,6 class SessionAPITest(NotebookTestBase): | |||||
64 | self.sess_api.delete(session['id']) |
|
64 | self.sess_api.delete(session['id']) | |
65 | shutil.rmtree(pjoin(self.notebook_dir.name, 'foo')) |
|
65 | shutil.rmtree(pjoin(self.notebook_dir.name, 'foo')) | |
66 |
|
66 | |||
67 | def assert_404(self, id): |
|
|||
68 | try: |
|
|||
69 | self.sess_api.get(id) |
|
|||
70 | except requests.HTTPError as e: |
|
|||
71 | self.assertEqual(e.response.status_code, 404) |
|
|||
72 | else: |
|
|||
73 | assert False, "Getting nonexistent session didn't give HTTP error" |
|
|||
74 |
|
||||
75 | def test_create(self): |
|
67 | def test_create(self): | |
76 | sessions = self.sess_api.list().json() |
|
68 | sessions = self.sess_api.list().json() | |
77 | self.assertEqual(len(sessions), 0) |
|
69 | self.assertEqual(len(sessions), 0) | |
@@ -101,7 +93,8 class SessionAPITest(NotebookTestBase): | |||||
101 | sessions = self.sess_api.list().json() |
|
93 | sessions = self.sess_api.list().json() | |
102 | self.assertEqual(sessions, []) |
|
94 | self.assertEqual(sessions, []) | |
103 |
|
95 | |||
104 | self.assert_404(sid) |
|
96 | with assert_http_error(404): | |
|
97 | self.sess_api.get(sid) | |||
105 |
|
98 | |||
106 | def test_modify(self): |
|
99 | def test_modify(self): | |
107 | newsession = self.sess_api.create('nb1.ipynb', 'foo').json() |
|
100 | newsession = self.sess_api.create('nb1.ipynb', 'foo').json() |
@@ -3,12 +3,12 | |||||
3 | import sys |
|
3 | import sys | |
4 | import time |
|
4 | import time | |
5 | import requests |
|
5 | import requests | |
|
6 | from contextlib import contextmanager | |||
6 | from subprocess import Popen, PIPE |
|
7 | from subprocess import Popen, PIPE | |
7 | from unittest import TestCase |
|
8 | from unittest import TestCase | |
8 |
|
9 | |||
9 | from IPython.utils.tempdir import TemporaryDirectory |
|
10 | from IPython.utils.tempdir import TemporaryDirectory | |
10 |
|
11 | |||
11 |
|
||||
12 | class NotebookTestBase(TestCase): |
|
12 | class NotebookTestBase(TestCase): | |
13 | """A base class for tests that need a running notebook. |
|
13 | """A base class for tests that need a running notebook. | |
14 |
|
14 | |||
@@ -67,3 +67,15 class NotebookTestBase(TestCase): | |||||
67 | @classmethod |
|
67 | @classmethod | |
68 | def base_url(cls): |
|
68 | def base_url(cls): | |
69 | return 'http://localhost:%i/' % cls.port |
|
69 | return 'http://localhost:%i/' % cls.port | |
|
70 | ||||
|
71 | ||||
|
72 | @contextmanager | |||
|
73 | def assert_http_error(status): | |||
|
74 | try: | |||
|
75 | yield | |||
|
76 | except requests.HTTPError as e: | |||
|
77 | real_status = e.response.status_code | |||
|
78 | assert real_status == status, \ | |||
|
79 | "Expected status %d, got %d" % (real_status, status) | |||
|
80 | else: | |||
|
81 | assert False, "Expected HTTP error status" No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now