test_knowledge_document_parsers.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from __future__ import annotations
  2. import sys
  3. from pathlib import Path
  4. REPO_ROOT = Path(__file__).resolve().parents[1]
  5. for module_name in list(sys.modules):
  6. if module_name == "app" or module_name.startswith("app."):
  7. del sys.modules[module_name]
  8. for path in [
  9. REPO_ROOT / "libs" / "core-shared" / "src",
  10. REPO_ROOT / "services" / "knowledge-service",
  11. ]:
  12. sys.path.insert(0, str(path))
  13. from app.application.document_parsers import parse_document_content
  14. def test_parse_markdown_html_json_csv_documents() -> None:
  15. markdown = parse_document_content(
  16. source_type="markdown",
  17. content_text="# Title\n\nUse [docs](https://example.com) and `code`.",
  18. )
  19. html = parse_document_content(
  20. source_type="html",
  21. content_text="<h1>Title</h1><script>hidden()</script><p>Hello <b>world</b></p>",
  22. )
  23. json_doc = parse_document_content(
  24. source_type="json",
  25. content_text='{"order":{"id":"A1","status":"paid"}}',
  26. )
  27. csv_doc = parse_document_content(
  28. source_type="csv",
  29. content_text="id,status\nA1,paid\nA2,refunded\n",
  30. )
  31. assert "Title" in markdown.content_text
  32. assert "docs" in markdown.content_text
  33. assert "hidden" not in html.content_text
  34. assert "Hello world" in html.content_text
  35. assert "order.id: A1" in json_doc.content_text
  36. assert "row 2: id: A2; status: refunded" in csv_doc.content_text