2022-06-29 21:09:48 +02:00
|
|
|
# System Packages
|
2023-05-12 11:37:34 +02:00
|
|
|
import logging
|
2022-06-29 21:09:48 +02:00
|
|
|
from pathlib import Path
|
2023-06-14 01:55:58 +02:00
|
|
|
import os
|
2023-10-26 18:42:29 +02:00
|
|
|
import asyncio
|
2022-06-29 21:09:48 +02:00
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# Internal Packages
|
2023-02-14 21:50:51 +01:00
|
|
|
from khoj.search_type import text_search
|
2023-10-26 18:42:29 +02:00
|
|
|
from khoj.utils.rawconfig import ContentConfig, SearchConfig
|
2023-11-01 22:51:33 +01:00
|
|
|
from khoj.processor.org_mode.org_to_entries import OrgToEntries
|
|
|
|
from khoj.processor.github.github_to_entries import GithubToEntries
|
2023-10-26 20:37:41 +02:00
|
|
|
from khoj.utils.fs_syncer import collect_files, get_org_files
|
2023-11-21 19:56:04 +01:00
|
|
|
from khoj.database.models import LocalOrgConfig, KhojUser, Entry, GithubConfig
|
2023-10-26 18:42:29 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Test
|
2022-09-11 00:09:24 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-11-01 01:59:53 +01:00
|
|
|
def test_text_search_setup_with_missing_file_raises_error(org_config_with_only_new_file: LocalOrgConfig):
|
2022-09-11 00:09:24 +02:00
|
|
|
# Arrange
|
2023-01-09 20:17:36 +01:00
|
|
|
# Ensure file mentioned in org.input-files is missing
|
|
|
|
single_new_file = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
single_new_file.unlink()
|
2022-09-11 00:09:24 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
2023-08-31 21:55:17 +02:00
|
|
|
with pytest.raises(FileNotFoundError):
|
2023-10-18 06:31:15 +02:00
|
|
|
get_org_files(org_config_with_only_new_file)
|
2022-09-11 00:09:24 +02:00
|
|
|
|
|
|
|
|
2023-10-18 07:59:10 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_get_org_files_with_org_suffixed_dir_doesnt_raise_error(tmp_path, default_user: KhojUser):
|
2023-10-18 07:59:10 +02:00
|
|
|
# Arrange
|
|
|
|
orgfile = tmp_path / "directory.org" / "file.org"
|
|
|
|
orgfile.parent.mkdir()
|
|
|
|
with open(orgfile, "w") as f:
|
|
|
|
f.write("* Heading\n- List item\n")
|
2023-10-26 18:42:29 +02:00
|
|
|
|
|
|
|
LocalOrgConfig.objects.create(
|
|
|
|
input_filter=[f"{tmp_path}/**/*"],
|
|
|
|
input_files=None,
|
|
|
|
user=default_user,
|
2023-10-18 07:59:10 +02:00
|
|
|
)
|
|
|
|
|
2023-11-04 23:36:10 +01:00
|
|
|
# Act
|
2023-10-26 18:42:29 +02:00
|
|
|
org_files = collect_files(user=default_user)["org"]
|
|
|
|
|
2023-11-04 23:36:10 +01:00
|
|
|
# Assert
|
|
|
|
# should return orgfile and not raise IsADirectoryError
|
2023-10-26 18:42:29 +02:00
|
|
|
assert org_files == {f"{orgfile}": "* Heading\n- List item\n"}
|
2023-10-18 07:59:10 +02:00
|
|
|
|
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-11-07 08:49:08 +01:00
|
|
|
def test_text_search_setup_with_empty_file_creates_no_entries(
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config_with_only_new_file: LocalOrgConfig, default_user: KhojUser, caplog
|
2023-02-17 17:04:26 +01:00
|
|
|
):
|
2023-08-31 21:55:17 +02:00
|
|
|
# Arrange
|
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
2023-11-04 23:36:10 +01:00
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=True, user=default_user)
|
2023-10-26 18:42:29 +02:00
|
|
|
|
2023-11-04 23:36:10 +01:00
|
|
|
# Assert
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 0 new entries for user " in caplog.records[-1].message
|
2023-10-26 18:42:29 +02:00
|
|
|
verify_embeddings(0, default_user)
|
2022-09-10 12:05:21 +02:00
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-11-01 01:59:53 +01:00
|
|
|
def test_text_indexer_deletes_embedding_before_regenerate(
|
|
|
|
content_config: ContentConfig, default_user: KhojUser, caplog
|
|
|
|
):
|
2023-08-31 21:55:17 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config = LocalOrgConfig.objects.filter(user=default_user).first()
|
|
|
|
data = get_org_files(org_config)
|
2023-11-04 23:36:10 +01:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
2023-11-01 01:59:53 +01:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=True, user=default_user)
|
2023-11-01 01:59:53 +01:00
|
|
|
|
|
|
|
# Assert
|
2023-11-04 23:26:04 +01:00
|
|
|
assert "Deleting all entries for file type org" in caplog.text
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 13 new entries for user " in caplog.records[-1].message
|
2023-11-01 01:59:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_text_search_setup_batch_processes(content_config: ContentConfig, default_user: KhojUser, caplog):
|
|
|
|
# Arrange
|
|
|
|
org_config = LocalOrgConfig.objects.filter(user=default_user).first()
|
|
|
|
data = get_org_files(org_config)
|
2023-11-04 23:36:10 +01:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
2023-11-01 01:59:53 +01:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=True, user=default_user)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 13 new entries for user " in caplog.records[-1].message
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
|
2023-05-12 11:37:34 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_text_index_same_if_content_unchanged(content_config: ContentConfig, default_user: KhojUser, caplog):
|
2023-05-12 11:37:34 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config = LocalOrgConfig.objects.filter(user=default_user).first()
|
|
|
|
data = get_org_files(org_config)
|
2023-08-31 21:55:17 +02:00
|
|
|
|
2023-05-12 11:37:34 +02:00
|
|
|
# Act
|
|
|
|
# Generate initial notes embeddings during asymmetric setup
|
2023-11-01 01:59:53 +01:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=True, user=default_user)
|
2023-05-12 11:37:34 +02:00
|
|
|
initial_logs = caplog.text
|
|
|
|
caplog.clear() # Clear logs
|
|
|
|
|
|
|
|
# Run asymmetric setup again with no changes to data source. Ensure index is not updated
|
2023-11-01 01:59:53 +01:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=False, user=default_user)
|
2023-05-12 11:37:34 +02:00
|
|
|
final_logs = caplog.text
|
|
|
|
|
|
|
|
# Assert
|
2023-11-04 23:26:04 +01:00
|
|
|
assert "Deleting all entries for file type org" in initial_logs
|
|
|
|
assert "Deleting all entries for file type org" not in final_logs
|
2023-05-12 11:37:34 +02:00
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-06-29 05:11:26 +02:00
|
|
|
@pytest.mark.anyio
|
2023-10-26 18:42:29 +02:00
|
|
|
# @pytest.mark.asyncio
|
|
|
|
async def test_text_search(search_config: SearchConfig):
|
|
|
|
# Arrange
|
|
|
|
default_user = await KhojUser.objects.acreate(
|
|
|
|
username="test_user", password="test_password", email="test@example.com"
|
|
|
|
)
|
|
|
|
org_config = await LocalOrgConfig.objects.acreate(
|
|
|
|
input_files=None,
|
|
|
|
input_filter=["tests/data/org/*.org"],
|
|
|
|
index_heading_entries=False,
|
|
|
|
user=default_user,
|
|
|
|
)
|
|
|
|
data = get_org_files(org_config)
|
2023-08-31 21:55:17 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
await loop.run_in_executor(
|
|
|
|
None,
|
|
|
|
text_search.setup,
|
2023-11-01 22:51:33 +01:00
|
|
|
OrgToEntries,
|
2023-10-26 18:42:29 +02:00
|
|
|
data,
|
|
|
|
True,
|
|
|
|
True,
|
|
|
|
default_user,
|
2023-07-14 10:19:38 +02:00
|
|
|
)
|
2023-10-26 18:42:29 +02:00
|
|
|
|
2023-11-17 23:49:39 +01:00
|
|
|
query = "Load Khoj on Emacs?"
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 18:42:29 +02:00
|
|
|
hits = await text_search.query(default_user, query)
|
|
|
|
results = text_search.collate_results(hits)
|
|
|
|
results = sorted(results, key=lambda x: float(x.score))[:1]
|
2023-11-04 23:36:10 +01:00
|
|
|
|
|
|
|
# Assert
|
2022-09-15 22:34:43 +02:00
|
|
|
search_result = results[0].entry
|
2023-11-17 23:49:39 +01:00
|
|
|
assert "Emacs load path" in search_result, 'Expected "Emacs load path" in entry'
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
|
2022-12-23 19:52:02 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_entry_chunking_by_max_tokens(org_config_with_only_new_file: LocalOrgConfig, default_user: KhojUser, caplog):
|
2022-12-23 19:52:02 +01:00
|
|
|
# Arrange
|
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
|
2023-01-09 20:17:36 +01:00
|
|
|
new_file_to_index = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
with open(new_file_to_index, "w") as f:
|
2022-12-23 19:52:02 +01:00
|
|
|
f.write(f"* Entry more than {max_tokens} words\n")
|
2023-02-17 17:04:26 +01:00
|
|
|
for index in range(max_tokens + 1):
|
2022-12-23 19:52:02 +01:00
|
|
|
f.write(f"{index} ")
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
|
|
|
|
2022-12-23 19:52:02 +01:00
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model after adding new org-mode file
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=False, user=default_user)
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
# Assert
|
2023-11-04 23:26:04 +01:00
|
|
|
assert (
|
|
|
|
"Deleted 0 entries. Created 2 new entries for user " in caplog.records[-1].message
|
|
|
|
), "new entry not split by max tokens"
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
|
2023-09-06 21:04:18 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-09-06 21:04:18 +02:00
|
|
|
def test_entry_chunking_by_max_tokens_not_full_corpus(
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config_with_only_new_file: LocalOrgConfig, default_user: KhojUser, caplog
|
2023-09-06 21:04:18 +02:00
|
|
|
):
|
|
|
|
# Arrange
|
|
|
|
# Insert org-mode entry with size exceeding max token limit to new org file
|
|
|
|
data = {
|
|
|
|
"readme.org": """
|
|
|
|
* Khoj
|
2023-10-26 18:42:29 +02:00
|
|
|
/Allow natural language search on user content like notes, images using transformer based models/
|
2023-09-06 21:04:18 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
All data is processed locally. User can interface with khoj app via [[./interface/emacs/khoj.el][Emacs]], API or Commandline
|
2023-09-06 21:04:18 +02:00
|
|
|
|
|
|
|
** Dependencies
|
2023-10-26 18:42:29 +02:00
|
|
|
- Python3
|
|
|
|
- [[https://docs.conda.io/en/latest/miniconda.html#latest-miniconda-installer-links][Miniconda]]
|
2023-09-06 21:04:18 +02:00
|
|
|
|
|
|
|
** Install
|
2023-10-26 18:42:29 +02:00
|
|
|
#+begin_src shell
|
|
|
|
git clone https://github.com/khoj-ai/khoj && cd khoj
|
|
|
|
conda env create -f environment.yml
|
|
|
|
conda activate khoj
|
|
|
|
#+end_src"""
|
2023-09-06 21:04:18 +02:00
|
|
|
}
|
|
|
|
text_search.setup(
|
2023-11-01 22:51:33 +01:00
|
|
|
OrgToEntries,
|
2023-09-06 21:04:18 +02:00
|
|
|
data,
|
|
|
|
regenerate=False,
|
2023-10-26 18:42:29 +02:00
|
|
|
user=default_user,
|
2023-09-06 21:04:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
max_tokens = 256
|
|
|
|
new_file_to_index = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
with open(new_file_to_index, "w") as f:
|
|
|
|
f.write(f"* Entry more than {max_tokens} words\n")
|
|
|
|
for index in range(max_tokens + 1):
|
|
|
|
f.write(f"{index} ")
|
|
|
|
|
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model after adding new org-mode file
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(
|
2023-11-01 22:51:33 +01:00
|
|
|
OrgToEntries,
|
2023-10-26 18:42:29 +02:00
|
|
|
data,
|
|
|
|
regenerate=False,
|
|
|
|
full_corpus=False,
|
|
|
|
user=default_user,
|
|
|
|
)
|
|
|
|
|
2023-09-06 21:04:18 +02:00
|
|
|
# Assert
|
2023-11-04 23:26:04 +01:00
|
|
|
assert (
|
|
|
|
"Deleted 0 entries. Created 2 new entries for user " in caplog.records[-1].message
|
|
|
|
), "new entry not split by max tokens"
|
2023-09-06 21:04:18 +02:00
|
|
|
|
|
|
|
|
2022-06-29 21:09:48 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-07-16 09:47:11 +02:00
|
|
|
def test_regenerate_index_with_new_entry(
|
2023-10-26 18:42:29 +02:00
|
|
|
content_config: ContentConfig, new_org_file: Path, default_user: KhojUser, caplog
|
2023-07-16 09:47:11 +02:00
|
|
|
):
|
2022-06-29 21:09:48 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config = LocalOrgConfig.objects.filter(user=default_user).first()
|
2023-11-04 23:36:10 +01:00
|
|
|
initial_data = get_org_files(org_config)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# append org-mode entry to first org input file in config
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config.input_files = [f"{new_org_file}"]
|
2023-01-09 20:17:36 +01:00
|
|
|
with open(new_org_file, "w") as f:
|
2022-06-29 21:09:48 +02:00
|
|
|
f.write("\n* A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n")
|
|
|
|
|
2023-11-04 23:36:10 +01:00
|
|
|
final_data = get_org_files(org_config)
|
2023-08-31 21:55:17 +02:00
|
|
|
|
2023-07-16 09:47:11 +02:00
|
|
|
# Act
|
2023-11-04 23:36:10 +01:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToEntries, initial_data, regenerate=True, user=default_user)
|
|
|
|
initial_logs = caplog.text
|
|
|
|
caplog.clear() # Clear logs
|
|
|
|
|
2022-06-29 21:09:48 +02:00
|
|
|
# regenerate notes jsonl, model embeddings and model to include entry from new file
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-04 23:36:10 +01:00
|
|
|
text_search.setup(OrgToEntries, final_data, regenerate=True, user=default_user)
|
|
|
|
final_logs = caplog.text
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 13 new entries for user " in initial_logs
|
|
|
|
assert "Deleted 13 entries. Created 14 new entries for user " in final_logs
|
|
|
|
verify_embeddings(14, default_user)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
|
|
|
|
2023-07-16 02:18:07 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-07-16 02:18:07 +02:00
|
|
|
def test_update_index_with_duplicate_entries_in_stable_order(
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config_with_only_new_file: LocalOrgConfig, default_user: KhojUser, caplog
|
2023-07-16 02:18:07 +02:00
|
|
|
):
|
|
|
|
# Arrange
|
|
|
|
new_file_to_index = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
|
|
|
|
# Insert org-mode entries with same compiled form into new org file
|
|
|
|
new_entry = "* TODO A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n"
|
|
|
|
with open(new_file_to_index, "w") as f:
|
|
|
|
f.write(f"{new_entry}{new_entry}")
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
|
|
|
|
2023-07-16 02:18:07 +02:00
|
|
|
# Act
|
2023-11-01 01:59:53 +01:00
|
|
|
# generate embeddings, entries, notes model from scratch after adding new org-mode file
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=True, user=default_user)
|
2023-11-01 01:59:53 +01:00
|
|
|
initial_logs = caplog.text
|
|
|
|
caplog.clear() # Clear logs
|
2023-07-16 02:18:07 +02:00
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
|
|
|
|
2023-11-01 01:59:53 +01:00
|
|
|
# update embeddings, entries, notes model with no new changes
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=False, user=default_user)
|
2023-11-01 01:59:53 +01:00
|
|
|
final_logs = caplog.text
|
2023-07-16 02:18:07 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify only 1 entry added even if there are multiple duplicate entries
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 1 new entries for user " in initial_logs
|
2023-11-04 23:26:04 +01:00
|
|
|
assert "Deleted 0 entries. Created 0 new entries for user " in final_logs
|
2023-07-16 02:18:07 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
verify_embeddings(1, default_user)
|
2023-07-16 02:18:07 +02:00
|
|
|
|
|
|
|
|
2023-07-16 12:47:05 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_update_index_with_deleted_entry(org_config_with_only_new_file: LocalOrgConfig, default_user: KhojUser, caplog):
|
2023-07-16 12:47:05 +02:00
|
|
|
# Arrange
|
|
|
|
new_file_to_index = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
|
|
|
|
# Insert org-mode entries with same compiled form into new org file
|
|
|
|
new_entry = "* TODO A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n"
|
|
|
|
with open(new_file_to_index, "w") as f:
|
|
|
|
f.write(f"{new_entry}{new_entry} -- Tatooine")
|
2023-11-04 23:36:10 +01:00
|
|
|
initial_data = get_org_files(org_config_with_only_new_file)
|
2023-07-16 12:47:05 +02:00
|
|
|
|
|
|
|
# update embeddings, entries, notes model after removing an entry from the org file
|
|
|
|
with open(new_file_to_index, "w") as f:
|
|
|
|
f.write(f"{new_entry}")
|
|
|
|
|
2023-11-04 23:36:10 +01:00
|
|
|
final_data = get_org_files(org_config_with_only_new_file)
|
2023-08-31 21:55:17 +02:00
|
|
|
|
2023-07-16 12:47:05 +02:00
|
|
|
# Act
|
2023-11-04 23:36:10 +01:00
|
|
|
# load embeddings, entries, notes model after adding new org file with 2 entries
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-04 23:36:10 +01:00
|
|
|
text_search.setup(OrgToEntries, initial_data, regenerate=True, user=default_user)
|
|
|
|
initial_logs = caplog.text
|
|
|
|
caplog.clear() # Clear logs
|
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToEntries, final_data, regenerate=False, user=default_user)
|
2023-11-01 01:59:53 +01:00
|
|
|
final_logs = caplog.text
|
2023-07-16 12:47:05 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify only 1 entry added even if there are multiple duplicate entries
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 2 new entries for user " in initial_logs
|
2023-11-04 23:26:04 +01:00
|
|
|
assert "Deleted 1 entries. Created 0 new entries for user " in final_logs
|
2023-07-16 12:47:05 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
verify_embeddings(1, default_user)
|
2023-07-16 12:47:05 +02:00
|
|
|
|
|
|
|
|
2022-09-07 02:06:29 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_update_index_with_new_entry(content_config: ContentConfig, new_org_file: Path, default_user: KhojUser, caplog):
|
2022-09-07 02:06:29 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
org_config = LocalOrgConfig.objects.filter(user=default_user).first()
|
|
|
|
data = get_org_files(org_config)
|
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=True, user=default_user)
|
2023-11-01 01:59:53 +01:00
|
|
|
initial_logs = caplog.text
|
|
|
|
caplog.clear() # Clear logs
|
2022-09-07 02:06:29 +02:00
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# append org-mode entry to first org input file in config
|
|
|
|
with open(new_org_file, "w") as f:
|
2023-07-16 10:24:03 +02:00
|
|
|
new_entry = "\n* A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n"
|
|
|
|
f.write(new_entry)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
data = get_org_files(org_config)
|
2023-08-31 21:55:17 +02:00
|
|
|
|
2022-09-07 02:06:29 +02:00
|
|
|
# Act
|
|
|
|
# update embeddings, entries with the newly added note
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
2023-11-01 22:51:33 +01:00
|
|
|
text_search.setup(OrgToEntries, data, regenerate=False, user=default_user)
|
2023-11-01 01:59:53 +01:00
|
|
|
final_logs = caplog.text
|
2022-09-07 02:06:29 +02:00
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# Assert
|
2023-11-16 11:47:58 +01:00
|
|
|
assert "Deleted 8 entries. Created 13 new entries for user " in initial_logs
|
2023-11-04 23:26:04 +01:00
|
|
|
assert "Deleted 0 entries. Created 1 new entries for user " in final_logs
|
2023-11-16 11:47:58 +01:00
|
|
|
verify_embeddings(14, default_user)
|
2023-06-14 01:55:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.skipif(os.getenv("GITHUB_PAT_TOKEN") is None, reason="GITHUB_PAT_TOKEN not set")
|
2023-10-26 18:42:29 +02:00
|
|
|
def test_text_search_setup_github(content_config: ContentConfig, default_user: KhojUser):
|
|
|
|
# Arrange
|
|
|
|
github_config = GithubConfig.objects.filter(user=default_user).first()
|
2023-11-04 23:36:10 +01:00
|
|
|
|
2023-06-14 01:55:58 +02:00
|
|
|
# Act
|
2023-06-18 11:24:25 +02:00
|
|
|
# Regenerate github embeddings to test asymmetric setup without caching
|
2023-10-26 18:42:29 +02:00
|
|
|
text_search.setup(
|
2023-11-01 22:51:33 +01:00
|
|
|
GithubToEntries,
|
2023-10-26 18:42:29 +02:00
|
|
|
{},
|
|
|
|
regenerate=True,
|
|
|
|
user=default_user,
|
|
|
|
config=github_config,
|
2023-07-14 10:19:38 +02:00
|
|
|
)
|
2023-06-14 01:55:58 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-11-01 02:50:54 +01:00
|
|
|
embeddings = Entry.objects.filter(user=default_user, file_type="github").count()
|
2023-10-26 18:42:29 +02:00
|
|
|
assert embeddings > 1
|
2023-07-15 23:33:15 +02:00
|
|
|
|
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
def verify_embeddings(expected_count, user):
|
2023-11-01 02:50:54 +01:00
|
|
|
embeddings = Entry.objects.filter(user=user, file_type="org").count()
|
2023-10-26 18:42:29 +02:00
|
|
|
assert embeddings == expected_count
|