def test_encrypt_decrypt(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # the module returns the following error # ENCODING ERROR WITH Python 2.7, will not fix it return else: password = "******" * 2 temp = get_temp_folder(__file__, "temp_encrypt") temp2 = get_temp_folder(__file__, "temp_encrypt2") tempmm = get_temp_folder(__file__, "temp_encrypt_status") cstatus = os.path.join(tempmm, "crypt_status.txt") cmap = os.path.join(tempmm, "crypt_map.txt") srcf = os.path.abspath(os.path.join(temp, "..")) sys.argv = ["", srcf, temp, password, "--status", cstatus, "--map", cmap] encrypt(fLOG=fLOG) this = __file__ sys.argv = ["", temp, temp2, password] decrypt(fLOG=fLOG) with open(__file__, "rb") as f: c1 = f.read() with open(os.path.join(temp2, os.path.split(this)[-1]), "rb") as f: c2 = f.read() self.assertEqual(c1, c2) fLOG("end")
def test_compress_7zip(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = get_temp_folder(__file__, "temp_compress_7zip") url = "https://docs.python.org/3.5/library/ftplib.html" f = download(url, fold) if is_travis_or_appveyor() != "travis": out7 = os.path.join(fold, "try.7z") r = zip7_files(out7, [f], fLOG=fLOG, temp_folder=fold) fLOG(r) if not os.path.exists(out7): raise FileNotFoundError(out7) else: warnings.warn( "unzipping files with 7z on a is not tested on appveyor") return if sys.version_info[0] == 2: typbytes = bytearray else: typbytes = bytes if is_travis_or_appveyor() == "appveyor": warnings.warn( "py7zlib.py requires to be updated with the github version") return from py7zlib import COMPRESSION_METHOD_COPY fLOG("***", COMPRESSION_METHOD_COPY) res = un7zip_files(out7) assert isinstance(res, list) self.assertEqual(len(res), 1) fLOG(res[0][0]) if not isinstance(res[0][1], (typbytes, str)): raise TypeError(type(res[0][1])) assert res[0][0].endswith("ftplib.html") fold = get_temp_folder(__file__, "temp_compress_7zip2") res = un7zip_files(out7, where_to=fold, fLOG=fLOG) if sys.version_info[0] == 2: return self.assertEqual(len(res), 1) s = res[0].replace("\\", "/") if not s.endswith("_unittests/ut_filehelper/temp_compress_7zip2/ftplib.html"): raise Exception(res[0])
def test_encrypt_decrypt_file(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # the module returns the following error # ENCODING ERROR WITH Python 2.7, will not fix it return else: password = "******" * 2 temp = get_temp_folder(__file__, "temp_encrypt_file") dest = os.path.join(temp, "__file__.enc") sys.argv = ["", __file__, dest, password] encrypt_file(fLOG=fLOG) dest2 = os.path.join(temp, "__file__.py") sys.argv = ["", dest, dest2, password] decrypt_file(fLOG=fLOG) with open(__file__, "rb") as f: c1 = f.read() with open(dest2, "rb") as f: c2 = f.read() self.assertEqual(c1, c2) fLOG("end")
def test_notebook_runner(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_notebook") nbfile = os.path.join(temp, "..", "data", "simple_example.ipynb") assert os.path.exists(nbfile) addpath = os.path.normpath(os.path.join(temp, "..", "..", "..", "src")) assert os.path.exists(addpath) outfile = os.path.join(temp, "out_notebook.ipynb") assert not os.path.exists(outfile) if sys.version_info[0] == 2: return kernel_name = None if is_travis_or_appveyor() is not None else install_python_kernel_for_unittest( "pyquickhelper") stat, out = run_notebook(nbfile, working_dir=temp, outfilename=outfile, additional_path=[addpath], kernel_name=kernel_name) fLOG(stat) fLOG(out) assert os.path.exists(outfile) assert "No module named 'pyquickhelper'" not in out assert "datetime.datetime(2015, 3, 2" in out
def test_notebook_raw(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # does not work on Python 2 return path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.normpath(os.path.join(path, "data")) nbs = [os.path.join(fold, _) for _ in os.listdir(fold) if "TD_2A" in _] assert len(nbs) > 0 formats = ["latex", "present", "ipynb", "html", "python", "rst", "pdf", "docx"][:1] temp = get_temp_folder(__file__, "temp_nb_bug_raw") if is_travis_or_appveyor() is not None: warnings.warn( "travis, appveyor, unable to test TestNoteBooksBug.test_notebook") return res = process_notebooks(nbs, temp, temp, formats=formats) fLOG("*****", len(res)) for _ in res: fLOG(_) assert os.path.exists(_[0]) check = os.path.join(temp, "TD_2A_Eco_Web_Scraping.tex") with open(check, "r", encoding="utf8") as f: content = f.read() if "\\begin{verbatim" not in content: raise Exception(content)
def test_notebook(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # does not work on Python 2 return path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.normpath(os.path.join(path, "notebooks2")) nbs = [os.path.join(fold, _) for _ in os.listdir(fold) if ".ipynb" in _] formats = ["present", "ipynb", "html", "python", "rst", "pdf", "docx"] temp = get_temp_folder(__file__, "temp_nb_bug2") if is_travis_or_appveyor() is not None: warnings.warn("travis, appveyor, unable to test TestNoteBooksBug.test_notebook") return nbs2 = [] for r in nbs: r2 = os.path.join(temp, os.path.split(r)[-1]) shutil.copy(r, r2) nbs2.append(r2) r = upgrade_notebook(r2) fLOG("change", r) res = process_notebooks(nbs2, temp, temp, formats=formats, exc=False) fLOG("*****", len(res)) for _ in res: fLOG(_) assert os.path.exists(_[0]) check = os.path.join(temp, "miparcours.tex") with open(check, "r", encoding="utf8") as f: content = f.read() assert "\\end{document}" in content
def test_pandas_tennis(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") days = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"] temp = get_temp_folder(__file__, "temp_pandas_tennis") rows = [] dt = datetime.datetime.now() for i in range(0, 360): row = dict(date="%04d-%02d-%02d" % (dt.year, dt.month, dt.day), jour=dt.weekday(), journ=days[dt.weekday()]) dt += datetime.timedelta(1) rows.append(row) df = pandas.DataFrame(rows) df["equipe"] = 0 df.ix[(df.jour == 2) | (df.jour == 4), "equipe"] = 1 df["equipe_sum"] = (df.equipe.cumsum() * df["equipe"] + df["equipe"]) % 3 df["equipe_sum"] += df["equipe"] fLOG(df.head(n=10)) df.to_excel(os.path.join(temp, "tennis.xlsx"), index=False)
def test_rst2html_png_bug(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor(): # it requires latex return if sys.version_info[:2] <= (2, 7): # i don't want to fix it for Python 2.7 return temp = get_temp_folder(__file__, "temp_rst2html_png_latex") rst = os.path.join(os.path.abspath( os.path.dirname(__file__)), "data", "puzzle_girafe.rst") with open(rst, "r", encoding="utf-8") as f: content = f.read() text = rst2html(content, fLOG=fLOG, outdir=temp, imgmath_latex_preamble=""" \\newcommand{\\acc}[1]{\\left\\{#1\\right\\}} \\newcommand{\\cro}[1]{\\left[#1\\right]} \\newcommand{\\pa}[1]{\\left(#1\\right)} \\newcommand{\\girafedec}[3]{ \\begin{array}{ccccc} #1 &=& #2 &+& #3 \\\\ a' &=& a &-& o \\end{array}} \\newcommand{\\vecteur}[2]{\\pa{#1,\\dots,#2}} \\newcommand{\\R}[0]{\\mathbb{R}} \\newcommand{\\N}[0]{\\mathbb{N}} """) # fLOG(text) ji = os.path.join(temp, "out.html") with open(ji, "w", encoding="utf-8") as f: f.write(text)
def test_notebook_runner_empty(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # written in Python 3 return temp = get_temp_folder(__file__, "temp_notebook_empty") nbfile = os.path.join( temp, "..", "data", "td2a_cenonce_session_4B.ipynb") assert os.path.exists(nbfile) addpath = os.path.normpath(os.path.join(temp, "..", "..", "..", "src")) assert os.path.exists(addpath) kernel_name = None if is_travis_or_appveyor() is not None else install_python_kernel_for_unittest( "pyquickhelper") 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], fLOG=fLOG, kernel_name=kernel_name) fLOG(out) assert os.path.exists(outfile) assert "No module named 'pyquickhelper'" not in out
def test_install_revealjs_github(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: return temp = get_temp_folder(__file__, "temp_install_revealjs_github") dest = get_temp_folder(__file__, "temp_install_revealjs_github_dest") fs = download_revealjs(temp, dest, fLOG=fLOG) fLOG(fs) assert len(fs) > 0 for a in fs: assert os.path.exists(a)
def test_url_more_recent(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") this = os.path.abspath(os.path.dirname(__file__)) name = "test_syncho.py" temp = get_temp_folder(__file__, "temp_url_more_recent") shutil.copy(os.path.join(this, name), temp) dest = os.path.join(temp, name) url = "https://github.com/sdpython/pyquickhelper/tree/master/_unittests/ut_loghelper/" + name f1 = URL.urlopen(url) r = _first_more_recent(f1, dest) f1.close() assert isinstance(r, bool) url = "http://www.lemonde.fr/" f1 = URL.urlopen(url) r = _first_more_recent(f1, dest) f1.close() assert isinstance(r, bool) fLOG(r)
def test_download_zip(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = get_temp_folder(__file__, "temp_download") url = "https://docs.python.org/3.5/library/ftplib.html" f = download(url, fold) fLOG(f) assert os.path.exists(f) if not f.endswith("ftplib.html"): raise Exception(f) out = os.path.join(fold, "try.html.gz") r = gzip_files(out, [f], fLOG=fLOG) fLOG(r) assert os.path.exists(out) out = os.path.join(fold, "try.zip") r = zip_files(out, [f], fLOG=fLOG) fLOG(r) assert os.path.exists(out) if is_travis_or_appveyor() is None: out7 = os.path.join(fold, "try.7z") r = zip7_files(out7, [f, out], fLOG=fLOG, temp_folder=fold) fLOG(r) if not os.path.exists(out7): raise FileNotFoundError(out7) else: fLOG("skip 7z")
def test_post_list(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") # the test will fail if you add a file in data/blog others # with rst files which is not a blog post directives.register_directive("blogpost", BlogPostDirective) path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.join(path, "data", "blog") out = get_temp_folder(__file__, "temp_post_list") p = BlogPostList(fold) cats = p.get_categories() fLOG(cats) months = p.get_months() fLOG(months) assert cats == ['documentation', 'example'] assert months == ['2015-04'] res = p.write_aggregated(out) assert len(res) >= 4 for r in res: assert os.path.exists(r)
def test_jenkins(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_jenkins_yml") this = os.path.abspath(os.path.dirname(__file__)) yml = os.path.abspath(os.path.join( this, "..", "..", ".local.jenkins.win.yml")) if not os.path.exists(yml): raise FileNotFoundError(yml) context = dict(Python34="c:\\Python34_x64", Python35=os.path.dirname(sys.executable), Python36=os.path.dirname(sys.executable), Python27="c:\\Python27_x64", Anaconda3="c:\\Anaconda3", Anaconda2="c:\\Anaconda2", WinPython35="c:\\PythonENSAE", root_path="d:\\jenkins\\yml") git_repo = "https://github.com/sdpython/pyquickhelper.git" srv = JenkinsExt("http://localhost:8080/", "user", "password", mock=True, fLOG=fLOG, engines=context) for i, tuconv in enumerate(enumerate_processed_yml(yml, server=srv, context=context, git_repo=git_repo, yml_platform="win")): conv, name, var = tuconv c = "conda" if "c:\\Anaconda" in conv else 'win' with open(os.path.join(temp, "yml-%s-%d.xml" % (c, i)), "w") as f: f.write(conv)
def test_notebook_iter(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # written in Python 3 return temp = get_temp_folder(__file__, "temp_notebook_add_slides_metadata") nbfile = os.path.join( temp, "..", "data", "having_a_form_in_a_notebook.ipynb") assert os.path.exists(nbfile) nb = read_nb(nbfile, kernel=False) for i, cell in enumerate(nb.iter_cells()): ls = nb.cell_height(cell) #fLOG(i,"-",nb.cell_type(cell), l) #if l > 400: fLOG(cell) assert ls > 0 return new_tags = nb.add_tag_slide() for k, v in sorted(new_tags.items()): a, b, c = v fLOG(k, a, b) assert len(new_tags) > 0
def test_newdirective_with_rst2html(self): """ this test also test the extension runpython """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: warnings.warn( "test_newdirective_with_rst2html not run on Python 2.7") return from docutils import nodes class runpythonthis_node(nodes.Structural, nodes.Element): pass class RunPythonThisDirective (RunPythonDirective): runpython_class = runpythonthis_node def visit_rp_node(self, node): self.body.append("<p><b>visit_rp_node</b></p>") def depart_rp_node(self, node): self.body.append("<p><b>depart_rp_node</b></p>") content = """ test a directive ================ .. runpythonthis:: print(u"this code shoud appear" + u"___") """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node, visit_rp_node, depart_rp_node)] html = rst2html(content, fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives) ta = "<p><b>visit_rp_node</b></p>" if ta not in html: raise Exception(html) tb = "<p><b>depart_rp_node</b></p>" if tb not in html: raise Exception(html) t1 = "this code shoud appear___".split() for t in t1: if t not in html: temp = get_temp_folder( __file__, "temp_newdirective_with_rst2html") with open(os.path.join(temp, "bug.html"), "w", encoding="utf8") as f: f.write(html) raise Exception(html)
def test_convert_slides_api(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: return path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.normpath( os.path.join( path, "..", "..", "_doc", "notebooks")) nb = os.path.join(fold, "example_pyquickhelper.ipynb") assert os.path.exists(nb) nbr = read_nb(nb, kernel=False) temp = get_temp_folder(__file__, "temp_nb_api") outfile = os.path.join(temp, "out_nb_slides.slides.html") res = nb2slides(nbr, outfile) assert len(res) > 1 for r in res: assert os.path.exists(r) outfile = os.path.join(temp, "out_nb_slides.html") res = nb2html(nbr, outfile) assert len(res) == 1 for r in res: assert os.path.exists(r)
def test_notebook_gallery(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: return temp = get_temp_folder(__file__, "temp_gallery") fold = os.path.normpath(os.path.join( temp, "..", "data_gallery", "notebooks")) assert os.path.exists(fold) file = os.path.join(temp, "all_notebooks.rst") build_notebooks_gallery(fold, file, fLOG=fLOG) if not os.path.exists(file): raise FileNotFoundError(file) with open(file, "r", encoding="utf8") as f: text = f.read() if "GitHub/pyquickhelper" in text.replace("\\", "/"): raise Exception(text) spl = text.split("notebooks/td2a_eco_competition_modeles_logistiques") if len(spl) != 3: raise Exception(text)
def test_notebook_runner_operation(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: # written in Python 3 return temp = get_temp_folder(__file__, "temp_notebook_operation") nbfile = os.path.join(temp, "..", "data", "simple_example.ipynb") nbfile2 = os.path.join( temp, "..", "data", "td2a_cenonce_session_4B.ipynb") nb1 = read_nb(nbfile, kernel=False) n1 = len(nb1) nb2 = read_nb(nbfile2, kernel=False) n2 = len(nb2) add = nb1 + nb2 nb1.merge_notebook([nb2]) n3a = len(add) n3 = len(nb1) if n1 + n2 != n3: raise Exception("{0} + {1} != {2}".format(n1, n2, n3)) if n3a != n3: raise Exception("{0} != {1}".format(n3a, n3)) fLOG(n1, n2, n3, n3a) outfile = os.path.join(temp, "merge_nb.ipynb") nb1.to_json(outfile) assert os.path.exists(outfile) outfile = os.path.join(temp, "merge_nb_add.ipynb") add.to_json(outfile) assert os.path.exists(outfile)
def test_clean_pep8(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_pep8_clean") name = os.path.join(temp, "python_try.py") with open(name, "w") as f: f.write(""" import sys import os def f1 () : #g return [2,3] """.replace(" ", "")) r = remove_extra_spaces_and_pep8(name) assert r > 0 with open(name, "r") as f: content = f.read() self.assertEqual(content.strip(), """ import sys import os def f1(): # g return [2, 3] """.replace(" ", "").strip()) r = remove_extra_spaces_and_pep8(name) self.assertEqual(r, 0)
def test_bigger(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: warnings.warn( "test_biffer not run on Python 2.7") return from docutils import nodes as skip_ content = """ test a directive ================ before :bigger:`facebook` after this code shoud appear """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') html = rst2html(content, fLOG=fLOG, writer="custom", keep_warnings=True, directives=None) t1 = "this code shoud not appear" if t1 in html: raise Exception(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "facebook" if t1 not in html: raise Exception(html) t1 = "linkedin" if t1 in html: raise Exception(html) t1 = '{1}' if t1 in html: raise Exception(html) t1 = "visit_sharenet_node" if t1 in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_bigger") with open(os.path.join(temp, "out_bigger.html"), "w", encoding="utf8") as f: f.write(html)
def test_notebook_thumbnail(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: warnings.warn("Not working with python 2.7") return temp = get_temp_folder(__file__, "temp_notebook_number") nbfile = os.path.join(temp, "..", "data", "pyensae_text2table.ipynb") assert os.path.exists(nbfile) fLOG("reading", nbfile) nb = read_nb(nbfile) fLOG("creating thumbnail") image = nb.get_thumbnail() fLOG(type(image)) temp = get_temp_folder(__file__, "temp_notebook_thumbnail") name = os.path.join(temp, "pyensae_text2table.thumb.png") fLOG("saving") image.save(name) assert os.path.exists(name) nbfile = os.path.join(temp, "..", "data", "notebook_with_svg.ipynb") assert os.path.exists(nbfile) fLOG("reading", nbfile) nb = read_nb(nbfile) fLOG("creating thumbnail") image = nb.get_thumbnail() name = os.path.join(temp, "notebook_with_svg.thumb.svg") fLOG("saving") with open(name, "w", encoding="utf-8") as f: f.write(image) assert os.path.exists(name) nbfile = os.path.join(temp, "..", "data", "example_corrplot.ipynb") assert os.path.exists(nbfile) fLOG("reading", nbfile) nb = read_nb(nbfile) fLOG("creating thumbnail") image = nb.get_thumbnail() name = os.path.join(temp, "example_corrplot.thumb.png") fLOG("saving") image.save(name) assert os.path.exists(name)
def test_patch_installation(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_patch_installation") copied = fix_python35_dll(sys.prefix, temp, force=True) for c in copied: fLOG("copied", os.path.split(c)[-1])
def test_todoext_done(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. todoext:: :title: first todo :tag: bug :issue: 7 :hidden: this code shoud appear___ after """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("todoext", TodoExt, todoext_node, visit_todoext_node, depart_todoext_node)] html = rst2html(content, fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives, extlinks={'issue': ('http://%s', '_issue_')}) temp = get_temp_folder(__file__, "temp_todoext") with open(os.path.join(temp, "out_todoext.html"), "w", encoding="utf8") as f: f.write(html) t1 = "this code shoud appear" if t1 in html: raise Exception(html) t1 = "after" if t1 not in html: raise Exception(html) t1 = "first todo" if t1 in html: raise Exception(html) t1 = "(bug)" if t1 in html: raise Exception(html) t1 = 'href="http://7"' if t1 in html: raise Exception(html)
def test_exreflist(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. exref:: :title: first todo :tag: freg :lid: id3 this code shoud appear___ middle .. exreflist:: :tag: freg :sort: title after """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("exref", ExRef, exref_node, visit_exref_node, depart_exref_node)] html = rst2html(content, fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives) if "admonition-exref exref_node admonition" not in html: raise html temp = get_temp_folder(__file__, "temp_exreflist") with open(os.path.join(temp, "out_exref.html"), "w", encoding="utf8") as f: f.write(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "after" if t1 not in html: raise Exception(html) t1 = "first todo" if t1 not in html: raise Exception(html)
def test_backup_file(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: return try: import Crypto as skip__ algo = "AES" except ImportError: algo = "fernet" temp = get_temp_folder(__file__, "temp_backup_files_file") root = os.path.normpath(os.path.join(temp, "..")) fLOG(root) api = TransferAPIFile(os.path.join(temp, "backup")) ft = FileTreeNode(root, filter=".*[.]py", repository=False) enc = EncryptedBackup( key=b"unit" * 8, file_tree_node=ft, transfer_api=api, file_status=os.path.join(temp, "status.txt"), file_map=os.path.join(temp, "mapping.txt"), root_local=os.path.join(temp, "..", ".."), threshold_size=2000, fLOG=fLOG, algo=algo) done, issue = enc.start_transfering() assert len(done) > 0 assert len(issue) == 0 for k, v in sorted(enc.Mapping.items()): fLOG(k, len(v.pieces), v) enc.load_mapping() outfile = os.path.join(temp, "backed_test_backup_file.py") fpth = "ut_filehelper\\test_backup_file.py" if not sys.platform.startswith("win"): fpth = fpth.replace("\\", "/") s = enc.retrieve(fpth, filename=outfile) with open(outfile, "r") as f: c2 = f.read() with open(__file__.replace(".pyc", ".py"), "r") as f: c1 = f.read() self.assertEqual(c1, c2) detemp = os.path.join(temp, "retrieved") s = enc.retrieve_all(detemp, regex=".*[.]py") assert len(s) > 0
def test_file_head(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_" + self._testMethodName) out = os.path.join(temp, "out.5.py") assert not os.path.exists(out) head = file_head(__file__.replace(".pyc", ".py"), out=out, head=5) assert os.path.exists(head)
def test_download(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_download") url = "http://www.xavierdupre.fr/enseignement/complements/added.zip" down = download(url, temp) assert os.path.exists(down) assert down.endswith("added.txt")
def test_html_in_frame(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") ht = html_in_frame("<h1>title</h1>") body = "<html><body>" + ht + "</body></html>" assert "data:text/html;base64,PGgxPnRpdGxlPC9oMT4=" in ht temp = get_temp_folder(__file__, "temp_html_in_frame") tempf = os.path.join(temp, "out.html") with open(tempf, "w") as f: f.write(body)
def test_sharenet_inline(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[0] == 2: warnings.warn( "test_sharenet not run on Python 2.7") return from docutils import nodes as skip_ content = """ test a directive ================ abeforea :sharenet:`facebook-linkedin-twitter-20-body` aaftera """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') html = rst2html(content, fLOG=fLOG, writer="custom", keep_warnings=True, directives=None) t1 = "abeforea" if t1 not in html: raise Exception(html) t1 = "aftera" if t1 not in html: raise Exception(html) t1 = "function share_icon(divid, text)" if t1 not in html: raise Exception(html) t1 = '<a href="#"' if t1 not in html: raise Exception(html) t1 = '{1}' if t1 in html: raise Exception(html) t1 = "visit_sharenet_node" if t1 in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_sharenet_inline") with open(os.path.join(temp, "out_sharenet.html"), "w", encoding="utf8") as f: f.write(html)