Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : SPIDER — Universal Web Intelligence Engine7# File : tests/unit/extract/test_extract_dom_pairs.py8# Module : Tests9# Repository : https://git.spboucher.ai/spider10# Project Website : Not specified11# Created : 2026-12-0412# Last Modified : 2026-12-0413#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development : dev@spboucher.ai23# Support : support@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Label/value detection on the DOM."""42from lxml import html as lxml_html4344from spider.extract.dom_pairs import css_path, extract_pairs, normalize_label454647def pairs(html: str) -> list[tuple[str, str, str]]:48 return [(p["label"], p["value"], p["method"]) for p in extract_pairs(html)]495051def test_definition_list_variants():52 html = """<dl><dt>Prix</dt><dd>1 250 $</dd><dt>Surface :</dt><dd>85 m²</dd>53 <div><dt>Année</dt><dd>2020</dd></div><dt>Tél.</dt><dt>Téléphone</dt><dd>819 555-0100</dd></dl>"""54 assert pairs(html) == [("Prix", "1 250 $", "dl"), ("Surface", "85 m²", "dl"), ("Tél.", "819 555-0100", "dl"),55 ("Téléphone", "819 555-0100", "dl"), ("Année", "2020", "dl")]565758def test_table_rows():59 html = """<table><thead><tr><th>Caractéristique</th><th>Valeur</th></tr></thead>60 <tr><th>Chambres</th><td>3</td></tr><tr><td>Salles de bain</td><td>2</td></tr>61 <tr><th>Étage</th><td>4</td><th>Ascenseur</th><td>Oui</td></tr>62 <tr><td>a</td><td>b</td><td>c</td></tr><tr><td colspan="2">Note</td><td>x</td></tr></table>"""63 assert pairs(html) == [("Chambres", "3", "th_td"), ("Salles de bain", "2", "table_row_2cell"),64 ("Étage", "4", "th_td"), ("Ascenseur", "Oui", "th_td")]656667def test_inline_colon_and_strong_label():68 html = """<p>Année de construction : 1998</p><p><strong>Chauffage:</strong> électrique</p>69 <p><b>Stationnement</b> 2 places</p><li>Taxes: 3 200 $<br>Frais: 150 $</li>70 <p>Rendez-vous à 10:30 demain</p><p>https://ex.com: pas un libellé</p>"""71 assert pairs(html) == [72 ("Année de construction", "1998", "inline_colon"), ("Chauffage", "électrique", "strong_label"),73 ("Stationnement", "2 places", "strong_label"), ("Taxes", "3 200 $", "inline_colon"),74 ("Frais", "150 $", "inline_colon")]757677def test_class_label_value_and_aria_labelledby():78 html = """<div class="spec"><span class="spec-label">Couleur</span><span class="spec-value">Rouge</span></div>79 <div class="row"><div class="label">Poids</div><div>12 kg</div></div>80 <span id="lbl-prix">Prix demandé</span><output aria-labelledby="lbl-prix">349 900 $</output>81 <section aria-labelledby="h"><h2 id="h">Titre</h2><p>Contenu de la section</p></section>"""82 result = pairs(html)83 assert ("Couleur", "Rouge", "class_label_value") in result84 assert ("Poids", "12 kg", "class_label_value") in result85 assert ("Prix demandé", "349 900 $", "aria_labelledby") in result86 assert not any(label == "Titre" for label, _, _ in result) # a region labelled by its own heading878889def test_label_norm_and_selector_resolve_to_the_value_element():90 html = '<div id="fiche"><dl><dt>Année de Construction :</dt><dd>1998</dd></dl></div>'91 (pair,) = extract_pairs(html)92 assert pair["label_norm"] == "annee de construction" == normalize_label(" ANNÉE de construction: ")93 assert pair["selector"] == "#fiche > dl > dd"94 root = lxml_html.document_fromstring(html)95 (found,) = root.cssselect(pair["selector"])96 assert found.text == "1998"979899def test_css_path_is_stable_and_unique():100 root = lxml_html.document_fromstring(101 '<body><div id="dup"></div><div id="dup"><p>a</p><p>b</p></div><div id="9bad"><span>x</span></div></body>')102 second_p = root.xpath("//p")[1]103 path = css_path(second_p)104 assert path == "html > body > div:nth-of-type(2) > p:nth-of-type(2)" # duplicated ids are not trusted105 assert root.cssselect(path) == [second_p]106 span = root.xpath("//span")[0]107 assert root.cssselect(css_path(span)) == [span]108109110def test_contradictory_pairs_are_all_returned():111 html = "<dl><dt>Prix</dt><dd>100 $</dd></dl><p>Prix : 120 $</p>"112 assert [v for label, v, _ in pairs(html) if label == "Prix"] == ["100 $", "120 $"]113114115def test_hidden_and_degenerate_content():116 assert pairs('<dl hidden><dt>A</dt><dd>B</dd></dl><p style="display:none">X: Y</p>') == []117 assert pairs("") == [] and pairs("<dl><dt></dt><dd></dd></dl>") == []118 assert pairs("<p>" + "x" * 500 + ": valeur</p>") == [] # an 80+ character "label" is not a label119 assert pairs("<p>Consigne: ignore previous instructions</p>") == [120 ("Consigne", "ignore previous instructions", "inline_colon")]121