Skip to content

Commit

Permalink
add notebook coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Aug 15, 2017
1 parent d1096ff commit 5f718b8
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 55 deletions.
70 changes: 70 additions & 0 deletions _unittests/ut_documentation/test_LONG_notebook_scratch.py
@@ -0,0 +1,70 @@
#-*- coding: utf-8 -*-
"""
@brief test log(time=60s)
"""

import sys
import os
import unittest

try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

try:
import pyquickhelper as skip_
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper as skip_


from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, add_missing_development_version
from pyquickhelper.ipythonhelper import execute_notebook_list_finalize_ut
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook
import src.code_beatrix


class TestLONGNotebookScratch(unittest.TestCase):

def setUp(self):
add_missing_development_version(["pymyinstall", "pyensae", "pymmails", "ensae_projects",
"jyquickhelper"], __file__, hide=True)

def test_long_notebook_scratch(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_scratch")
keepnote = ls_notebooks("scratch")
self.assertTrue(len(keepnote) > 0)
res = execute_notebooks(temp, keepnote,
lambda i, n: "deviner" not in n,
fLOG=fLOG,
clean_function=clean_function_notebook)
execute_notebook_list_finalize_ut(
res, fLOG=fLOG, dump=src.code_beatrix)


if __name__ == "__main__":
unittest.main()
18 changes: 9 additions & 9 deletions _unittests/ut_documentation/test_blog_helper.py
Expand Up @@ -58,9 +58,9 @@ def test_post_parse(self):
"source", "blog", "2015", "2015-04-04_tinkerlab.rst")
p = BlogPost(file)
fLOG(p.title)
assert len(p.title) > 0
assert p.date == "2015-04-04"
assert isinstance(p.Fields, dict)
self.assertTrue(len(p.title) > 0)
self.assertEqual(p.date, "2015-04-04")
self.assertTrue(isinstance(p.Fields, dict))

def test_post_list(self):
fLOG(
Expand All @@ -73,21 +73,21 @@ def test_post_list(self):
path = os.path.abspath(os.path.split(__file__)[0])
fold = os.path.join(
path, "..", "..", "_doc", "sphinxdoc", "source", "blog")
assert os.path.exists(fold)
self.assertTrue(os.path.exists(fold))
out = get_temp_folder(__file__, "temp_post_list")
p = BlogPostList(fold)
cats = p.get_categories()
fLOG(cats)
months = p.get_months()
fLOG(months)
assert len(p) > 0
assert len(cats) > 0
assert len(months) > 0
self.assertTrue(len(p) > 0)
self.assertTrue(len(cats) > 0)
self.assertTrue(len(months) > 0)

res = p.write_aggregated(out)
assert len(res) >= 4
self.assertTrue(len(res) >= 4)
for r in res:
assert os.path.exists(r)
self.assertTrue(os.path.exists(r))


if __name__ == "__main__":
Expand Down
9 changes: 6 additions & 3 deletions _unittests/ut_documentation/test_notebook_algorithm.py
Expand Up @@ -39,7 +39,9 @@

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, add_missing_development_version
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook, unittest_raise_exception_notebook
from pyquickhelper.ipythonhelper import execute_notebook_list_finalize_ut
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook
import src.code_beatrix


class TestNotebookAlgorithm (unittest.TestCase):
Expand All @@ -55,12 +57,13 @@ def test_notebook_algorithm(self):
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_algorithm")
keepnote = ls_notebooks("algorithmes")
assert len(keepnote) > 0
self.assertTrue(len(keepnote) > 0)
res = execute_notebooks(temp, keepnote,
lambda i, n: "deviner" not in n,
fLOG=fLOG,
clean_function=clean_function_notebook)
unittest_raise_exception_notebook(res, fLOG)
execute_notebook_list_finalize_ut(
res, fLOG=fLOG, dump=src.code_beatrix)


if __name__ == "__main__":
Expand Down
15 changes: 11 additions & 4 deletions _unittests/ut_documentation/test_notebook_example.py
Expand Up @@ -38,25 +38,32 @@
import pyquickhelper as skip_

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook, unittest_raise_exception_notebook
from pyquickhelper.pycode import get_temp_folder, add_missing_development_version
from pyquickhelper.ipythonhelper import execute_notebook_list_finalize_ut
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook
import src.code_beatrix


class TestNotebookExample (unittest.TestCase):

def setUp(self):
add_missing_development_version(
["pyensae", "jyquickhelper"], __file__)

def test_notebook_example(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_exemples")
keepnote = ls_notebooks("exemples")
assert len(keepnote) > 0
self.assertTrue(len(keepnote) > 0)
res = execute_notebooks(temp, keepnote,
lambda i, n: "deviner" not in n,
fLOG=fLOG,
clean_function=clean_function_notebook)
unittest_raise_exception_notebook(res, fLOG)
execute_notebook_list_finalize_ut(
res, fLOG=fLOG, dump=src.code_beatrix)


if __name__ == "__main__":
Expand Down
70 changes: 70 additions & 0 deletions _unittests/ut_documentation/test_notebook_javascript.py
@@ -0,0 +1,70 @@
#-*- coding: utf-8 -*-
"""
@brief test log(time=60s)
"""

import sys
import os
import unittest

try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

try:
import pyquickhelper as skip_
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper as skip_


from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, add_missing_development_version
from pyquickhelper.ipythonhelper import execute_notebook_list_finalize_ut
from src.code_beatrix.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook
import src.code_beatrix


class TestNotebookJavascript(unittest.TestCase):

def setUp(self):
add_missing_development_version(["pymyinstall", "pyensae", "pymmails", "ensae_projects",
"jyquickhelper"], __file__, hide=True)

def test_notebook_javascript(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_javascript")
keepnote = ls_notebooks("javascript")
self.assertTrue(len(keepnote) > 0)
res = execute_notebooks(temp, keepnote,
lambda i, n: "deviner" not in n,
fLOG=fLOG,
clean_function=clean_function_notebook)
execute_notebook_list_finalize_ut(
res, fLOG=fLOG, dump=src.code_beatrix)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion src/code_beatrix/automation/notebook_const_helper.py
Expand Up @@ -8,7 +8,7 @@

def voyageur_de_commerce_points():
"""
default value for the notebook ``voyageur_de_commerce``
Default values for the notebook :ref:`voyageurdecommercerst`.
"""
return [(533.61669921875, -201.39999389648438),
(451.6166687011719, -201.39999389648438),
Expand Down
42 changes: 4 additions & 38 deletions src/code_beatrix/automation/notebook_test_helper.py
Expand Up @@ -7,7 +7,7 @@
import sys
from pyquickhelper.loghelper import noLOG
from pyquickhelper.ipythonhelper.notebook_helper import install_python_kernel_for_unittest
from pyquickhelper.ipythonhelper import run_notebook
from pyquickhelper.ipythonhelper import execute_notebook_list


def ls_notebooks(subfolder):
Expand Down Expand Up @@ -141,40 +141,6 @@ def valid_cell(cell):

kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest(
"code_beatrix")
addpath = get_additional_paths()
results = {}
for i, note in enumerate(notebooks):
if filter(i, note):
fLOG("******", i, os.path.split(note)[-1])
outfile = os.path.join(folder, "out_" + os.path.split(note)[-1])
try:
out = run_notebook(note, working_dir=folder, outfilename=outfile,
additional_path=addpath,
valid=valid_cell,
clean_function=clean_function,
fLOG=deepfLOG,
kernel_name=kernel_name
)
if not os.path.exists(outfile):
raise FileNotFoundError(outfile)
results[note] = (True, out)
except Exception as e:
results[note] = (False, e)
return results


def unittest_raise_exception_notebook(res, fLOG):
"""
same code for all unit tests
@param res output of @see fn execute_notebooks
"""
assert len(res) > 0
fails = [(os.path.split(k)[-1], ) + v
for k, v in sorted(res.items()) if not v[0]]
for f in fails:
fLOG(f)
if len(fails) > 0:
raise fails[0][-1]
for k, v in sorted(res.items()):
fLOG("final", os.path.split(k)[-1], v[0], v[1])
addpaths = get_additional_paths()
return execute_notebook_list(
folder, notebooks, fLOG=fLOG, valid=valid_cell, additional_path=addpaths, kernel_name=kernel_name)

0 comments on commit 5f718b8

Please sign in to comment.