2022-06-29 21:09:48 +02:00
|
|
|
# System Packages
|
2022-09-10 12:05:21 +02:00
|
|
|
from copy import deepcopy
|
2022-06-29 21:09:48 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# Internal Packages
|
2022-08-06 02:05:35 +02:00
|
|
|
from src.utils.state import model
|
2022-07-21 16:05:43 +02:00
|
|
|
from src.search_type import text_search
|
2022-01-15 02:54:38 +01:00
|
|
|
from src.utils.rawconfig import ContentConfig, SearchConfig
|
2022-09-14 09:53:43 +02:00
|
|
|
from src.processor.org_mode.org_to_jsonl import OrgToJsonl
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Test
|
2022-09-11 00:09:24 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_asymmetric_setup_with_missing_file_raises_error(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
2022-09-12 09:39:39 +02:00
|
|
|
file_to_index = Path(content_config.org.input_filter[0]).parent / "new_file_to_index.org"
|
2022-09-11 00:09:24 +02:00
|
|
|
new_org_content_config = deepcopy(content_config.org)
|
|
|
|
new_org_content_config.input_files = [f'{file_to_index}']
|
|
|
|
new_org_content_config.input_filter = None
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
|
|
|
with pytest.raises(FileNotFoundError):
|
2022-09-14 09:53:43 +02:00
|
|
|
text_search.setup(OrgToJsonl, new_org_content_config, search_config.asymmetric, regenerate=True)
|
2022-09-11 00:09:24 +02:00
|
|
|
|
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_asymmetric_setup_with_empty_file_raises_error(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
2022-09-12 09:39:39 +02:00
|
|
|
file_to_index = Path(content_config.org.input_filter[0]).parent / "new_file_to_index.org"
|
2022-09-10 12:05:21 +02:00
|
|
|
file_to_index.touch()
|
|
|
|
new_org_content_config = deepcopy(content_config.org)
|
|
|
|
new_org_content_config.input_files = [f'{file_to_index}']
|
|
|
|
new_org_content_config.input_filter = None
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
|
|
|
with pytest.raises(ValueError, match=r'^No valid entries found*'):
|
2022-09-14 09:53:43 +02:00
|
|
|
text_search.setup(OrgToJsonl, new_org_content_config, search_config.asymmetric, regenerate=True)
|
2022-09-10 12:05:21 +02:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
# delete created test file
|
|
|
|
file_to_index.unlink()
|
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_asymmetric_setup(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Act
|
|
|
|
# Regenerate notes embeddings during asymmetric setup
|
2022-09-14 09:53:43 +02:00
|
|
|
notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(notes_model.entries) == 10
|
|
|
|
assert len(notes_model.corpus_embeddings) == 10
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_asymmetric_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2022-09-14 09:53:43 +02:00
|
|
|
model.notes_search = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
query = "How to git install application?"
|
|
|
|
|
|
|
|
# Act
|
2022-07-21 16:05:43 +02:00
|
|
|
hits, entries = text_search.query(
|
2021-10-03 05:28:33 +02:00
|
|
|
query,
|
2022-07-26 20:56:36 +02:00
|
|
|
model = model.notes_search,
|
|
|
|
rank_results=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
2022-07-21 16:05:43 +02:00
|
|
|
results = text_search.collate_results(
|
2021-10-03 05:28:33 +02:00
|
|
|
hits,
|
2022-07-12 16:48:49 +02:00
|
|
|
entries,
|
2021-10-03 05:28:33 +02:00
|
|
|
count=1)
|
|
|
|
|
|
|
|
# Assert
|
2022-07-19 16:26:16 +02:00
|
|
|
# Actual_data should contain "Khoj via Emacs" entry
|
2022-09-15 22:34:43 +02:00
|
|
|
search_result = results[0].entry
|
2021-10-03 05:28:33 +02:00
|
|
|
assert "git clone" in search_result
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
|
2022-12-23 19:52:02 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_entry_chunking_by_max_tokens(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
2022-12-26 01:45:40 +01:00
|
|
|
initial_notes_model= text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
assert len(initial_notes_model.entries) == 10
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 10
|
|
|
|
|
|
|
|
file_to_add_on_reload = Path(content_config.org.input_filter[0]).parent / "entry_exceeding_max_tokens.org"
|
|
|
|
content_config.org.input_files = [f'{file_to_add_on_reload}']
|
|
|
|
|
2022-12-26 01:45:40 +01:00
|
|
|
# Insert org-mode entry with size exceeding max token limit to new org file
|
2022-12-23 19:52:02 +01:00
|
|
|
max_tokens = 256
|
|
|
|
with open(file_to_add_on_reload, "w") as f:
|
|
|
|
f.write(f"* Entry more than {max_tokens} words\n")
|
|
|
|
for index in range(max_tokens+1):
|
|
|
|
f.write(f"{index} ")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model after adding new org-mode file
|
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify newly added org-mode entry is split by max tokens
|
|
|
|
assert len(initial_notes_model.entries) == 12
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 12
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
# delete reload test file added
|
|
|
|
content_config.org.input_files = []
|
|
|
|
file_to_add_on_reload.unlink()
|
|
|
|
|
|
|
|
|
2022-06-29 21:09:48 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_asymmetric_reload(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
2022-12-23 20:18:22 +01:00
|
|
|
initial_notes_model= text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
assert len(initial_notes_model.entries) == 10
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 10
|
|
|
|
|
2022-09-12 09:39:39 +02:00
|
|
|
file_to_add_on_reload = Path(content_config.org.input_filter[0]).parent / "reload.org"
|
2022-06-29 21:09:48 +02:00
|
|
|
content_config.org.input_files = [f'{file_to_add_on_reload}']
|
|
|
|
|
|
|
|
# append Org-Mode Entry to first Org Input File in Config
|
|
|
|
with open(file_to_add_on_reload, "w") as f:
|
|
|
|
f.write("\n* A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n")
|
|
|
|
|
|
|
|
# regenerate notes jsonl, model embeddings and model to include entry from new file
|
2022-09-14 09:53:43 +02:00
|
|
|
regenerated_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model from previously generated notes jsonl and model embeddings files
|
2022-09-14 09:53:43 +02:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(regenerated_notes_model.entries) == 11
|
|
|
|
assert len(regenerated_notes_model.corpus_embeddings) == 11
|
|
|
|
|
|
|
|
# verify new entry loaded from updated embeddings, entries
|
|
|
|
assert len(initial_notes_model.entries) == 11
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 11
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
# delete reload test file added
|
2022-07-06 23:25:31 +02:00
|
|
|
content_config.org.input_files = []
|
2022-06-29 21:09:48 +02:00
|
|
|
file_to_add_on_reload.unlink()
|
2022-09-07 02:06:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_incremental_update(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
2022-09-14 09:53:43 +02:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
|
|
|
assert len(initial_notes_model.entries) == 10
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 10
|
|
|
|
|
2022-09-12 09:39:39 +02:00
|
|
|
file_to_add_on_update = Path(content_config.org.input_filter[0]).parent / "update.org"
|
2022-09-07 02:06:29 +02:00
|
|
|
content_config.org.input_files = [f'{file_to_add_on_update}']
|
|
|
|
|
|
|
|
# append Org-Mode Entry to first Org Input File in Config
|
|
|
|
with open(file_to_add_on_update, "w") as f:
|
|
|
|
f.write("\n* A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# update embeddings, entries with the newly added note
|
2022-09-14 09:53:43 +02:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
|
|
|
# verify new entry added in updated embeddings, entries
|
|
|
|
assert len(initial_notes_model.entries) == 11
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 11
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
# delete file added for update testing
|
|
|
|
content_config.org.input_files = []
|
|
|
|
file_to_add_on_update.unlink()
|