马一丁

Feature-Test RE Sanitization

  1 +import unittest
  2 +
  3 +from ReportEngine.ir import IRValidator
  4 +from ReportEngine.nodes.chapter_generation_node import ChapterGenerationNode
  5 +
  6 +
  7 +class ChapterSanitizationTestCase(unittest.TestCase):
  8 + """Lightweight regression tests for the chapter sanitization helpers."""
  9 +
  10 + def setUp(self):
  11 + self.node = ChapterGenerationNode(llm_client=None, validator=IRValidator(), storage=None)
  12 +
  13 + def test_table_cell_empty_blocks_repaired(self):
  14 + chapter = {
  15 + "blocks": [
  16 + {
  17 + "type": "table",
  18 + "rows": [
  19 + {
  20 + "cells": [
  21 + {"blocks": []},
  22 + {"text": "同比变化", "blocks": None},
  23 + ]
  24 + }
  25 + ],
  26 + }
  27 + ]
  28 + }
  29 + self.node._sanitize_chapter_blocks(chapter)
  30 + table_block = chapter["blocks"][0]
  31 + cells = table_block["rows"][0]["cells"]
  32 + self.assertEqual(len(cells), 2)
  33 + for cell in cells:
  34 + blocks = cell.get("blocks")
  35 + self.assertIsInstance(blocks, list)
  36 + self.assertGreater(len(blocks), 0)
  37 + for block in blocks:
  38 + self.assertEqual(block.get("type"), "paragraph")
  39 +
  40 + def test_table_rows_scalar_values_expanded(self):
  41 + chapter = {"blocks": [{"type": "table", "rows": ["全国趋势"]}]}
  42 + self.node._sanitize_chapter_blocks(chapter)
  43 + table_block = chapter["blocks"][0]
  44 + self.assertEqual(len(table_block["rows"]), 1)
  45 + row = table_block["rows"][0]
  46 + self.assertIn("cells", row)
  47 + self.assertEqual(len(row["cells"]), 1)
  48 + cell = row["cells"][0]
  49 + self.assertIsInstance(cell.get("blocks"), list)
  50 + self.assertEqual(
  51 + cell["blocks"][0]["inlines"][0]["text"],
  52 + "全国趋势",
  53 + )
  54 +
  55 +
  56 +if __name__ == "__main__":
  57 + unittest.main()