Skip to content

Commit

Permalink
Test for the notebook.
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Apr 2, 2017
1 parent 4ad9e97 commit 10d22ce
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 35 deletions.
12 changes: 7 additions & 5 deletions spyder_notebook/notebookplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,17 @@ def close_client(self, index=None, client=None):
self.tabwidget.removeTab(self.tabwidget.indexOf(client))
self.clients.remove(client)

def save_as(self, filename=None):
def save_as(self, name=None):
"""Save notebook as."""
current_client = self.get_current_client()
current_client.save()
original_path = current_client.get_name()
original_name = osp.basename(original_path)
if not filename:
filename, _selfilter = getsavefilename(self, _("Save notebook"),
original_name, FILES_FILTER)
if not name:
original_name = osp.basename(original_path)
else:
original_name = name
filename, _selfilter = getsavefilename(self, _("Save notebook"),
original_name, FILES_FILTER)
if filename:
nb_contents = nbformat.read(original_path, as_version=4)
nbformat.write(nb_contents, filename)
Expand Down
4 changes: 1 addition & 3 deletions spyder_notebook/tests/test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
"collapsed": true
},
"outputs": [],
"source": [
Expand Down
113 changes: 86 additions & 27 deletions spyder_notebook/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
# Standard library imports
import os
import os.path as osp
import json

# Test library imports
# Third-party library imports
import pytest
import requests
from qtpy.QtWebEngineWidgets import WEBENGINE
from qtpy.QtCore import Qt
from qtpy.QtCore import Qt, QTimer
from qtpy.QtWidgets import QFileDialog, QApplication

# Notebook imports
from notebook.utils import url_path_join

# Local imports
from spyder_notebook.notebookplugin import NotebookPlugin


NOTEBOOK_UP = 5000
INTERACTION_CLICK = 500
INTERACTION_CLICK = 100
LOCATION = osp.realpath(osp.join(os.getcwd(), osp.dirname(__file__)))


Expand All @@ -47,6 +53,51 @@ def get_innerHTML(nbwidget, className):
}})();
""".format(repr(className)))

def manage_save_dialog(qtbot, directory=LOCATION):
"""
Manage the QFileDialog when saving.
You can use this with QTimer to manage the QFileDialog.
Before calling anything that may show a QFileDialog for save call:
QTimer.singleShot(1000, lambda: manage_save_dialog(qtbot))
"""
top_level_widgets = QApplication.topLevelWidgets()
for w in top_level_widgets:
if isinstance(w, QFileDialog):
if directory is not None:
w.setDirectory(directory)
w.accept()

def get_kernel_id(client):
"""Get the kernel id for a client and the sessions url."""
sessions_url = client.add_token(url_path_join(client.server_url,
'api/sessions'))
sessions_req = requests.get(sessions_url).content.decode()
sessions = json.loads(sessions_req)

if os.name == 'nt':
path = client.path.replace('\\', '/')
else:
path = client.path

for session in sessions:
if session['notebook']['path'] == path:
kernel_id = session['kernel']['id']
return (kernel_id, sessions_url)

def is_kernel_up(kernel_id, sessions_url):
"""Determines if the kernel with the id is up."""
sessions_req = requests.get(sessions_url).content.decode()
sessions = json.loads(sessions_req)

kernel = False
for session in sessions:
if kernel_id == session['kernel']['id']:
kernel = True
break

return kernel

@pytest.fixture
def setup_notebook(qtbot):
"""Set up the Notebook plugin."""
Expand All @@ -56,24 +107,44 @@ def setup_notebook(qtbot):
notebook.show()
return notebook

def test_shutdown_notebook_kernel(qtbot):
"""Test that the kernel is shutdown from the server when closing a notebook."""
# Create notebook
notebook = setup_notebook(qtbot)

# Wait for prompt
nbwidget = notebook.get_current_nbwidget()
qtbot.waitUntil(lambda: prompt_present(nbwidget), timeout=NOTEBOOK_UP)

# Get kernel id for the client
kernel_id, sessions_url = get_kernel_id(notebook.get_current_client())

# Close the current client
notebook.close_client()

# Assert that the kernel is down for the closed client
assert not is_kernel_up(kernel_id, sessions_url)

def test_open_notebook(qtbot):
"""Test that a netbook can be opened from a non-ascii directory."""
"""Test that a notebook can be opened from a non-ascii directory."""
# Move the test file to non-ascii directory
test_notebook = osp.join(LOCATION, 'test.ipynb')
test_notebook_ascii = osp.join(LOCATION, 'á', 'test.ipynb')
os.mkdir(os.path.join(LOCATION, 'á'))
os.rename(test_notebook, test_notebook_ascii)
test_notebook_non_ascii = osp.join(LOCATION, 'äöüß', 'test.ipynb')
os.mkdir(os.path.join(LOCATION, 'äöüß'))
os.rename(test_notebook, test_notebook_non_ascii)

# Create notebook
notebook = setup_notebook(qtbot)

# Wait for prompt
notebook.open_notebook(filenames=[test_notebook_ascii])
notebook.open_notebook(filenames=[test_notebook_non_ascii])
nbwidget = notebook.get_current_nbwidget()
qtbot.waitUntil(lambda: prompt_present(nbwidget), timeout=NOTEBOOK_UP)

# Assert that the In prompt has "Test" in it
# and the client has the correct name
assert "Test" in get_innerHTML(nbwidget, "CodeMirror-line")
assert notebook.get_current_client().get_short_name() == "test.ipynb"

def test_save_notebook(qtbot):
"""Test that a notebook can be saved."""
Expand All @@ -84,7 +155,7 @@ def test_save_notebook(qtbot):
nbwidget = notebook.get_current_nbwidget()
qtbot.waitUntil(lambda: prompt_present(nbwidget), timeout=NOTEBOOK_UP)

# writes: a = "test"
# Writes: a = "test"
qtbot.keyClick(nbwidget, Qt.Key_A, delay=INTERACTION_CLICK)
qtbot.keyClick(nbwidget, Qt.Key_Space, delay=INTERACTION_CLICK)
qtbot.keyClick(nbwidget, Qt.Key_Equal, delay=INTERACTION_CLICK)
Expand All @@ -97,30 +168,18 @@ def test_save_notebook(qtbot):
qtbot.keyClick(nbwidget, Qt.Key_QuoteDbl, delay=INTERACTION_CLICK)

# Save the notebook
filename = osp.join(LOCATION, 'saveTest.ipynb')
notebook.save_as(filename=filename)
nbwidget = notebook.get_current_nbwidget()
name = 'save.ipynb'
QTimer.singleShot(1000, lambda: manage_save_dialog(qtbot))
notebook.save_as(name=name)

# Wait for prompt
nbwidget = notebook.get_current_nbwidget()
qtbot.waitUntil(lambda: prompt_present(nbwidget), timeout=NOTEBOOK_UP)

# Assert that the In prompt has "test" in it
# and the client has the correct name
assert "test" in get_innerHTML(nbwidget, "CodeMirror-line")

def test_kill_notebook_kernel(qtbot):
"""Test that the kernel is killed from the server when closing a notebook."""
# Create notebook
notebook = setup_notebook(qtbot)

# Wait for prompt
nbwidget = notebook.get_current_nbwidget()
qtbot.waitUntil(lambda: prompt_present(nbwidget), timeout=NOTEBOOK_UP)

# Get kernel id for the client
kernel_id = "the server info about the id"
notebook.close_client()

# Get kernels id for the clients
assert notebook.get_current_client().get_short_name() == "save.ipynb"

def test_new_notebook(qtbot):
"""Test that a new client is really a notebook."""
Expand Down

0 comments on commit 10d22ce

Please sign in to comment.