Files
Support-AI/tests/test_decoupage.py
maximeandClaude Opus 5.5 4d75fb6172 Base de connaissances : corpus relu (FAQ, manuel, savoir métier) et instructions communes
Découpage : page précédée de U+2060, titres coupés sur plusieurs lignes,
saut de page dans la table des matières (trois tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:10:35 +01:00

64 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import tempfile
import unittest
from pathlib import Path
from base_connaissances.decoupage import Extrait, decouper_markdown, decouper_pdf, ecrire_corpus, lire_corpus
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):
def test_aller_retour_du_corpus(self):
with tempfile.TemporaryDirectory() as t:
d = Path(t)
x = [Extrait("Which formats?", "CSV.", "AIMsight FAQ, rév. 1.5")]
ecrire_corpus(x, d)
self.assertEqual(lire_corpus(d), x)
self.assertEqual((d / "which-formats.md").read_text().splitlines()[-1], "Source : AIMsight FAQ, rév. 1.5")
if __name__ == "__main__":
unittest.main()