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_tables.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"""Table extraction: spans, multi-level headers, units, records, merging."""42from spider.extract.tables import detect_unit, extract_tables, merge_tables4344MULTI = """<table id="loyers"><caption>Loyers moyens</caption>45<thead><tr><th rowspan="2">Ville</th><th colspan="2">2024</th><th colspan="2">2025</th></tr>46<tr><th>Q1 ($)</th><th>Q2 ($)</th><th>Q1 ($)</th><th>Q2 ($)</th></tr></thead>47<tbody><tr><th scope="row">Gatineau</th><td>1 200</td><td>1 250</td><td>1 300</td><td>1 350</td></tr>48<tr><th scope="row">Ottawa</th><td>1 800</td><td>1 850</td><td>1 900</td><td>1 950</td></tr></tbody></table>"""495051def test_multi_level_headers_and_spans():52 (table,) = extract_tables(MULTI)53 assert table.caption == "Loyers moyens"54 assert (table.n_rows, table.n_cols) == (4, 5)55 assert table.header_rows == [0, 1] and table.has_spans56 assert table.header_paths == [["Ville"], ["2024", "Q1 ($)"], ["2024", "Q2 ($)"], ["2025", "Q1 ($)"],57 ["2025", "Q2 ($)"]]58 assert table.header_path(2, 1) == ["2024", "Q1 ($)"]59 assert table.units == [None, "$", "$", "$", "$"]60 assert table.row_header_cols == [0] and table.row_headers(3) == ["Ottawa"]61 assert table.selector == "#loyers"626364def test_expanded_grid_keeps_origin():65 (table,) = extract_tables(MULTI)66 ville = table.grid[0][0]67 assert ville is table.grid[1][0] # the rowspan cell fills both header rows68 assert (ville.origin_row, ville.origin_col, ville.rowspan, ville.colspan) == (0, 0, 2, 1)69 year = table.grid[0][2]70 assert year is table.grid[0][1] and (year.origin_col, year.colspan, year.is_header) == (1, 2, True)71 cell = table.grid[2][1]72 assert cell.text == "1 200" and " " in cell.raw and not cell.is_header737475def test_to_records():76 (table,) = extract_tables(MULTI)77 assert table.to_records() == [78 {"Ville": "Gatineau", "2024 / Q1 ($)": "1 200", "2024 / Q2 ($)": "1 250", "2025 / Q1 ($)": "1 300",79 "2025 / Q2 ($)": "1 350"},80 {"Ville": "Ottawa", "2024 / Q1 ($)": "1 800", "2024 / Q2 ($)": "1 850", "2025 / Q1 ($)": "1 900",81 "2025 / Q2 ($)": "1 950"},82 ]838485def test_body_rowspan_and_missing_cells():86 html = """<table><tr><th>Type</th><th>Taille</th><th>Prix</th></tr>87 <tr><td rowspan="2">Studio</td><td>S</td><td>900</td></tr><tr><td>M</td><td>950</td></tr>88 <tr><td>Loft</td></tr></table>"""89 (table,) = extract_tables(html)90 assert table.header_rows == [0]91 assert table.to_rows(include_headers=False) == [["Studio", "S", "900"], ["Studio", "M", "950"], ["Loft", "", ""]]92 assert table.grid[3][1] is None93 assert table.to_records()[1] == {"Type": "Studio", "Taille": "M", "Prix": "950"}949596def test_table_without_header_and_duplicate_keys():97 (table,) = extract_tables("<table><tr><td>a</td><td>b</td></tr></table>")98 assert table.header_rows == [] and table.to_records() == [{"col_1": "a", "col_2": "b"}]99 (table,) = extract_tables("<table><tr><th>X</th><th>X</th></tr><tr><td>1</td><td>2</td></tr></table>")100 assert table.column_keys() == ["X", "X_2"]101102103def test_units_in_headers():104 assert detect_unit("Price ($)") == "$"105 assert detect_unit("Surface (m²)") == "m²"106 assert detect_unit("Superficie (pi²)") == "ft²"107 assert detect_unit("Poids en kg") == "kg"108 assert detect_unit("Prix [CAD]") == "CAD"109 assert detect_unit("Notes (optional)") is None110 assert detect_unit("Ville") is None111112113def test_pathological_spans_are_clamped():114 html = '<table><tr><td rowspan="100000" colspan="100000">x</td><td>y</td></tr><tr><td>z</td></tr></table>'115 (table,) = extract_tables(html)116 assert table.n_rows == 2 and table.n_cols <= 500117 assert extract_tables("<table></table><table><tr></tr></table>") == []118 assert extract_tables('<table><tr><td colspan="abc" rowspan="-3">x</td></tr></table>')[0].n_cols == 1119120121def test_nested_and_hidden_tables():122 html = """<table id="outer"><tr><td><table id="inner"><tr><td>in</td></tr></table></td><td>out</td></tr></table>123 <table hidden><tr><td>no</td></tr></table>"""124 tables = extract_tables(html)125 assert [t.selector for t in tables] == ["#outer", "#inner"]126 assert tables[0].n_cols == 2 and tables[1].to_rows() == [["in"]]127128129PAGE_1 = "<table><thead><tr><th>Nom</th><th>Prix ($)</th></tr></thead><tbody><tr><td>A</td><td>1</td></tr></tbody></table>"130PAGE_2 = """<table><thead><tr><th>Nom</th><th>Prix ($)</th></tr></thead><tbody>131<tr><td>Nom</td><td>Prix ($)</td></tr><tr><td>B</td><td>2</td></tr></tbody></table>"""132OTHER = "<table><tr><th>Ville</th></tr><tr><td>Hull</td></tr></table>"133134135def test_merge_tables_split_across_pages():136 tables = extract_tables(PAGE_1, source="capture-1") + extract_tables(OTHER, source="capture-1") \137 + extract_tables(PAGE_2, source="capture-2")138 merged = merge_tables(tables)139 assert len(merged) == 2140 first = merged[0]141 assert first.to_records() == [{"Nom": "A", "Prix ($)": "1"}, {"Nom": "B", "Prix ($)": "2"}]142 assert first.header_rows == [0] and first.n_rows == 3143 assert [(p["table_index"], p["source"], p["rows"], p["dropped_header_rows"]) for p in first.provenance] == [144 (0, "capture-1", [1, 2], 0), (2, "capture-2", [2, 3], 1)]145 assert merged[1].to_records() == [{"Ville": "Hull"}]146 assert len(tables[0].grid) == 2 # inputs are left untouched147148149def test_merge_headerless_continuation_is_opt_in():150 continuation = "<table><tr><td>C</td><td>3</td></tr></table>"151 tables = extract_tables(PAGE_1) + extract_tables(continuation)152 assert len(merge_tables(tables)) == 2153 (merged,) = merge_tables(tables, allow_headerless_continuation=True)154 assert [r["Nom"] for r in merged.to_records()] == ["A", "C"]155