# -*- coding: utf-8 -*-
"""
@brief      test log(time=21s)
"""

import sys
import os
import unittest
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, add_missing_development_version, is_travis_or_appveyor
from pyquickhelper.ipythonhelper import execute_notebook_list, execute_notebook_list_finalize_ut, get_additional_paths


class TestNotebook123Coverage(unittest.TestCase):

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

    def a_test_notebook_runner(self, name, folder, valid=None):
        temp = get_temp_folder(__file__, "temp_notebook_123_{0}".format(name))
        doc = os.path.join(temp, "..", "..", "..", "_doc", "notebooks", folder)
        self.assertTrue(os.path.exists(doc))
        keepnote = [os.path.join(doc, _) for _ in os.listdir(doc) if name in _]
        self.assertTrue(len(keepnote) > 0)

        import pyquickhelper
        import jyquickhelper
        import pyensae
        import jupytalk
        add_path = get_additional_paths(
            [jyquickhelper, pyquickhelper, pyensae, jupytalk])
        res = execute_notebook_list(
            temp, keepnote, additional_path=add_path, valid=valid)
        execute_notebook_list_finalize_ut(res, fLOG=fLOG, dump=jupytalk)

    def test_notebook_automation_finance_trading(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if is_travis_or_appveyor() in ('circleci',):
            # It fails on circleci.
            # prices = StockPrices(tick="NASDAQ:MSFT") --> HTTPError
            return
        self.a_test_notebook_runner("automation_finance_trading", "2017/meshs")

    def test_notebook_centrale_201606(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        self.a_test_notebook_runner("centrale_201606", "2016/centrale")

    def test_notebook_kaggle_review_2016(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        self.a_test_notebook_runner("kaggle_review_2016", "2016/ensae")

    def test_notebook_10_plotting_libraries(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        self.a_test_notebook_runner("10_plotting_libraries", "2016/pydata")

    def test_notebook_js_lightning_python(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        def valid(cell):
            if "from lightning import Lightning" in cell:
                return False
            if "lgn.scatter(x, y, alpha=0.5, values=v, colormap='Reds')" in cell:
                return False
            return True

        self.a_test_notebook_runner(
            "js_lightning_python", "2016/pydata", valid=valid)

    def test_notebook_pyjsc_vispy(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        self.a_test_notebook_runner("pyjsc_vispy", "2016/pydata")

    def test_notebook_2017_1a_ensae_nocture(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        self.a_test_notebook_runner("2017_1a_ensae_nocture", "2017/ensae")

    def test_notebook_2017_kaggle(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        self.a_test_notebook_runner("kaggle", "2017/ensae")


if __name__ == "__main__":
    unittest.main()
