Skip to content

Commit ae0fde3

Browse files
authored
Merge pull request #171 from datalayer-contrib/pytest_fixture_updates
Minor enhancements to the fixtures
2 parents ae6ae6a + 2751ad4 commit ae0fde3

File tree

4 files changed

+53
-54
lines changed

4 files changed

+53
-54
lines changed

jupyter_server/pytest_plugin.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from jupyter_server.extension import serverextension
1616
from jupyter_server.serverapp import ServerApp
1717
from jupyter_server.utils import url_path_join
18+
from jupyter_server.services.contents.filemanager import FileContentsManager
1819

1920
import nbformat
2021

@@ -63,6 +64,11 @@ def mkdir(tmp_path, *parts):
6364
env_config_path = pytest.fixture(
6465
lambda tmp_path: mkdir(tmp_path, "env", "etc", "jupyter")
6566
)
67+
some_resource = u"The very model of a modern major general"
68+
sample_kernel_json = {
69+
'argv':['cat', '{connection_file}'],
70+
'display_name':'Test kernel',
71+
}
6672
argv = pytest.fixture(lambda: [])
6773

6874

@@ -164,6 +170,7 @@ def serverapp(configurable_serverapp, config, argv):
164170

165171
@pytest.fixture
166172
def app(serverapp):
173+
"""app fixture is needed by pytest_tornasync plugin"""
167174
return serverapp.web_app
168175

169176

@@ -178,7 +185,7 @@ def http_port(http_server_port):
178185

179186

180187
@pytest.fixture
181-
def base_url(http_server_port):
188+
def base_url():
182189
return "/"
183190

184191

@@ -199,6 +206,49 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
199206
return client_fetch
200207

201208

209+
@pytest.fixture
210+
def ws_fetch(auth_header, http_port):
211+
"""websocket fetch fixture that handles auth, base_url, and path"""
212+
def client_fetch(*parts, headers={}, params={}, **kwargs):
213+
# Handle URL strings
214+
path = url_escape(url_path_join(*parts), plus=False)
215+
urlparts = urllib.parse.urlparse('ws://localhost:{}'.format(http_port))
216+
urlparts = urlparts._replace(
217+
path=path,
218+
query=urllib.parse.urlencode(params)
219+
)
220+
url = urlparts.geturl()
221+
# Add auth keys to header
222+
headers.update(auth_header)
223+
# Make request.
224+
req = tornado.httpclient.HTTPRequest(
225+
url,
226+
headers=auth_header,
227+
connect_timeout=120
228+
)
229+
return tornado.websocket.websocket_connect(req)
230+
return client_fetch
231+
232+
233+
@pytest.fixture
234+
def kernelspecs(data_dir):
235+
spec_names = ['sample', 'sample 2']
236+
for name in spec_names:
237+
sample_kernel_dir = data_dir.joinpath('kernels', name)
238+
sample_kernel_dir.mkdir(parents=True)
239+
# Create kernel json file
240+
sample_kernel_file = sample_kernel_dir.joinpath('kernel.json')
241+
sample_kernel_file.write_text(json.dumps(sample_kernel_json))
242+
# Create resources text
243+
sample_kernel_resources = sample_kernel_dir.joinpath('resource.txt')
244+
sample_kernel_resources.write_text(some_resource)
245+
246+
247+
@pytest.fixture(params=[True, False])
248+
def contents_manager(request, tmp_path):
249+
return FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
250+
251+
202252
@pytest.fixture
203253
def create_notebook(root_dir):
204254
"""Create a notebook in the test's home directory."""

tests/services/contents/test_manager.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515

1616
# -------------- Functions ----------------------------
1717

18-
# contents_manager_atomic = pytest.fixture(lambda tmp_path: FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=True))
19-
# contents_manager_nonatomic = pytest.fixture(lambda tmp_path: FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=False))
20-
21-
@pytest.fixture(params=[True, False])
22-
def contents_manager(request, tmp_path):
23-
return FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
24-
25-
2618
def _make_dir(contents_manager, api_path):
2719
"""
2820
Make a directory.

tests/services/kernels/test_api.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,6 @@
1313
from ...utils import expected_http_error
1414

1515

16-
@pytest.fixture
17-
def ws_fetch(auth_header, http_port):
18-
"""fetch fixture that handles auth, base_url, and path"""
19-
def client_fetch(*parts, headers={}, params={}, **kwargs):
20-
# Handle URL strings
21-
path = url_escape(url_path_join(*parts), plus=False)
22-
urlparts = urllib.parse.urlparse('ws://localhost:{}'.format(http_port))
23-
urlparts = urlparts._replace(
24-
path=path,
25-
query=urllib.parse.urlencode(params)
26-
)
27-
url = urlparts.geturl()
28-
# Add auth keys to header
29-
headers.update(auth_header)
30-
# Make request.
31-
req = tornado.httpclient.HTTPRequest(
32-
url,
33-
headers=auth_header,
34-
connect_timeout=120
35-
)
36-
return tornado.websocket.websocket_connect(req)
37-
return client_fetch
38-
39-
4016
async def test_no_kernels(fetch):
4117
r = await fetch(
4218
'api', 'kernels',

tests/services/kernelspecs/test_api.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,13 @@
33

44
import tornado
55

6+
from jupyter_server.pytest_plugin import some_resource
7+
68
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
79

810
from ...utils import expected_http_error
911

1012

11-
sample_kernel_json = {
12-
'argv':['cat', '{connection_file}'],
13-
'display_name':'Test kernel',
14-
}
15-
some_resource = u"The very model of a modern major general"
16-
17-
18-
@pytest.fixture
19-
def kernelspecs(data_dir):
20-
spec_names = ['sample', 'sample 2']
21-
for name in spec_names:
22-
sample_kernel_dir = data_dir.joinpath('kernels', name)
23-
sample_kernel_dir.mkdir(parents=True)
24-
# Create kernel json file
25-
sample_kernel_file = sample_kernel_dir.joinpath('kernel.json')
26-
sample_kernel_file.write_text(json.dumps(sample_kernel_json))
27-
# Create resources text
28-
sample_kernel_resources = sample_kernel_dir.joinpath('resource.txt')
29-
sample_kernel_resources.write_text(some_resource)
30-
31-
3213
async def test_list_kernelspecs_bad(fetch, kernelspecs, data_dir):
3314
bad_kernel_dir = data_dir.joinpath(data_dir, 'kernels', 'bad')
3415
bad_kernel_dir.mkdir(parents=True)

0 commit comments

Comments
 (0)