c84decca8d
Feature/rework table handling * test: add cases for configuration boundary * test: add table post-processing tests (cases to ensure preservation of post-table text and metadata during table operations) * test: enhance table handling tests for small and large tables, ensuring proper suffixing and splitting behavior * Fix and clarify intent in comments * fix: enhance handling of post-table text when combining tables * refactor: remove commented-out code in clean_tables function for clarity * test: update post-table text preservation assertions and add debug tracing * refactor: remove unused remove_table_from_page function and its test * refactor: enhance combine method documentation and clarify split_by_size_with_smart_tables logic * Merge remote-tracking branch 'origin/main' into feature/rework-table-handling * refactor: reduce large table threshold from 1000 to 50 rows for better table splitting * Add simple splitting * refactor: uncomment dynamic answer retrieval in run_one_to_n_prompts for improved functionality * Merge remote-tracking branch 'origin/main' into feature/rework-table-handling * refactor: remove unused table handling functions and related constants for cleaner code * refactor: remove unused test functions in preparation for new tests * refactor: streamline continuation table handling by utilizing existing flags * refactor: update table combination logic to include all tables from the main page * refactor: enhance table handling to automatically resolve column mismatches, respect row limits, and improve unit/integration test coverage * rename clean_tables_simple to clean_tables * Update docstrings * refactor: rename table handling functions * Fix indentation and add tests * isort, black * Merge remote-tracking branch 'origin/main' into feature/rework-table-handling Approved-by: Katon Minhas
131 lines
5.2 KiB
Python
131 lines
5.2 KiB
Python
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from src.investment.preprocess import clean_tables
|
|
|
|
|
|
class TestPreprocessingIntegration(unittest.TestCase):
|
|
|
|
def test_clean_tables_simple_end_to_end(self):
|
|
"""Test the complete simplified table cleaning pipeline."""
|
|
text_dict = {
|
|
"1": "Metadata\n-------Table Start--------\nMain Table\n[['A', 'B']] + [[f'Row{i}', str(i)] for i in range(1, 8)]\n-------Table End--------",
|
|
"2": "-------Table Start--------\n\n[['Row8', '8'], ['Row9', '9']]\n-------Table End--------", # Continuation
|
|
"3": "Text without tables",
|
|
}
|
|
|
|
# This should: 1) Combine continuations, 2) Split by row count
|
|
result = clean_tables(text_dict, "test_file.txt")
|
|
|
|
# Should have combined page 2 into page 1, then split the long table
|
|
self.assertNotIn("2", result) # Continuation removed
|
|
self.assertIn("3", result) # Non-table page preserved
|
|
# Should have split pages like "1.0", "1.1", etc.
|
|
|
|
def test_clean_tables_small_table_gets_dot_zero_suffix(self):
|
|
"""Test that pages with small tables get .0 suffix but aren't split."""
|
|
text_dict = {
|
|
"15": "Rate Table\n-------Table Start--------\nRate Table\n[['HCPC', 'Rate'], ['T1019', '$5.00']]\n-------Table End--------"
|
|
}
|
|
|
|
result = clean_tables(text_dict, "test_file.pdf")
|
|
|
|
# Should get .0 suffix because it contains tables
|
|
self.assertIn("15.0", result)
|
|
self.assertNotIn("15", result)
|
|
|
|
# Content should be preserved
|
|
self.assertIn("T1019", result["15.0"])
|
|
|
|
# Should not be split (small table)
|
|
chunk_keys = [k for k in result.keys() if k.startswith("15.")]
|
|
self.assertEqual(len(chunk_keys), 1)
|
|
|
|
def test_clean_tables_continuation_gets_combined_and_dot_zero(self):
|
|
"""Test that continuations get combined and result gets .0 suffix."""
|
|
text_dict = {
|
|
"1": "Rate Table\n-------Table Start--------\nRate Table Header\n[['HCPC', 'Rate'], ['T1019', '$5.00']]\n-------Table End--------",
|
|
"2": "-------Table Start--------\n\n[['T1021', '$4.00']]\n-------Table End--------", # Continuation
|
|
}
|
|
|
|
result = clean_tables(text_dict, "test_file.pdf")
|
|
|
|
# Should combine and get .0 suffix since contains tables
|
|
self.assertIn("1.0", result)
|
|
self.assertNotIn("1", result)
|
|
self.assertNotIn("2", result) # Absorbed into Page 1
|
|
|
|
# Verify combined content
|
|
self.assertIn("T1019", result["1.0"])
|
|
self.assertIn("T1021", result["1.0"])
|
|
|
|
def test_clean_tables_large_table_row_based_splitting(self):
|
|
"""Test that large tables get split based on row count."""
|
|
# Create a table with many rows that will exceed TABLE_ROW_LIMIT
|
|
large_table_rows = [["HCPC", "Rate"]] + [
|
|
[f"T{i:04d}", f"${i}.00"] for i in range(60)
|
|
] # 60 data rows
|
|
text_dict = {
|
|
"31": f"EXHIBIT F\n-------Table Start--------\nPCAP Rates\n{large_table_rows}\n-------Table End--------\nPost-table text"
|
|
}
|
|
|
|
result = clean_tables(text_dict, "test_file.pdf")
|
|
|
|
# Should create multiple chunks due to row limit (default 50)
|
|
chunk_keys = [k for k in result.keys() if k.startswith("31.")]
|
|
self.assertGreater(
|
|
len(chunk_keys),
|
|
1,
|
|
f"Expected multiple chunks for 60 rows, got: {chunk_keys}",
|
|
)
|
|
|
|
# Original page should be gone
|
|
self.assertNotIn("31", result)
|
|
|
|
# All table data should be preserved somewhere
|
|
all_content = "".join(result.values())
|
|
self.assertIn("T0000", all_content)
|
|
self.assertIn("T0059", all_content)
|
|
self.assertIn("EXHIBIT F", all_content)
|
|
|
|
# Post-table text should be preserved on last chunk
|
|
last_chunk = max(chunk_keys)
|
|
self.assertIn("Post-table text", result[last_chunk])
|
|
|
|
def test_clean_tables_no_tables_unchanged(self):
|
|
"""Test that pages without tables remain unchanged."""
|
|
text_dict = {
|
|
"5": "This is just text content with no tables.",
|
|
"6": "More text content without any table markers.",
|
|
}
|
|
|
|
result = clean_tables(text_dict, "test_file.pdf")
|
|
|
|
# Should remain unchanged (no .0 suffix)
|
|
assert "5" in result
|
|
assert "6" in result
|
|
assert "5.0" not in result
|
|
assert "6.0" not in result
|
|
|
|
# Content should be identical
|
|
assert result["5"] == text_dict["5"]
|
|
assert result["6"] == text_dict["6"]
|
|
|
|
def test_clean_tables_multiple_tables_same_page(self):
|
|
"""Test that multiple tables on same page get .0 suffix but stay together."""
|
|
text_dict = {
|
|
"1": "Page metadata\n-------Table Start--------\nFirst Table\n[['Col1', 'Col2'], ['A', 'B']]\n-------Table End--------\n\nSome text\n-------Table Start--------\nSecond Table\n[['X', 'Y'], ['1', '2']]\n-------Table End--------"
|
|
}
|
|
|
|
result = clean_tables(text_dict, "test_file.pdf")
|
|
|
|
# Should get .0 suffix because it contains tables
|
|
assert "1.0" in result
|
|
assert "1" not in result
|
|
|
|
# Both tables should be present in single chunk
|
|
assert "First Table" in result["1.0"]
|
|
assert "Second Table" in result["1.0"]
|