2022-06-29 21:09:48 +02:00
|
|
|
# System Packages
|
2023-05-12 11:37:34 +02:00
|
|
|
import logging
|
2023-10-18 06:31:15 +02:00
|
|
|
import locale
|
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-02-14 21:50:51 +01:00
|
|
|
from khoj.processor.org_mode.org_to_jsonl import OrgToJsonl
|
2023-06-14 01:55:58 +02:00
|
|
|
from khoj.processor.github.github_to_jsonl import GithubToJsonl
|
2023-10-26 20:37:41 +02:00
|
|
|
from khoj.utils.fs_syncer import collect_files, get_org_files
|
2023-10-26 18:42:29 +02:00
|
|
|
from database.models import LocalOrgConfig, KhojUser, Embeddings, GithubConfig
|
|
|
|
|
|
|
|
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
|
|
|
|
def test_text_search_setup_with_missing_file_raises_error(
|
|
|
|
org_config_with_only_new_file: LocalOrgConfig, search_config: SearchConfig
|
|
|
|
):
|
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-10-26 18:42:29 +02:00
|
|
|
org_files = collect_files(user=default_user)["org"]
|
|
|
|
|
2023-10-18 07:59:10 +02:00
|
|
|
# Act
|
|
|
|
# should not raise IsADirectoryError and return orgfile
|
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-07-16 10:23:22 +02:00
|
|
|
def test_text_search_setup_with_empty_file_raises_error(
|
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)
|
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):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=True, user=default_user)
|
|
|
|
|
|
|
|
assert "Created 0 new embeddings. Deleted 3 embeddings for user " in caplog.records[2].message
|
|
|
|
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
|
|
|
|
def test_text_search_setup(content_config, 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)
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=True, user=default_user)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Deleting all embeddings for file type org" in caplog.records[1].message
|
|
|
|
assert "Created 10 new embeddings. Deleted 3 embeddings for user " in caplog.records[2].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-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToJsonl, 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-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=False, user=default_user)
|
2023-05-12 11:37:34 +02:00
|
|
|
final_logs = caplog.text
|
|
|
|
|
|
|
|
# Assert
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Deleting all embeddings for file type org" in initial_logs
|
|
|
|
assert "Deleting all embeddings 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"
|
|
|
|
)
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
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,
|
|
|
|
OrgToJsonl,
|
|
|
|
data,
|
|
|
|
True,
|
|
|
|
True,
|
|
|
|
default_user,
|
2023-07-14 10:19:38 +02:00
|
|
|
)
|
2023-10-26 18:42:29 +02:00
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
query = "How to git install application?"
|
|
|
|
|
|
|
|
# Act
|
2023-10-26 18:42:29 +02:00
|
|
|
hits = await text_search.query(default_user, query)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-10-26 18:42:29 +02:00
|
|
|
results = text_search.collate_results(hits)
|
|
|
|
results = sorted(results, key=lambda x: float(x.score))[:1]
|
2023-07-29 00:33:22 +02:00
|
|
|
# search results should contain "git clone" 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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
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):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=False, user=default_user)
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify newly added org-mode entry is split by max tokens
|
2023-10-26 18:42:29 +02:00
|
|
|
record = caplog.records[1]
|
|
|
|
assert "Created 2 new embeddings. Deleted 0 embeddings for user " in record.message
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
|
2023-09-06 21:04:18 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
# @pytest.mark.skip(reason="Flaky due to compressed_jsonl file being rewritten by other tests")
|
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(
|
|
|
|
OrgToJsonl,
|
|
|
|
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(
|
|
|
|
OrgToJsonl,
|
|
|
|
data,
|
|
|
|
regenerate=False,
|
|
|
|
full_corpus=False,
|
|
|
|
user=default_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
record = caplog.records[1]
|
2023-09-06 21:04:18 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify newly added org-mode entry is split by max tokens
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Created 2 new embeddings. Deleted 0 embeddings for user " in record.message
|
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()
|
|
|
|
data = get_org_files(org_config)
|
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=True, user=default_user)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Created 10 new embeddings. Deleted 3 embeddings for user " in caplog.records[2].message
|
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-10-26 18:42:29 +02:00
|
|
|
data = get_org_files(org_config)
|
2023-08-31 21:55:17 +02:00
|
|
|
|
2023-07-16 09:47:11 +02:00
|
|
|
# Act
|
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):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=True, user=default_user)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Created 11 new embeddings. Deleted 10 embeddings for user " in caplog.records[-1].message
|
|
|
|
verify_embeddings(11, 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
|
|
|
|
# load 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(OrgToJsonl, data, regenerate=True, user=default_user)
|
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-07-16 02:18:07 +02:00
|
|
|
# update 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(OrgToJsonl, data, regenerate=False, user=default_user)
|
2023-07-16 02:18:07 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify only 1 entry added even if there are multiple duplicate entries
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Created 1 new embeddings. Deleted 3 embeddings for user " in caplog.records[2].message
|
|
|
|
assert "Created 0 new embeddings. Deleted 0 embeddings for user " in caplog.records[4].message
|
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-08-31 21:55:17 +02:00
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
2023-07-16 12:47:05 +02: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):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=True, user=default_user)
|
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-08-31 21:55:17 +02:00
|
|
|
data = get_org_files(org_config_with_only_new_file)
|
|
|
|
|
2023-07-16 12:47:05 +02:00
|
|
|
# Act
|
2023-10-26 18:42:29 +02:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=False, user=default_user)
|
2023-07-16 12:47:05 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify only 1 entry added even if there are multiple duplicate entries
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Created 2 new embeddings. Deleted 3 embeddings for user " in caplog.records[2].message
|
|
|
|
assert "Created 0 new embeddings. Deleted 1 embeddings for user " in caplog.records[4].message
|
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):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=True, user=default_user)
|
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):
|
|
|
|
text_search.setup(OrgToJsonl, data, regenerate=False, user=default_user)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# Assert
|
2023-10-26 18:42:29 +02:00
|
|
|
assert "Created 10 new embeddings. Deleted 3 embeddings for user " in caplog.records[2].message
|
|
|
|
assert "Created 1 new embeddings. Deleted 0 embeddings for user " in caplog.records[4].message
|
2022-09-07 02:06:29 +02:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
verify_embeddings(11, 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-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(
|
|
|
|
GithubToJsonl,
|
|
|
|
{},
|
|
|
|
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-10-26 18:42:29 +02:00
|
|
|
embeddings = Embeddings.objects.filter(user=default_user, file_type="github").count()
|
|
|
|
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):
|
|
|
|
embeddings = Embeddings.objects.filter(user=user, file_type="org").count()
|
|
|
|
assert embeddings == expected_count
|