From 08b75cf8c2eb2b1574c2d8c712af328fc8341ff3 Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Fri, 15 Mar 2019 19:24:54 +0100 Subject: [PATCH] new tests --- tests/app/__init__.py | 0 tests/{ => app}/conftest.py | 12 ++-- .../execute_test.py} | 0 tests/app/nbextensions_test.py | 22 +++++++ tests/app/serve_directory_test.py | 14 +++++ tests/{ => app}/template_custom_test.py | 6 +- tests/{ => app}/template_gridstack_test.py | 0 tests/server/__init__.py | 0 tests/server/conftest.py | 58 +++++++++++++++++++ tests/server/execute_test.py | 20 +++++++ tests/server/nbextensions_test.py | 22 +++++++ 11 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 tests/app/__init__.py rename tests/{ => app}/conftest.py (75%) rename tests/{voila_single_notebook_test.py => app/execute_test.py} (100%) create mode 100644 tests/app/nbextensions_test.py create mode 100644 tests/app/serve_directory_test.py rename tests/{ => app}/template_custom_test.py (75%) rename tests/{ => app}/template_gridstack_test.py (100%) create mode 100644 tests/server/__init__.py create mode 100644 tests/server/conftest.py create mode 100644 tests/server/execute_test.py create mode 100644 tests/server/nbextensions_test.py diff --git a/tests/app/__init__.py b/tests/app/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/conftest.py b/tests/app/conftest.py similarity index 75% rename from tests/conftest.py rename to tests/app/conftest.py index d6c58e63d..b7e557043 100644 --- a/tests/conftest.py +++ b/tests/app/conftest.py @@ -18,8 +18,12 @@ def voila_config(): @pytest.fixture -def voila_notebook(): - return os.path.join(BASE_DIR, 'notebooks/print.ipynb') +def voila_notebook(voila_notebook_dir): + return os.path.join(voila_notebook_dir, 'print.ipynb') + +@pytest.fixture +def voila_notebook_dir(): + return os.path.join(BASE_DIR, '../notebooks') @pytest.fixture @@ -38,8 +42,8 @@ def voila_app(voila_args, voila_config): voila_app.initialize(voila_args) voila_config(voila_app) voila_app.start() - return voila_app - + yield voila_app + voila_app.clear_instance() @pytest.fixture def app(voila_app): diff --git a/tests/voila_single_notebook_test.py b/tests/app/execute_test.py similarity index 100% rename from tests/voila_single_notebook_test.py rename to tests/app/execute_test.py diff --git a/tests/app/nbextensions_test.py b/tests/app/nbextensions_test.py new file mode 100644 index 000000000..ca47c155d --- /dev/null +++ b/tests/app/nbextensions_test.py @@ -0,0 +1,22 @@ +# tests programmatic config of template sytem +import pytest +import os + +BASE_DIR = os.path.dirname(__file__) + +@pytest.fixture +def voila_config(): + def config(app): + pass + os.environ['JUPYTER_CONFIG_DIR'] = os.path.join(BASE_DIR, '../configs/general') + yield config + del os.environ['JUPYTER_CONFIG_DIR'] + + +@pytest.mark.gen_test +def test_lists_extension(http_client, base_url): + response = yield http_client.fetch(base_url) + assert response.code == 200 + html_text = response.body.decode('utf-8') + assert 'Hi Voila' in html_text + assert 'ipytest/extension.js' in html_text diff --git a/tests/app/serve_directory_test.py b/tests/app/serve_directory_test.py new file mode 100644 index 000000000..c1ed6752e --- /dev/null +++ b/tests/app/serve_directory_test.py @@ -0,0 +1,14 @@ +# test serving a notebook +import pytest + +@pytest.fixture +def voila_args(voila_notebook_dir, voila_args_extra): + return ['--VoilaApp.root_dir=%r' % voila_notebook_dir, voila_notebook_dir, '--VoilaApp.log_level=DEBUG'] + voila_args_extra + + +@pytest.mark.gen_test +def test_hello_world(http_client, base_url): + response = yield http_client.fetch(base_url + 'voila/render/print') + assert response.code == 200 + assert 'Hi Voila' in response.body.decode('utf-8') + diff --git a/tests/template_custom_test.py b/tests/app/template_custom_test.py similarity index 75% rename from tests/template_custom_test.py rename to tests/app/template_custom_test.py index 708252cd0..24f27dde9 100644 --- a/tests/template_custom_test.py +++ b/tests/app/template_custom_test.py @@ -13,10 +13,10 @@ def voila_args_extra(): @pytest.fixture def voila_config(): def config(app): - path_gridstack = os.path.abspath(os.path.join(BASE_DIR, '../share/jupyter/voila/template/gridstack/nbconvert_templates')) - path_default = os.path.abspath(os.path.join(BASE_DIR, '../share/jupyter/voila/template/default/nbconvert_templates')) + path_gridstack = os.path.abspath(os.path.join(BASE_DIR, '../../share/jupyter/voila/template/gridstack/nbconvert_templates')) + path_default = os.path.abspath(os.path.join(BASE_DIR, '../../share/jupyter/voila/template/default/nbconvert_templates')) app.nbconvert_template_paths = [path_gridstack, path_default] - path = os.path.abspath(os.path.join(BASE_DIR, '../share/jupyter/voila/template/default/templates')) + path = os.path.abspath(os.path.join(BASE_DIR, '../../share/jupyter/voila/template/default/templates')) app.template_paths = [path] return config diff --git a/tests/template_gridstack_test.py b/tests/app/template_gridstack_test.py similarity index 100% rename from tests/template_gridstack_test.py rename to tests/app/template_gridstack_test.py diff --git a/tests/server/__init__.py b/tests/server/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/server/conftest.py b/tests/server/conftest.py new file mode 100644 index 000000000..1a47f5821 --- /dev/null +++ b/tests/server/conftest.py @@ -0,0 +1,58 @@ +import os + +import pytest +from jupyter_server.serverapp import ServerApp +from tornado import httpserver + + +BASE_DIR = os.path.dirname(__file__) + + +@pytest.fixture +def jupyter_server_config(): + return lambda app: None + + +@pytest.fixture +def jupyter_server_notebook(): + return os.path.join(BASE_DIR, '../notebooks') + + +@pytest.fixture +def jupyter_server_args_extra(): + return [] + + +@pytest.fixture +def print_notebook_url(base_url): + return base_url + "/voila/render/print" + + +@pytest.fixture +def jupyter_server_args(jupyter_server_notebook, jupyter_server_args_extra): + debug_args = ['--ServerApp.log_level=DEBUG'] if os.environ.get('VOILA_TEST_DEBUG', False) else [] + default_args = ['--ServerApp.token=""'] + return [jupyter_server_notebook] + jupyter_server_args_extra + debug_args + default_args + + +@pytest.fixture +def jupyter_server_app(jupyter_server_args, jupyter_server_config): + jupyter_server_app = ServerApp.instance() + # we monkey patch + old_listen = httpserver.HTTPServer.listen + httpserver.HTTPServer.listen = lambda *x, **y: None + # NOTE: in voila's conftest.py we call config after initialize + jupyter_server_config(jupyter_server_app) + jupyter_server_app.initialize(jupyter_server_args) + yield jupyter_server_app + httpserver.HTTPServer.listen = old_listen + ServerApp.clear_instance() + + + + +@pytest.fixture +def app(jupyter_server_app): + return jupyter_server_app.web_app + + diff --git a/tests/server/execute_test.py b/tests/server/execute_test.py new file mode 100644 index 000000000..e94c5bcb4 --- /dev/null +++ b/tests/server/execute_test.py @@ -0,0 +1,20 @@ +# test basics of voila running a notebook +import pytest +import tornado.web +import tornado.gen +import re +import json + +try: + from unittest import mock +except: + import mock + + +@pytest.mark.gen_test +def test_hello_world(http_client, print_notebook_url): + response = yield http_client.fetch(print_notebook_url) + assert response.code == 200 + html_text = response.body.decode('utf-8') + assert 'Hi Voila' in html_text + assert 'gridstack.css' not in html_text, "gridstack should not be the default" diff --git a/tests/server/nbextensions_test.py b/tests/server/nbextensions_test.py new file mode 100644 index 000000000..f471b6efc --- /dev/null +++ b/tests/server/nbextensions_test.py @@ -0,0 +1,22 @@ +# tests programmatic config of template sytem +import pytest +import os + +BASE_DIR = os.path.dirname(__file__) + +@pytest.fixture +def jupyter_server_config(): + def config(app): + pass + os.environ['JUPYTER_CONFIG_DIR'] = os.path.join(BASE_DIR, 'configs', 'general') + yield config + del os.environ['JUPYTER_CONFIG_DIR'] + +@pytest.mark.xfail(reason='needs to be fixed') +@pytest.mark.gen_test +def test_lists_extension(http_client, print_notebook_url): + response = yield http_client.fetch(print_notebook_url) + assert response.code == 200 + html_text = response.body.decode('utf-8') + assert 'Hi Voila' in html_text + assert 'ipytest/extension.js' in html_text