2022-08-21 18:41:40 +02:00
|
|
|
# Standard Packages
|
|
|
|
import json
|
|
|
|
|
|
|
|
# Internal Packages
|
2022-09-06 23:16:48 +02:00
|
|
|
from src.processor.org_mode.org_to_jsonl import convert_org_entries_to_jsonl, convert_org_nodes_to_entries, extract_org_entries
|
2022-08-21 18:41:40 +02:00
|
|
|
from src.utils.helpers import is_none_or_empty
|
|
|
|
|
|
|
|
|
|
|
|
def test_entry_with_empty_body_line_to_jsonl(tmp_path):
|
|
|
|
'''Ensure entries with empty body are ignored.
|
|
|
|
Property drawers not considered Body. Ignore control characters for evaluating if Body empty.'''
|
|
|
|
# Arrange
|
|
|
|
entry = f'''*** Heading
|
|
|
|
:PROPERTIES:
|
|
|
|
:ID: 42-42-42
|
|
|
|
:END:
|
2022-09-10 12:11:58 +02:00
|
|
|
\t\r
|
2022-08-21 18:41:40 +02:00
|
|
|
'''
|
|
|
|
orgfile = create_file(tmp_path, entry)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Org files
|
2022-09-06 23:16:48 +02:00
|
|
|
entry_nodes, file_to_entries = extract_org_entries(org_files=[orgfile])
|
2022-08-21 18:41:40 +02:00
|
|
|
|
|
|
|
# Process Each Entry from All Notes Files
|
2022-09-06 23:16:48 +02:00
|
|
|
entries = convert_org_nodes_to_entries(entry_nodes, file_to_entries)
|
|
|
|
jsonl_data = convert_org_entries_to_jsonl(entries)
|
2022-08-21 18:41:40 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert is_none_or_empty(jsonl_data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_entry_with_body_to_jsonl(tmp_path):
|
|
|
|
"Ensure entries with valid body text are loaded."
|
|
|
|
# Arrange
|
|
|
|
entry = f'''*** Heading
|
|
|
|
:PROPERTIES:
|
|
|
|
:ID: 42-42-42
|
|
|
|
:END:
|
2022-09-10 12:11:58 +02:00
|
|
|
\t\r
|
|
|
|
Body Line 1
|
|
|
|
'''
|
|
|
|
orgfile = create_file(tmp_path, entry)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Org files
|
|
|
|
entries, entry_to_file_map = extract_org_entries(org_files=[orgfile])
|
|
|
|
|
|
|
|
# Process Each Entry from All Notes Files
|
2022-09-10 21:11:43 +02:00
|
|
|
jsonl_string = convert_org_entries_to_jsonl(convert_org_nodes_to_entries(entries, entry_to_file_map))
|
2022-09-10 12:11:58 +02:00
|
|
|
jsonl_data = [json.loads(json_string) for json_string in jsonl_string.splitlines()]
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(jsonl_data) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_file_with_no_headings_to_jsonl(tmp_path):
|
|
|
|
"Ensure files with no heading, only body text are loaded."
|
|
|
|
# Arrange
|
|
|
|
entry = f'''
|
|
|
|
- Bullet point 1
|
|
|
|
- Bullet point 2
|
2022-08-21 18:41:40 +02:00
|
|
|
'''
|
|
|
|
orgfile = create_file(tmp_path, entry)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Extract Entries from specified Org files
|
2022-09-06 23:16:48 +02:00
|
|
|
entry_nodes, file_to_entries = extract_org_entries(org_files=[orgfile])
|
2022-08-21 18:41:40 +02:00
|
|
|
|
|
|
|
# Process Each Entry from All Notes Files
|
2022-09-06 23:16:48 +02:00
|
|
|
entries = convert_org_nodes_to_entries(entry_nodes, file_to_entries)
|
|
|
|
jsonl_string = convert_org_entries_to_jsonl(entries)
|
2022-08-21 18:41:40 +02:00
|
|
|
jsonl_data = [json.loads(json_string) for json_string in jsonl_string.splitlines()]
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(jsonl_data) == 1
|
|
|
|
|
|
|
|
|
|
|
|
# Helper Functions
|
|
|
|
def create_file(tmp_path, entry, filename="test.org"):
|
|
|
|
org_file = tmp_path / f"notes/{filename}"
|
|
|
|
org_file.parent.mkdir()
|
|
|
|
org_file.touch()
|
|
|
|
org_file.write_text(entry)
|
|
|
|
return org_file
|