Skip to content

Commit

Permalink
Demo: new tests for the frontends
Browse files Browse the repository at this point in the history
  • Loading branch information
jankatins committed Dec 10, 2015
1 parent 311fceb commit 83da104
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ python:
- 2.7
sudo: false
install:
- travis_retry pip install ipython nose requests
- travis_retry pip install ipython nose requests mock
- python setup.py develop
script:
- python test.py
16 changes: 12 additions & 4 deletions ipyext/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def demo(content, frontend=None):
frontend = nb_frontend
elif frontend == "ipython":
frontend = ipy_frontend
elif frontend == "notebook":
frontend = nb_frontend
elif isinstance(frontend, Frontend):
# just use it...
pass
else:
print("Not a valid frontend: {}".format(frontend))
return

if content == "STOP":
for frontend in (nb_frontend, ipy_frontend):
Expand All @@ -107,7 +115,8 @@ def demo(content, frontend=None):
try:
type, content = backend.get(content)
except Exception as e:
print(e.args)
print("Can't get the demo code:")
print("; ".join(e.args))
return

if type == "toc":
Expand Down Expand Up @@ -210,15 +219,15 @@ def get(self, content):
ret = r.json()
# unauthenticated requests have a ratelimit...
if 'message' in ret:
raise Exception(ret['message'])
raise Exception("Github: " + ret['message'])
if typ == "cells":
content = ret["content"]
content = decodebytes(content.encode())
content = content.decode()
# Todo: split into cells?
# splitting must depend on the frontend :-(
# in the notebook, splititng matplotlib stuff results in multiple plots
# (also the plot.show() call shoudld be in teh same cell). It would be ok
# (also the plot.show() call shoudld be in the same cell). It would be ok
# for the console...
return typ, [{"source": content, "cell_type":"code"}]
elif typ == "toc":
Expand Down Expand Up @@ -279,7 +288,6 @@ def __init__(self):
self._buffer = []
try:
self.ip = get_ipython()
self._orig_run_cell = self.ip.run_cell
except:
raise Exception("Not running in a IPython environment")

Expand Down
63 changes: 57 additions & 6 deletions ipyext/tests/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from __future__ import absolute_import

import nose.tools as nt
import mock

from IPython.testing import tools as tt

from ipyext.demo import demo, Frontend, GithubURLBackend
Expand All @@ -27,14 +29,20 @@ def _build(self, cells):
self.buffer.append(cell['cell_type'])
self.buffer.append(cell['source'])

def is_running(self):
if self.buffer:
return True
return False


def test_github_backend():
# this only works if we have access to the internet...
backend = GithubURLBackend()
typ, content = backend.get("<gh_matplotlib>")
nt.assert_equal(typ, "toc")
typ, content = backend.get("<gh_matplotlib>/style_sheets/plot_grayscale.py")
nt.assert_equal(typ, "cells")
# no full test of teh content as I don't want to rely on github and matplotlib for this...
# no full test of the content as I don't want to rely on github and matplotlib for this...

with tt.AssertPrints("Unknown github demo source"):
demo("<gh_not_xxx_existing>")
Expand All @@ -55,9 +63,9 @@ def test_python_backend():
"markdown",
"\n".join([
"## Comments",
"Comments are interpreted as markdown syntax, removing the initial `# `.",
"If a comment starts only as `#` it is interpreted as a comment, which will",
"end up together with the code."
"Comments are interpreted as markdown syntax, removing the",
"initial `# `. If a comment starts only with `#`, it is interpreted",
"as a code comment, which will end up together with the code."
]),
"code",
"\n".join([
Expand All @@ -68,8 +76,9 @@ def test_python_backend():
"markdown",
"\n".join([
"## Magics",
"Using magics would result in not compiling code, so magics have to be commented out.",
"The demo will remove the comment and insert it into the cell as code."
"Using magics would result in not compiling code, so magics",
"have to be commented out. The demo will remove the comment",
"and insert it into the cell as code."
]),
"code",
"\n".join([
Expand All @@ -85,8 +94,50 @@ def test_python_backend():
]

fe = TestFrontent(exp)

demo(ipyext.demo.demo_example, frontend=fe)


def test_wrong_frontend():
with tt.AssertPrints("Not a valid frontend"):
demo(nt, frontend="Not_Existing")


@mock.patch('IPython.display.display')
def test_notebook_frontend(mock_display):
from IPython.core.display import Javascript
ip = get_ipython()
ip.run_cell("from ipyext.demo import demo\nimport ipyext.demo")
ip.run_cell("demo(ipyext.demo.demo_example)")
nt.assert_true(mock_display.called)
nt.assert_equal(mock_display.call_count, 1)
# call_args = ((Javascript(...),),)
nt.assert_is_instance(mock_display.call_args[0][0], Javascript)
nt.assert_true("insert_cell_below" in mock_display.call_args[0][0]._repr_javascript_())
with tt.AssertPrints("No demo running... Nothing to do."):
ip.run_cell("demo('STOP')")


@mock.patch('IPython.core.interactiveshell.InteractiveShell.set_next_input')
def test_ipython_frontend(mock_set_next_input):
from IPython.core.display import Javascript
ip = get_ipython()
ip.run_cell("from ipyext.demo import demo\nimport ipyext.demo")
with tt.AssertPrints("Starting demo..."):
ip.run_cell("demo(ipyext.demo.demo_example, frontend='ipython')")
nt.assert_true("IPythonFrontend._post_run_cell" in repr(ip.events.callbacks["post_run_cell"]))
print(mock_set_next_input.call_args)
nt.assert_true(mock_set_next_input.called)
nt.assert_equal(mock_set_next_input.call_count, 1)
# call_args = (("<firstcell...>,),replace=False)
nt.assert_true("Comments are interpreted as markdown syntax" in mock_set_next_input.call_args[0][0])
ip.run_cell("pass")
nt.assert_true("Using magics would result" in mock_set_next_input.call_args[0][0])
with tt.AssertPrints("Demo stopped!"):
ip.run_cell("demo('STOP')")
nt.assert_false("IPythonFrontend._post_run_cell" in repr(ip.events.callbacks["post_run_cell"]))


if __name__ == '__main__':
import nose

Expand Down

0 comments on commit 83da104

Please sign in to comment.