forked from iiasa/ixmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for tutorials (iiasa#14)
- Loading branch information
1 parent
01d61a1
commit 618f03e
Showing
3 changed files
with
78 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
FROM gidden/messageix-base | ||
|
||
COPY . /message_ix | ||
COPY . /ixmp | ||
WORKDIR / | ||
ENV MESSAGE_IX_PATH /message_ix | ||
ENV IXMP_R_PATH /message_ix/ixmp | ||
RUN cd /message_ix && python2 setup.py install | ||
RUN cd /message_ix && python3 setup.py install | ||
ENV MESSAGE_IX_PATH /ixmp | ||
ENV IXMP_R_PATH /ixmp/ixmp | ||
RUN cd /ixmp && python2 setup.py install | ||
RUN cd /ixmp && python3 setup.py install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import io | ||
import os | ||
import subprocess | ||
import sys | ||
import tempfile | ||
import nbformat | ||
import pytest | ||
|
||
from testing_utils import here | ||
|
||
from test_r import r_installed | ||
|
||
xport_path = os.path.join(here, '..', 'tutorial', 'transport') | ||
|
||
# taken from the execellent example here: | ||
# https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/ | ||
|
||
|
||
def _notebook_run(path, kernel=None): | ||
"""Execute a notebook via nbconvert and collect output. | ||
:returns (parsed nb object, execution errors) | ||
""" | ||
major_version = sys.version_info[0] | ||
kernel = kernel or 'python{}'.format(major_version) | ||
dirname, __ = os.path.split(path) | ||
os.chdir(dirname) | ||
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout: | ||
args = [ | ||
"jupyter", "nbconvert", "--to", "notebook", "--execute", | ||
"--ExecutePreprocessor.timeout=60", | ||
"--ExecutePreprocessor.kernel_name={}".format(kernel), | ||
"--output", fout.name, path] | ||
subprocess.check_call(args) | ||
|
||
nb = nbformat.read(io.open(fout.name, encoding='utf-8'), | ||
nbformat.current_nbformat) | ||
|
||
errors = [ | ||
output for cell in nb.cells if "outputs" in cell | ||
for output in cell["outputs"] if output.output_type == "error" | ||
] | ||
|
||
return nb, errors | ||
|
||
|
||
def test_py_transport(): | ||
fname = os.path.join(xport_path, 'py_transport.ipynb') | ||
nb, errors = _notebook_run(fname) | ||
assert errors == [] | ||
|
||
|
||
def test_py_transport_scenario(): | ||
fname = os.path.join(xport_path, 'py_transport_scenario.ipynb') | ||
nb, errors = _notebook_run(fname) | ||
assert errors == [] | ||
|
||
|
||
@pytest.mark.skipif(not r_installed(), reason='requires R to be installed') | ||
def test_R_transport(): | ||
fname = os.path.join(xport_path, 'R_transport.ipynb') | ||
nb, errors = _notebook_run(fname, kernel='IR') | ||
assert errors == [] | ||
|
||
|
||
@pytest.mark.skipif(not r_installed(), reason='requires R to be installed') | ||
def test_R_transport_scenario(): | ||
fname = os.path.join(xport_path, 'R_transport_scenario.ipynb') | ||
nb, errors = _notebook_run(fname, kernel='IR') | ||
assert errors == [] |