def test_notebook_runner_correction_1_7(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook1a_correction_1_7")
        keepnote = ls_notebooks("td1a")
        assert len(keepnote) > 0

        cp = os.path.join(temp, "..", "data", "seance4_excel.txt")
        shutil.copy(cp, temp)
        cp = os.path.join(temp, "..", "data", "seance4_excel.xlsx")
        shutil.copy(cp, temp)

        res = execute_notebooks(temp, keepnote,
                                lambda i, n: "_12" not in n
                                and "session6." not in n
                                and "session8." not in n
                                and "session9." not in n
                                and "session_10." not in n
                                and "session_11." not in n
                                and "correction" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Пример #2
0
    def test_graph_corrplot(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # it requires scipy which is not included in the requirements.txt
            return

        fix_tkinter_issues_virtualenv()
        from matplotlib import pyplot as plt
        plt.style.use('ggplot')

        temp = get_temp_folder(__file__, "temp_corrplot")
        letters = "ABCDEFGHIJKLMNOP"[0:10]
        df = pandas.DataFrame(
            dict(((k, numpy.random.random(10) + ord(k) - 65) for k in letters)))
        df = df.corr()

        fig = plt.figure(num=None, facecolor='white')
        ax = plt.subplot(1, 1, 1, aspect='equal', axisbg='white')

        c = Corrplot(df)
        ax = c.plot(fig=fig, ax=ax)

        fLOG("save")
        img = os.path.join(temp, "corrplot.png")
        fig.savefig(img)
        fLOG("close")
        plt.close('all')
        fLOG("end")
        assert os.path.exists(img)
Пример #3
0
    def test_notebook_runner(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        # this notebook describes how to distribute the work with multiple processors
        # it requires to start multiple clusters first (a command line)
        # and to stop them afterwards
        # it still needs to be implemented
        # we skip !
        warnings.warn(
            "TODO: implement a unit test testing the distribution on multiple processors"
        )
        return

        temp = get_temp_folder(__file__, "temp_notebook2a_2_enonce_2D")
        keepnote = ls_notebooks("td2a")
        assert len(keepnote) > 0
        res = execute_notebooks(
            temp,
            keepnote,
            lambda i, n: "_2" in n and "enonce" in n and "_2D" in n,
            fLOG=fLOG,
            clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
    def test_graph_graphviz(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        code = """
        namespace hello
        {
            public static class world
            {
                public static double function(double x, doubly y)
                {
                    return x+y ;
                }
            }
        }
        """

        clparser, cllexer = get_parser_lexer("C#")
        parser = parse_code(code, clparser, cllexer)
        tree = parser.parse()
        st = get_tree_graph(tree, parser)
        dot = st.to_dot()
        # fLOG(dot)
        assert len(dot) > 0

        if "travis" not in sys.executable:
            temp = get_temp_folder(__file__, "temp_dot_grammar")
            name = os.path.join(temp, "graph.dot")
            with open(name, "w") as f:
                f.write(dot)
            img = os.path.join(temp, "graph.png")
            run_dot(name, img)
            assert os.path.exists(img)
 def test_notebook_runner(self):
     fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook2a_1")
     keepnote = ls_notebooks("td2a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote, lambda i, n: "_1" in n, fLOG=fLOG)
     unittest_raise_exception_notebook(res, fLOG)
Пример #6
0
    def test_notebook_latex1(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        path = os.path.abspath(os.path.split(__file__)[0])
        nbfold = os.path.normpath(
            os.path.join(path, "..", "..", "_doc", "notebooks"))
        nbs = [os.path.join(nbfold, "notebook_eleves", "2014_2015", "2015_kmeans.ipynb"),
               # os.path.join(nbfold, "notebook_eleves", "2014_2015", "2015_page_rank.ipynb"),
               # os.path.join(nbfold, "notebook_eleves", "2014_2015", "2015_factorisation_matrice.ipynb"),
               ]

        formats = ["pdf", ]

        temp = get_temp_folder(__file__, "temp_nb_bug_latex1")

        if "travis" in sys.executable:
            warnings.warn(
                "travis, unable to test TestNoteBooksBugLatex_ecs.test_notebook_latex1")
            return

        res = process_notebooks(nbs, temp, temp, formats=formats)
        fLOG("*****", len(res))
        for _ in res:
            fLOG(_)
            assert os.path.exists(_[0])
    def test_notebook_runner_2a_long(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and R
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_long.test_notebook_runner_2a_long")
            return

        if "R_HOME" not in os.environ or not os.path.exists(os.environ["R_HOME"]):
            paths = [r"C:\Program Files\R\R-3.2.2",
                     r"C:\Program Files\R\R-3.1.2"]
            for path in paths:
                if os.path.exists(path):
                    os.environ["R_HOME"] = path
                    break

        temp = get_temp_folder(__file__, "temp_notebook2a_long_")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp, keepnote,
                                lambda i, n: "python_r" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Пример #8
0
    def test_notebook_runner_2a_long(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and R
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_long.test_notebook_runner_2a_long"
            )
            return

        if "R_HOME" not in os.environ or not os.path.exists(
                os.environ["R_HOME"]):
            paths = [
                r"C:\Program Files\R\R-3.2.2", r"C:\Program Files\R\R-3.1.2"
            ]
            for path in paths:
                if os.path.exists(path):
                    os.environ["R_HOME"] = path
                    break

        temp = get_temp_folder(__file__, "temp_notebook2a_long_")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp,
                                keepnote,
                                lambda i, n: "python_r" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
    def test_notebook_runner_flat2db3(self):
        fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
        notebook = os.path.split(__file__)[-1].replace(".ipynb", "").replace(".py", "")[5:]
        temp = get_temp_folder(__file__, "temp_" + notebook)
        nbfile = os.path.join(temp, "..", "..", "..", "_doc", "notebooks", "%s.ipynb" % notebook)
        if not os.path.exists(nbfile):
            raise FileNotFoundError(nbfile)
        addpath = [
            os.path.normpath(os.path.join(temp, "..", "..", "..", "src")),
            os.path.normpath(os.path.join(temp, "..", "..", "..", "..", "pyquickhelper", "src")),
            os.path.normpath(os.path.join(temp, "..", "..", "..", "..", "pymyinstall", "src")),
        ]
        outfile = os.path.join(temp, "out_notebook.ipynb")
        assert not os.path.exists(outfile)
        valid = lambda code: 'run_cmd("SQLiteSpy.exe velib_vanves.db3")' not in code

        if "travis" in sys.executable:
            return

        kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest("pyensae")

        out = run_notebook(
            nbfile,
            working_dir=temp,
            outfilename=outfile,
            additional_path=addpath,
            valid=valid,
            fLOG=fLOG,
            kernel_name=kernel_name,
        )
        fLOG(out)
        assert os.path.exists(outfile)
Пример #10
0
    def test_notebook_runner(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook1")
        nbfile = os.path.join(
            temp,
            "..",
            "..",
            "..",
            "_doc",
            "notebooks",
            "td2a",
            "td2a_correction_session_1.ipynb")
        nbfile = os.path.normpath(nbfile)
        assert os.path.exists(nbfile)
        addpath = [
            os.path.dirname(
                pyquickhelper.__file__), os.path.dirname(
                pyensae.__file__)]
        addpath = [os.path.normpath(os.path.join(_, "..")) for _ in addpath]

        outfile = os.path.join(temp, "out_" + os.path.split(nbfile)[-1])
        assert not os.path.exists(outfile)
        stat, out = run_notebook(nbfile, working_dir=temp, outfilename=outfile,
                                 additional_path=addpath,
                                 valid=lambda code: '%system' not in code)
        fLOG(out)
        assert os.path.exists(outfile)
Пример #11
0
 def test_notebook_runner(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook2a_2_corection_2B")
     keepnote = ls_notebooks("td2a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote, lambda i, n: "_2" in n
                             and "correction" in n and "_2B" in n, fLOG=fLOG)
     unittest_raise_exception_notebook(res, fLOG)
    def test_folium(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        import folium
        temp = get_temp_folder(__file__, "temp_folium")
        outfile = os.path.join(temp, "osm.map")

        map_osm = folium.Map(location=[48.85, 2.34])
        map_osm.create_map(path=outfile)
        assert os.path.exists(outfile)
    def test_server_start_run(self):
        """
        if this test fails, the unit test is stuck, you need to stop the program yourself
        """
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip travis and Flask
            warnings.warn(
                "travis, unable to test TestSimpleServerRSSTeaching.test_server_start_run")
            return

        temp = get_temp_folder(__file__, "temp_rss_starter")
        dirn = os.path.abspath(os.path.dirname(location))
        xmlb = None

        data = os.path.join(temp, "..", "data", "ensae_teaching_cs_blogs.db3")
        shutil.copy(data, temp)
        dbfile = os.path.join(temp, "ensae_teaching_cs_blogs.db3")

        server = RSSServer(('localhost', 8093), dbfile)

        thread = rss_teachings_update_run_server(dbfile=dbfile, xml_blogs=xmlb,
                                                 browser="none",
                                                 server=server, thread=True)

        fLOG(thread)
        fLOG("fetching first url")
        url = "http://localhost:8093/"
        cont = get_url_content_timeout(url)
        if "Traceback" in cont and not "l''exception ::      Traceback" in cont:
            raise Exception(cont)
        assert len(cont) > 0
        assert "RSS" in cont
        assert "XD blog" in cont

        fLOG("fetching first url")
        url = "http://localhost:8093/rss_search.html?searchterm=command"
        cont = get_url_content_timeout(url)
        if "Traceback" in cont:
            fLOG(cont)
        if "Traceback" in cont and not "l''exception ::      Traceback" in cont:
            raise Exception(cont)
        assert len(cont) > 0
        assert "RSS" in cont
        assert "interesting" not in cont
        assert "command" in cont

        thread.shutdown()
        assert not thread.is_alive()
 def test_notebook_runner_1a(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_")
     keepnote = ls_notebooks("1a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "deviner" not in n,
                             fLOG=fLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
    def test_folium(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        import folium
        temp = get_temp_folder(__file__, "temp_folium")
        outfile = os.path.join(temp, "osm.map")

        map_osm = folium.Map(location=[48.85, 2.34])
        map_osm.create_map(path=outfile)
        assert os.path.exists(outfile)
 def test_notebook_runner_correction_11(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_correction_11")
     keepnote = ls_notebooks("td1a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "correction_session_11" in n,
                             fLOG=fLOG,
                             clean_function=TestNotebookRunner1a_correction_11.clean_function)
     unittest_raise_exception_notebook(res, fLOG)
 def test_notebook_runner_exams(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexams_")
     keepnote = ls_notebooks("exams")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "git" not in n and "interro_rapide_20_minutes_2014_11" not in n,
                             fLOG=fLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Пример #18
0
 def test_notebook_runner_exams(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexams_")
     keepnote = ls_notebooks("exams")
     assert len(keepnote) > 0
     res = execute_notebooks(temp,
                             keepnote,
                             lambda i, n: "git" not in n and
                             "interro_rapide_20_minutes_2014_11" not in n,
                             fLOG=fLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Пример #19
0
    def test_notebook_runner_2a(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip C# on linux
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_csharp.test_notebook_runner_2a"
            )
            return

        if not sys.platform.startswith("win"):
            return

        temp = get_temp_folder(__file__, "temp_notebook2a_sharp")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp,
                                keepnote,
                                lambda i, n: "csharp" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)

        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:
            e = str(fails[0][1][-1])
            if "Audio device error encountered" in str(e):
                # maybe the script is running on a virtual machine (no Audia
                # device)
                if os.environ["USERNAME"] == "ensaestudent" or \
                   os.environ["USERNAME"] == "vsxavierdupre" or \
                   "paris" in os.environ["COMPUTERNAME"].lower() or \
                   os.environ["USERNAME"].endswith("$"):  # anonymous Jenkins configuration
                    # I would prefer to catch a proper exception
                    # it just exclude one user only used on remotre machines
                    fLOG("no audio")
                    return
            elif "<class 'int'>-" in str(e):
                # issue with conversion from 3 to double
                return

            fLOG(str(e).replace("\n", " EOL "))
            raise fails[0][1][-1]
        else:
            fLOG("success")
 def test_notebook_runner_expose1(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexpose1_")
     keepnote = ls_notebooks("expose")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "velib" not in n and "paris_parcours" not in n
                             and "ml_table_mortalite" not in n,
                             fLOG=fLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
 def test_notebook_runner_exposelong3(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexposelong3_")
     keepnote = ls_notebooks("expose")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "mortalite" in n,
                             fLOG=fLOG,
                             deepfLOG=fLOG if __name__ == "__main__" else noLOG,
                             clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Пример #22
0
 def test_notebook_runner_enonce_12(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_correction_12")
     keepnote = ls_notebooks("td1a")
     assert len(keepnote) > 0
     res = execute_notebooks(temp, keepnote,
                             lambda i, n: "correction_session_12" in n,
                             fLOG=fLOG,
                             deepfLOG=fLOG if __name__ == "__main__" else noLOG,
                             clean_function=TestNotebookRunner1a_correction_12.clean_function)
     unittest_raise_exception_notebook(res, fLOG)
    def test_notebook_runner_2a(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if "travis" in sys.executable:
            # skip C# on linux
            warnings.warn(
                "travis, unable to test TestNotebookRunner2a_csharp.test_notebook_runner_2a")
            return

        if not sys.platform.startswith("win"):
            return

        temp = get_temp_folder(__file__, "temp_notebook2a_sharp")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp, keepnote,
                                lambda i, n: "csharp" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)

        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:
            e = str(fails[0][1][-1])
            if "Audio device error encountered" in str(e):
                # maybe the script is running on a virtual machine (no Audia
                # device)
                if os.environ["USERNAME"] == "ensaestudent" or \
                   os.environ["USERNAME"] == "vsxavierdupre" or \
                   "paris" in os.environ["COMPUTERNAME"].lower() or \
                   os.environ["USERNAME"].endswith("$"):  # anonymous Jenkins configuration
                    # I would prefer to catch a proper exception
                    # it just exclude one user only used on remotre machines
                    fLOG("no audio")
                    return
            elif "<class 'int'>-" in str(e):
                # issue with conversion from 3 to double
                return

            fLOG(str(e).replace("\n", " EOL "))
            raise fails[0][1][-1]
        else:
            fLOG("success")
 def test_notebook_runner_exposelong1(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexposelong1_")
     keepnote = ls_notebooks("expose")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "velib" in n,
         fLOG=fLOG,
         deepfLOG=fLOG if __name__ == "__main__" else noLOG,
         clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
Пример #25
0
 def test_notebook_runner_expose1(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebookexpose1_")
     keepnote = ls_notebooks("expose")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "velib" not in n and "paris_parcours" not in n and
         "ml_table_mortalite" not in n,
         fLOG=fLOG,
         clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
    def test_simple_for_workflow(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        code = """
        data_a = flowdata.DataCloud ;
        modb = flowmodule.RandomFilter(
                    n= 100
                    ) ;
        connect data_a to modb.input ;

        if ( positive_number(modb.average)) {
            modc = flowmodule.Classify(model="LinearRegression") ;
            connect ( modb.data , modc.train_data ) ;

            if ( positive_number(modc.number)) {
                modc2 = flowmodule.Classify(model="SVM") ;
                connect ( modc.data , mod2.input ) ;

                for ( loopv in range(10) ) {
                    modcl = flowmodule.Classify(model=loopv) ;
                    connect ( modc2.data , modcl.output ) ;
                }

            }
        }
        """

        clparser, cllexer = get_parser_lexer("SimpleWorkflow")
        parser = parse_code(code, clparser, cllexer)
        tree = parser.parse()
        st = get_tree_string(tree, parser)
        assert len(st) > 0
        st = get_tree_graph(tree, parser)
        dot = st.to_dot()
        assert len(dot) > 0

        if "travis" not in sys.executable:
            temp = get_temp_folder(__file__, "temp_simpleworkflow_grammar_for")
            name = os.path.join(temp, "graph.dot")
            with open(name, "w") as f:
                f.write(dot)
            img = os.path.join(temp, "graph.png")
            run_dot(name, img)
            assert os.path.exists(img)
Пример #27
0
    def test_files_compress(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        this = os.path.abspath(__file__)
        temp = get_temp_folder(__file__, "temp_compress")
        dest = os.path.join(temp, "temp_this.zip")

        mg = MagicFile()
        cmd = "dest [this]"
        fLOG("**", cmd)
        assert not os.path.exists(dest)
        mg.add_context({"this": this, "dest": dest})
        res = mg.compress(cmd)
        fLOG(res)
        assert os.path.exists(dest)
        assert res == 1
Пример #28
0
 def test_notebook_runner_correction_9_10(self):
     fLOG(__file__,
          self._testMethodName,
          OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_correction_9_10")
     keepnote = ls_notebooks("td1a")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "_12" not in n and "session1." not in n and
         "session2." not in n and "session3." not in n and "session4."
         not in n and "session5." not in n and "session6." not in n and
         "session7." not in n and "session8." not in n and "session_11."
         not in n and "correction" in n,
         fLOG=fLOG,
         clean_function=clean_function_1a)
     unittest_raise_exception_notebook(res, fLOG)
    def test_notebook_runner_magic_command(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        notebook = os.path.split(
            __file__)[-1].replace(".ipynb", "").replace(".py", "")[5:]
        temp = get_temp_folder(__file__, "temp_" + notebook)
        nbfile = os.path.join(
            temp,
            "..",
            "..",
            "..",
            "_doc",
            "notebooks",
            "%s.ipynb" %
            notebook)
        if not os.path.exists(nbfile):
            raise FileNotFoundError(nbfile)
        addpath = [os.path.normpath(os.path.join(temp, "..", "..", "..", "src")),
                   os.path.normpath(
            os.path.join(
                temp,
                "..",
                "..",
                "..",
                "..",
                "pyquickhelper",
                "src")),
        ]

        kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest(
            "pyensae")

        outfile = os.path.join(temp, "out_notebook.ipynb")
        assert not os.path.exists(outfile)
        out = run_notebook(
            nbfile,
            working_dir=temp,
            outfilename=outfile,
            additional_path=addpath,
            kernel_name=kernel_name)
        fLOG(out)
        assert os.path.exists(outfile)
Пример #30
0
    def test_write_script(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_write_script")

        res = write_module_scripts(temp, "win32", __blog__)
        assert len(res) > 1
        for c in res:
            assert os.path.exists(c)
            with open(c, "r") as f:
                content = f.read()
            if "__" in content:
                for line in content.split("\n"):
                    if "__" in line and "sys.path.append" not in line and "__file__" not in line:
                        raise Exception(content)
            if ".xml" in c:
                if '<outline text="' not in content:
                    raise Exception(content)
    def test_notebook_runner_enonce_1_7(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook1a_enonce_1_7")
        keepnote = ls_notebooks("td1a")
        assert len(keepnote) > 0

        res = execute_notebooks(temp, keepnote,
                                lambda i, n: "_12" not in n
                                and "cenonce_session1." not in n
                                and "cenonce_session6." not in n
                                and "cenonce_session8." not in n
                                and "cenonce_session9." not in n
                                and "cenonce_session_10." not in n
                                and "cenonce_session_11." not in n
                                and "enonce" in n,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
    def test_write_script(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_write_script")

        res = write_module_scripts(temp, "win32", __blog__)
        assert len(res) > 1
        for c in res:
            assert os.path.exists(c)
            with open(c, "r") as f:
                content = f.read()
            if "__" in content:
                for line in content.split("\n"):
                    if "__" in line and "sys.path.append" not in line and "__file__" not in line:
                        raise Exception(content)
            if ".xml" in c:
                if '<outline text="' not in content:
                    raise Exception(content)
Пример #33
0
    def test_faq_cython_compile(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_cython_primes")
        primes = os.path.join(temp, "..", "primes.pyx")
        shutil.copy(primes, temp)
        newfile = os.path.join(temp, "primes.pyx")
        cwd = os.getcwd()

        if "travis" in sys.executable:
            # the unit test does not end on travis
            warnings.warn("test_faq_cython_compile not test on travis")
            return

        compile_cython_single_script(newfile, fLOG=fLOG)

        # we checked it worked
        assert os.path.exists(os.path.join(temp, "primes.pyd"))

        sys.path.append(temp)
        import primes
        del sys.path[-1]

        p = primes.primes(100)
        fLOG(p)
        assert p == [
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
            67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
            139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
            211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
            281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
            367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439,
            443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
            523, 541
        ]

        fLOG(os.getcwd())
        assert cwd == os.getcwd()
Пример #34
0
    def test_graph_style(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_dot")

        if "travis" not in sys.executable:
            # still some issues with scipy and fortran compiler + graphviz
            # dependency
            from sklearn.datasets import load_iris
            from sklearn import tree

            clf = tree.DecisionTreeClassifier()
            iris = load_iris()
            clf = clf.fit(iris.data, iris.target)
            outfile = os.path.join(temp, "out_tree.dot")
            tree.export_graphviz(clf, out_file=outfile)
            img = os.path.join(temp, "out_tree.png")
            out, err = run_dot(outfile, img)
            assert os.path.exists(img)
    def test_faq_cython_compile(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_cython_primes")
        primes = os.path.join(temp, "..", "primes.pyx")
        shutil.copy(primes, temp)
        newfile = os.path.join(temp, "primes.pyx")
        cwd = os.getcwd()

        if "travis" in sys.executable:
            # the unit test does not end on travis
            warnings.warn("test_faq_cython_compile not test on travis")
            return

        compile_cython_single_script(newfile, fLOG=fLOG)

        # we checked it worked
        assert os.path.exists(os.path.join(temp, "primes.pyd"))

        sys.path.append(temp)
        import primes
        del sys.path[-1]

        p = primes.primes(100)
        fLOG(p)
        assert p == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
                     61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
                     139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
                     211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
                     281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
                     367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439,
                     443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
                     523, 541]

        fLOG(os.getcwd())
        assert cwd == os.getcwd()
Пример #36
0
    def test_venv_docutils08_readme(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        fold = os.path.dirname(os.path.abspath(__file__))
        readme = os.path.join(fold, "..", "..", "README.rst")
        assert os.path.exists(readme)
        with open(readme, "r", encoding="utf8") as f:
            content = f.read()

        temp = get_temp_folder(__file__, "temp_readme")

        if __name__ != "__main__":
            # does not work from a virtual environment
            return

        from pyquickhelper.pycode import check_readme_syntax
        from pyquickhelper.helpgen.markdown_helper import yield_sphinx_only_markup_for_pipy

        check_readme_syntax(readme, folder=temp, fLOG=fLOG)
Пример #37
0
    def test_notebook_runner_2a(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook2a_")
        keepnote = ls_notebooks("2a")
        assert len(keepnote) > 0

        def filter(i, n):
            if "git" not in n and "python_r" not in n and "csharp" not in n:
                if not sys.platform.startswith("win") and "_convert" in n:
                    return False
                else:
                    return True
            return False

        res = execute_notebooks(temp, keepnote,
                                filter,
                                fLOG=fLOG,
                                clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Пример #38
0
    def test_notebook_runner_correction_1_7(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")
        temp = get_temp_folder(__file__, "temp_notebook1a_correction_1_7")
        keepnote = ls_notebooks("td1a")
        assert len(keepnote) > 0

        cp = os.path.join(temp, "..", "data", "seance4_excel.txt")
        shutil.copy(cp, temp)
        cp = os.path.join(temp, "..", "data", "seance4_excel.xlsx")
        shutil.copy(cp, temp)

        res = execute_notebooks(
            temp,
            keepnote,
            lambda i, n: "_12" not in n and "session6." not in n and
            "session8." not in n and "session9." not in n and "session_10."
            not in n and "session_11." not in n and "correction" in n,
            fLOG=fLOG,
            clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Пример #39
0
    def test_notebook_folium(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_folium")
        outfile = os.path.join(temp, 'osm.html')

        if sys.platform.startswith("win") and ("anaconda" in sys.base_prefix or
                                               "_condavir" in sys.executable):
            # an expected error
            # jinja2.exceptions.TemplateNotFound: tiles\openstreetmap\tiles.txt
            warnings.warn(
                "test_notebook_folium not run on Anaconda, it raises that error: jinja2.exceptions.TemplateNotFound: tiles/openstreetmap/tiles.txt")
            return

        import folium
        map_osm = folium.Map(location=[48.85, 2.34])
        map_osm.create_map(path=outfile)
        assert os.path.exists(outfile)
        folium_inline_map(map_osm)
        fLOG("done")
 def test_notebook_runner_correction_9_10(self):
     fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")
     temp = get_temp_folder(__file__, "temp_notebook1a_correction_9_10")
     keepnote = ls_notebooks("td1a")
     assert len(keepnote) > 0
     res = execute_notebooks(
         temp,
         keepnote,
         lambda i, n: "_12" not in n
         and "session1." not in n
         and "session2." not in n
         and "session3." not in n
         and "session4." not in n
         and "session5." not in n
         and "session6." not in n
         and "session7." not in n
         and "session8." not in n
         and "session_11." not in n
         and "correction" in n,
         fLOG=fLOG,
         clean_function=clean_function_1a,
     )
     unittest_raise_exception_notebook(res, fLOG)
    def test_notebook_runner(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        # this notebook describes how to distribute the work with multiple processors
        # it requires to start multiple clusters first (a command line)
        # and to stop them afterwards
        # it still needs to be implemented
        # we skip !
        warnings.warn(
            "TODO: implement a unit test testing the distribution on multiple processors")
        return

        temp = get_temp_folder(__file__, "temp_notebook2a_2_enonce_2D")
        keepnote = ls_notebooks("td2a")
        assert len(keepnote) > 0
        res = execute_notebooks(temp, keepnote, lambda i, n: "_2" in n
                                and "enonce" in n
                                and "_2D" in n,
                                fLOG=fLOG, clean_function=clean_function_1a)
        unittest_raise_exception_notebook(res, fLOG)
Пример #42
0
    def test_command_magic(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        temp = get_temp_folder(__file__, "temp_magic_command")
        data = os.path.join(temp, "..", "data", "velib_vanves.txt")
        dbfile = os.path.join(temp, "temp_magic.db3")

        # connect
        magic = MagicSQL()
        magic.add_context({"unittest": None})
        db = magic.SQL_connect(dbfile)
        assert "DB" in magic.Context

        # import
        fLOG("import")
        r = magic.SQL_import_tsv(data)
        tables = magic.SQL_tables()
        assert "velib_vanves" in tables

        # schema
        fLOG("schema")
        sch = magic.SQL_schema("velib_vanves --as_list")
        magic.SQL_refresh_completion()
        fLOG(sch)
        assert sch is not None
        assert sch[0] == ('address', str)

        # view
        fLOG("view")
        head = magic.SQL("--df=dfo", "SELECT * FROM velib_vanves")
        fLOG(head.shape)
        dbo = magic.Context["dfo"]
        fLOG(dbo.shape)

        # view 2
        fLOG("view 2")
        full = magic.SQL('-q "SELECT * FROM velib_vanves"')
        fLOG(full.shape)
        assert full.shape == dbo.shape

        # import df
        fLOG("import df")
        magic.Context["ttt"] = full
        magic.SQL_import_df("ttt")
        tables = magic.SQL_tables()
        assert "ttt" in tables

        # add function
        fLOG("add function")

        def binomiale(p):
            return 1 if random.random() > p else 0
        magic.add_context({"binomiale": binomiale})
        magic.SQL_add_function("binomiale")
        full = magic.SQL(
            '', "SELECT SUM(bin) AS total FROM (SELECT *, binomiale(0.5) AS bin FROM velib_vanves)")
        if full.values[0][0] > 8000:
            raise Exception(str(full.values[0][0]))

        # close
        magic.SQL_drop_table("velib_vanves")
        tables = magic.SQL_tables()
        if "velib_vanves" in tables:
            raise Exception(tables)
        magic.SQL_close()