Skip to content

Commit

Permalink
Merge pull request #172 from datalayer-contrib/prepare_template
Browse files Browse the repository at this point in the history
Ensure initialize_templates is called by _prepare_templates
  • Loading branch information
Zsailer authored Feb 4, 2020
2 parents 278b89b + 88d022d commit ae6ae6a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 26 deletions.
1 change: 1 addition & 0 deletions jupyter_server/extension/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ def _prepare_templates(self):
self.settings.update({
"{}_template_paths".format(self.extension_name): self.template_paths
})
self.initialize_templates()

@staticmethod
def initialize_server(argv=[], load_other_extensions=True, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions jupyter_server/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def mkdir(tmp_path, *parts):
config_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "config"))
runtime_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "runtime"))
root_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "root_dir"))
template_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "templates"))
system_jupyter_path = pytest.fixture(
lambda tmp_path: mkdir(tmp_path, "share", "jupyter")
)
Expand Down
51 changes: 44 additions & 7 deletions tests/extension/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from jupyter_core import paths
from jupyter_server.extension import serverextension
from jupyter_server.extension.serverextension import _get_config_dir
from jupyter_server.extension.application import ExtensionApp
from jupyter_server.extension.handler import ExtensionHandler
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
from jupyter_server.extension.handler import ExtensionHandler, ExtensionHandlerJinjaMixin

# ----------------- Mock Extension App ----------------------

Expand All @@ -17,14 +17,21 @@ def get(self):
self.finish(self.config.mock_trait)


class MockExtensionApp(ExtensionApp):
class MockExtensionTemplateHandler(ExtensionHandlerJinjaMixin, ExtensionHandler):

def get(self):
self.write(self.render_template("index.html"))


class MockExtensionApp(ExtensionAppJinjaMixin, ExtensionApp):
extension_name = 'mockextension'
mock_trait = Unicode('mock trait', config=True)

loaded = False

def initialize_handlers(self):
self.handlers.append(('/mock', MockExtensionHandler))
self.handlers.append(('/mock_template', MockExtensionTemplateHandler))
self.loaded = True

@staticmethod
Expand All @@ -33,6 +40,36 @@ def _jupyter_server_extension_paths():
'module': '_mockdestination/index'
}]

@pytest.fixture
def make_mock_extension_app(template_dir):
def _make_mock_extension_app(**kwargs):
kwargs.setdefault('template_paths', [str(template_dir)])
return MockExtensionApp(**kwargs)

# TODO Should the index template creation be only be done only once?
index = template_dir.joinpath("index.html")
index.write_text("""
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}Jupyter Server 1{% endblock %}</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block meta %}
{% endblock %}
</head>
<body>
<div id="site">
{% block site %}
{% endblock site %}
</div>
{% block after_site %}
{% endblock after_site %}
</body>
</html>""")
return _make_mock_extension_app


@pytest.fixture
def config_file(config_dir):
Expand All @@ -43,21 +80,21 @@ def config_file(config_dir):


@pytest.fixture
def extended_serverapp(serverapp):
def extended_serverapp(serverapp, make_mock_extension_app):
""""""
m = MockExtensionApp()
m = make_mock_extension_app()
m.initialize(serverapp)
return m


@pytest.fixture
def inject_mock_extension(environ, extension_environ):
def inject_mock_extension(environ, extension_environ, make_mock_extension_app):
"""Fixture that can be used to inject a mock Jupyter Server extension into the tests namespace.
Usage: inject_mock_extension({'extension_name': ExtensionClass})
"""
def ext(modulename="mockextension"):
sys.modules[modulename] = e = MockExtensionApp()
sys.modules[modulename] = e = make_mock_extension_app()
return e

return ext
22 changes: 10 additions & 12 deletions tests/extension/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
from jupyter_server.serverapp import ServerApp
from jupyter_server.extension.application import ExtensionApp

from .conftest import MockExtensionApp


def test_instance_creation():
mock_extension = MockExtensionApp()
def test_instance_creation(make_mock_extension_app, template_dir):
mock_extension = make_mock_extension_app()
assert mock_extension.static_paths == []
assert mock_extension.template_paths == []
assert mock_extension.template_paths == [str(template_dir)]
assert mock_extension.settings == {}
assert mock_extension.handlers == []
assert mock_extension.handlers == []


def test_initialize(serverapp):
mock_extension = MockExtensionApp()
def test_initialize(serverapp, make_mock_extension_app):
mock_extension = make_mock_extension_app()
mock_extension.initialize(serverapp)
# Check that settings and handlers were added to the mock extension.
assert isinstance(mock_extension.serverapp, ServerApp)
Expand All @@ -35,24 +33,24 @@ def test_initialize(serverapp):
'trait_name,trait_value',
traits
)
def test_instance_creation_with_instance_args(trait_name, trait_value):
def test_instance_creation_with_instance_args(trait_name, trait_value, make_mock_extension_app):
kwarg = {}
kwarg.setdefault(trait_name, trait_value)
mock_extension = MockExtensionApp(**kwarg)
mock_extension = make_mock_extension_app(**kwarg)
assert getattr(mock_extension, trait_name) == trait_value


@pytest.mark.parametrize(
'trait_name,trait_value',
traits
)
def test_instance_creation_with_argv(serverapp, trait_name, trait_value):
def test_instance_creation_with_argv(serverapp, trait_name, trait_value, make_mock_extension_app):
kwarg = {}
kwarg.setdefault(trait_name, trait_value)
argv = [
'--MockExtensionApp.{name}={value}'.format(name=trait_name, value=trait_value)
]
mock_extension = MockExtensionApp()
mock_extension = make_mock_extension_app()
mock_extension.initialize(serverapp, argv=argv)
assert getattr(mock_extension, trait_name) == trait_value

Expand Down
2 changes: 0 additions & 2 deletions tests/extension/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from jupyter_core import paths
from jupyter_server.extension import serverextension

from .conftest import MockExtensionApp

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.script_launch_mode('subprocess')

Expand Down
17 changes: 12 additions & 5 deletions tests/extension/test_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from jupyter_server.serverapp import ServerApp
from .conftest import MockExtensionApp

# ------------------ Start tests -------------------

Expand All @@ -14,9 +13,17 @@ async def test_handler(fetch, extended_serverapp):
assert r.body.decode() == 'mock trait'


async def test_handler_setting(fetch, serverapp):
async def test_handler_template(fetch, extended_serverapp):
r = await fetch(
'mock_template',
method='GET'
)
assert r.code == 200


async def test_handler_setting(fetch, serverapp, make_mock_extension_app):
# Configure trait in Mock Extension.
m = MockExtensionApp(mock_trait='test mock trait')
m = make_mock_extension_app(mock_trait='test mock trait')
m.initialize(serverapp)

# Test that the extension trait was picked up by the webapp.
Expand All @@ -28,9 +35,9 @@ async def test_handler_setting(fetch, serverapp):
assert r.body.decode() == 'test mock trait'


async def test_handler_argv(fetch, serverapp):
async def test_handler_argv(fetch, serverapp, make_mock_extension_app):
# Configure trait in Mock Extension.
m = MockExtensionApp()
m = make_mock_extension_app()
argv = ['--MockExtensionApp.mock_trait="test mock trait"']
m.initialize(serverapp, argv=argv)

Expand Down

0 comments on commit ae6ae6a

Please sign in to comment.