2022-09-10 22:57:17 +02:00
|
|
|
# Standard Packages
|
|
|
|
import json
|
2023-03-30 07:30:25 +02:00
|
|
|
from pathlib import Path
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2023-02-14 21:50:51 +01:00
|
|
|
from khoj.processor.markdown.markdown_to_jsonl import MarkdownToJsonl
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_markdown_file_with_no_headings_to_jsonl(tmp_path):
|
|
|
|
"Convert files with no heading to jsonl."
|
|
|
|
# Arrange
|
2023-02-17 17:04:26 +01:00
|
|
|
entry = f"""
|
2022-09-10 22:57:17 +02:00
|
|
|
- Bullet point 1
|
|
|
|
- Bullet point 2
|
2023-02-17 17:04:26 +01:00
|
|
|
"""
|
2022-09-10 22:57:17 +02:00
|
|
|
markdownfile = create_file(tmp_path, entry)
|
2023-05-03 12:55:56 +02:00
|
|
|
expected_heading = "# " + markdownfile.stem
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Markdown files
|
2022-09-14 09:53:43 +02:00
|
|
|
entry_nodes, file_to_entries = MarkdownToJsonl.extract_markdown_entries(markdown_files=[markdownfile])
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
# Process Each Entry from All Notes Files
|
2022-09-14 09:53:43 +02:00
|
|
|
jsonl_string = MarkdownToJsonl.convert_markdown_maps_to_jsonl(
|
2023-02-17 17:04:26 +01:00
|
|
|
MarkdownToJsonl.convert_markdown_entries_to_maps(entry_nodes, file_to_entries)
|
|
|
|
)
|
2022-09-10 22:57:17 +02:00
|
|
|
jsonl_data = [json.loads(json_string) for json_string in jsonl_string.splitlines()]
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(jsonl_data) == 1
|
2023-05-03 12:55:56 +02:00
|
|
|
# Ensure raw entry with no headings do not get heading prefix prepended
|
|
|
|
assert not jsonl_data[0]["raw"].startswith("#")
|
|
|
|
# Ensure compiled entry has filename prepended as top level heading
|
|
|
|
assert jsonl_data[0]["compiled"].startswith(expected_heading)
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_single_markdown_entry_to_jsonl(tmp_path):
|
|
|
|
"Convert markdown entry from single file to jsonl."
|
|
|
|
# Arrange
|
2023-02-17 17:04:26 +01:00
|
|
|
entry = f"""### Heading
|
2022-09-10 22:57:17 +02:00
|
|
|
\t\r
|
|
|
|
Body Line 1
|
2023-02-17 17:04:26 +01:00
|
|
|
"""
|
2022-09-10 22:57:17 +02:00
|
|
|
markdownfile = create_file(tmp_path, entry)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Markdown files
|
2022-09-14 09:53:43 +02:00
|
|
|
entries, entry_to_file_map = MarkdownToJsonl.extract_markdown_entries(markdown_files=[markdownfile])
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
# Process Each Entry from All Notes Files
|
2022-09-14 09:53:43 +02:00
|
|
|
jsonl_string = MarkdownToJsonl.convert_markdown_maps_to_jsonl(
|
2023-02-17 17:04:26 +01:00
|
|
|
MarkdownToJsonl.convert_markdown_entries_to_maps(entries, entry_to_file_map)
|
|
|
|
)
|
2022-09-10 22:57:17 +02:00
|
|
|
jsonl_data = [json.loads(json_string) for json_string in jsonl_string.splitlines()]
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(jsonl_data) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_markdown_entries_to_jsonl(tmp_path):
|
|
|
|
"Convert multiple markdown entries from single file to jsonl."
|
|
|
|
# Arrange
|
2023-02-17 17:04:26 +01:00
|
|
|
entry = f"""
|
2022-09-10 22:57:17 +02:00
|
|
|
### Heading 1
|
|
|
|
\t\r
|
|
|
|
Heading 1 Body Line 1
|
|
|
|
### Heading 2
|
|
|
|
\t\r
|
|
|
|
Heading 2 Body Line 2
|
2023-02-17 17:04:26 +01:00
|
|
|
"""
|
2022-09-10 22:57:17 +02:00
|
|
|
markdownfile = create_file(tmp_path, entry)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Markdown files
|
2023-03-30 07:30:25 +02:00
|
|
|
entry_strings, entry_to_file_map = MarkdownToJsonl.extract_markdown_entries(markdown_files=[markdownfile])
|
|
|
|
entries = MarkdownToJsonl.convert_markdown_entries_to_maps(entry_strings, entry_to_file_map)
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
# Process Each Entry from All Notes Files
|
2023-03-30 07:30:25 +02:00
|
|
|
jsonl_string = MarkdownToJsonl.convert_markdown_maps_to_jsonl(entries)
|
2022-09-10 22:57:17 +02:00
|
|
|
jsonl_data = [json.loads(json_string) for json_string in jsonl_string.splitlines()]
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(jsonl_data) == 2
|
2023-03-30 07:30:25 +02:00
|
|
|
# Ensure entry compiled strings include the markdown files they originate from
|
|
|
|
assert all([markdownfile.stem in entry.compiled for entry in entries])
|
2022-09-10 22:57:17 +02:00
|
|
|
|
|
|
|
|
2022-09-12 09:39:39 +02:00
|
|
|
def test_get_markdown_files(tmp_path):
|
|
|
|
"Ensure Markdown files specified via input-filter, input-files extracted"
|
|
|
|
# Arrange
|
|
|
|
# Include via input-filter globs
|
|
|
|
group1_file1 = create_file(tmp_path, filename="group1-file1.md")
|
|
|
|
group1_file2 = create_file(tmp_path, filename="group1-file2.md")
|
|
|
|
group2_file1 = create_file(tmp_path, filename="group2-file1.markdown")
|
|
|
|
group2_file2 = create_file(tmp_path, filename="group2-file2.markdown")
|
|
|
|
# Include via input-file field
|
|
|
|
file1 = create_file(tmp_path, filename="notes.md")
|
|
|
|
# Not included by any filter
|
|
|
|
create_file(tmp_path, filename="not-included-markdown.md")
|
|
|
|
create_file(tmp_path, filename="not-included-text.txt")
|
|
|
|
|
2022-09-12 10:02:05 +02:00
|
|
|
expected_files = sorted(map(str, [group1_file1, group1_file2, group2_file1, group2_file2, file1]))
|
2022-09-12 09:39:39 +02:00
|
|
|
|
|
|
|
# Setup input-files, input-filters
|
2023-02-17 17:04:26 +01:00
|
|
|
input_files = [tmp_path / "notes.md"]
|
|
|
|
input_filter = [tmp_path / "group1*.md", tmp_path / "group2*.markdown"]
|
2022-09-12 09:39:39 +02:00
|
|
|
|
|
|
|
# Act
|
2022-09-14 09:53:43 +02:00
|
|
|
extracted_org_files = MarkdownToJsonl.get_markdown_files(input_files, input_filter)
|
2022-09-12 09:39:39 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(extracted_org_files) == 5
|
|
|
|
assert extracted_org_files == expected_files
|
|
|
|
|
|
|
|
|
2023-01-17 16:42:36 +01:00
|
|
|
def test_extract_entries_with_different_level_headings(tmp_path):
|
|
|
|
"Extract markdown entries with different level headings."
|
|
|
|
# Arrange
|
2023-02-17 17:04:26 +01:00
|
|
|
entry = f"""
|
2023-01-17 16:42:36 +01:00
|
|
|
# Heading 1
|
|
|
|
## Heading 2
|
2023-02-17 17:04:26 +01:00
|
|
|
"""
|
2023-01-17 16:42:36 +01:00
|
|
|
markdownfile = create_file(tmp_path, entry)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Markdown files
|
|
|
|
entries, _ = MarkdownToJsonl.extract_markdown_entries(markdown_files=[markdownfile])
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(entries) == 2
|
|
|
|
assert entries[0] == "# Heading 1"
|
|
|
|
assert entries[1] == "## Heading 2"
|
|
|
|
|
|
|
|
|
2022-09-10 22:57:17 +02:00
|
|
|
# Helper Functions
|
2023-05-03 12:55:56 +02:00
|
|
|
def create_file(tmp_path: Path, entry=None, filename="test.md"):
|
2022-09-12 09:39:39 +02:00
|
|
|
markdown_file = tmp_path / filename
|
2022-09-10 22:57:17 +02:00
|
|
|
markdown_file.touch()
|
2022-09-12 09:39:39 +02:00
|
|
|
if entry:
|
|
|
|
markdown_file.write_text(entry)
|
2022-09-10 22:57:17 +02:00
|
|
|
return markdown_file
|