scan, split, OCR, prepare for LLM
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

test_markdown.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from __future__ import annotations
  2. from pathlib import Path
  3. from src.markdown.generator import (
  4. _validate_dollar_signs,
  5. _validate_latex_braces,
  6. _validate_left_right,
  7. generate_markdown,
  8. validate_markdown,
  9. )
  10. from src.ocr.paddle_engine import ParsedBlock
  11. class TestValidateBraces:
  12. def test_balanced(self) -> None:
  13. assert _validate_latex_braces(r"a{b}c") == []
  14. def test_unbalanced_open(self) -> None:
  15. warnings = _validate_latex_braces(r"a{b")
  16. assert len(warnings) > 0
  17. def test_unbalanced_close(self) -> None:
  18. warnings = _validate_latex_braces(r"a}b")
  19. assert len(warnings) > 0
  20. def test_nested_balanced(self) -> None:
  21. assert _validate_latex_braces(r"{a{b}c}") == []
  22. def test_complex_formula(self) -> None:
  23. assert _validate_latex_braces(r"\frac{a}{b}") == []
  24. class TestValidateDollar:
  25. def test_balanced_inline(self) -> None:
  26. assert _validate_dollar_signs("$a + b$") == []
  27. def test_balanced_display(self) -> None:
  28. assert _validate_dollar_signs(r"$$a + b$$") == []
  29. def test_unbalanced_inline(self) -> None:
  30. warnings = _validate_dollar_signs("$a + b")
  31. assert len(warnings) > 0
  32. def test_balanced_multiple(self) -> None:
  33. assert _validate_dollar_signs("$x$ and $y$") == []
  34. def test_unbalanced_multiple(self) -> None:
  35. warnings = _validate_dollar_signs("$x and $y$")
  36. assert len(warnings) > 0
  37. class TestValidateLeftRight:
  38. def test_balanced(self) -> None:
  39. assert _validate_left_right(r"\left( x \right)") == []
  40. def test_unbalanced(self) -> None:
  41. warnings = _validate_left_right(r"\left( x")
  42. assert len(warnings) > 0
  43. class TestValidateMarkdown:
  44. def test_valid(self) -> None:
  45. assert validate_markdown("text\n\n$$\na + b\n$$\n") == []
  46. def test_unbalanced_dollar(self) -> None:
  47. warnings = validate_markdown("$$\n$a\n$$\n")
  48. assert len(warnings) > 0
  49. def test_unbalanced_brace_in_formula(self) -> None:
  50. warnings = validate_markdown("$$\na{\nb\n$$\n")
  51. assert len(warnings) > 0
  52. class TestGenerateMarkdown:
  53. def test_text_block(self) -> None:
  54. import tempfile
  55. blocks = [ParsedBlock(label="text", content="Hello world", bbox=(0, 0, 100, 20))]
  56. with tempfile.TemporaryDirectory() as tmp:
  57. p = generate_markdown(blocks, Path(tmp) / "output.md")
  58. assert p.exists()
  59. content = p.read_text()
  60. assert "Hello world" in content
  61. def test_formula_block(self) -> None:
  62. import tempfile
  63. blocks = [ParsedBlock(label="formula", content=r"a + b = c", bbox=(0, 0, 100, 20))]
  64. with tempfile.TemporaryDirectory() as tmp:
  65. p = generate_markdown(blocks, Path(tmp) / "output.md")
  66. content = p.read_text()
  67. assert "$$\n" in content
  68. assert "a + b = c" in content
  69. def test_title_block(self) -> None:
  70. import tempfile
  71. blocks = [ParsedBlock(label="paragraph_title", content="Section 1", bbox=(0, 0, 100, 20))]
  72. with tempfile.TemporaryDirectory() as tmp:
  73. p = generate_markdown(blocks, Path(tmp) / "output.md")
  74. content = p.read_text()
  75. assert "## Section 1" in content
  76. def test_no_escaped_latex(self) -> None:
  77. import tempfile
  78. blocks = [ParsedBlock(label="text", content=r"alpha \sim", bbox=(0, 0, 100, 20))]
  79. with tempfile.TemporaryDirectory() as tmp:
  80. p = generate_markdown(blocks, Path(tmp) / "output.md")
  81. content = p.read_text()
  82. assert r"\textbackslash" not in content
  83. assert r"alpha" in content