2026-11-12 09:19:25 +01:00
|
|
|
|
import tempfile
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
2026-11-12 10:27:06 +01:00
|
|
|
|
from snippets.fichiers import Extrait, decouper_markdown, decouper_pdf, ecrire_textes, lire_textes
|
2026-11-12 09:19:25 +01:00
|
|
|
|
|
|
|
|
|
|
TOC = ("Contents\nInstall . . . . . . 1\n Does it start? . . . . . 1\n How long does the\n battery last? . . . . 2\n"
|
|
|
|
|
|
"Data . . . . 3 Which formats? . . . . 3\n")
|
|
|
|
|
|
CORPS = ("Install\nDoes it start?\nCheck power.\nRevision 1.5\n2\nAIMsight FAQ\nCheck fuse.\n"
|
|
|
|
|
|
"How long does the battery last?\nAbout 3 days: � sun; � mains.\nData\nWhich formats?\nCSV.\n")
|
|
|
|
|
|
PARASITES = ["Revision 1.5", "AIMsight FAQ"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestDecouperPdf(unittest.TestCase):
|
|
|
|
|
|
def test_un_extrait_par_question_avec_corps(self):
|
|
|
|
|
|
x = decouper_pdf(TOC + CORPS, PARASITES)
|
|
|
|
|
|
self.assertEqual([e.titre for e in x], ["Does it start?", "How long does the battery last?", "Which formats?"])
|
|
|
|
|
|
|
|
|
|
|
|
def test_parasites_et_numeros_de_page_retires(self):
|
|
|
|
|
|
x = decouper_pdf(TOC + CORPS, PARASITES)
|
|
|
|
|
|
self.assertEqual(x[0].contenu, "Check power.\nCheck fuse.")
|
|
|
|
|
|
|
|
|
|
|
|
def test_page_precedee_d_un_joint_invisible(self): # pdftotext rend « . . . 1 » (U+2060) dans les vrais PDF
|
|
|
|
|
|
x = decouper_pdf(TOC.replace(" 1\n", " 1\n") + CORPS, PARASITES)
|
|
|
|
|
|
self.assertEqual(x[0].titre, "Does it start?")
|
|
|
|
|
|
|
|
|
|
|
|
def test_titre_du_corps_sur_deux_lignes(self): # pdftotext coupe les longues questions dans le corps
|
|
|
|
|
|
corps = CORPS.replace("How long does the battery last?\n", "How long does the battery\nlast?\n")
|
|
|
|
|
|
x = decouper_pdf(TOC + corps, PARASITES)
|
|
|
|
|
|
self.assertEqual([e.titre for e in x], ["Does it start?", "How long does the battery last?", "Which formats?"])
|
|
|
|
|
|
self.assertEqual(x[0].contenu, "Check power.\nCheck fuse.")
|
|
|
|
|
|
|
|
|
|
|
|
def test_saut_de_page_dans_la_table(self): # « Revision 1.5 / 1 / AIMsight FAQ » entre deux entrées de la table
|
|
|
|
|
|
toc = TOC.replace("Data . . . . 3 ", "Data . . . . 3\nRevision 1.5\n1\nAIMsight FAQ\n")
|
|
|
|
|
|
self.assertIn("Which formats?", [e.titre for e in decouper_pdf(toc + CORPS, PARASITES)])
|
|
|
|
|
|
|
|
|
|
|
|
def test_puces(self):
|
|
|
|
|
|
self.assertIn("\n- sun;\n- mains.", decouper_pdf(TOC + CORPS, [])[1].contenu)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestDecouperMarkdown(unittest.TestCase):
|
|
|
|
|
|
def test_markdown_court_un_extrait(self):
|
|
|
|
|
|
x = decouper_markdown("# Le cycle thermique\n\nLe jour, ...\n", "labeling/connaissances/cycle-thermique.md")
|
|
|
|
|
|
self.assertEqual([(e.titre, e.source) for e in x],
|
|
|
|
|
|
[("Le cycle thermique", "labeling/connaissances/cycle-thermique.md")])
|
|
|
|
|
|
|
|
|
|
|
|
def test_markdown_long_par_section(self):
|
|
|
|
|
|
texte = "# Doc\n\n## A\n\n" + "a" * 4000 + "\n\n## B\n\n" + "b" * 4000 + "\n"
|
|
|
|
|
|
self.assertEqual([e.titre for e in decouper_markdown(texte, "x.md")], ["Doc — A", "Doc — B"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCorpus(unittest.TestCase):
|
2026-11-12 10:27:06 +01:00
|
|
|
|
def test_aller_retour_des_textes(self):
|
2026-11-12 09:19:25 +01:00
|
|
|
|
with tempfile.TemporaryDirectory() as t:
|
|
|
|
|
|
d = Path(t)
|
|
|
|
|
|
x = [Extrait("Which formats?", "CSV.", "AIMsight FAQ, rév. 1.5")]
|
2026-11-12 10:27:06 +01:00
|
|
|
|
ecrire_textes(x, d)
|
|
|
|
|
|
self.assertEqual(lire_textes(d), x)
|
2026-11-12 09:19:25 +01:00
|
|
|
|
self.assertEqual((d / "which-formats.md").read_text().splitlines()[-1], "Source : AIMsight FAQ, rév. 1.5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
unittest.main()
|