2021-08-17 12:27:16 +02:00
|
|
|
# Standard Modules
|
2022-07-20 01:01:56 +02:00
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image
|
2022-09-12 15:42:41 +02:00
|
|
|
from urllib.parse import quote
|
2023-10-26 18:42:29 +02:00
|
|
|
import pytest
|
2021-08-17 12:27:16 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
from fastapi.testclient import TestClient
|
2023-10-26 18:42:29 +02:00
|
|
|
from fastapi import FastAPI
|
2023-10-26 19:17:29 +02:00
|
|
|
import pytest
|
2021-08-17 12:27:16 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2023-03-01 07:35:21 +01:00
|
|
|
from khoj.configure import configure_routes, configure_search_types
|
|
|
|
from khoj.utils import state
|
2023-07-14 10:19:38 +02:00
|
|
|
from khoj.utils.state import search_models, content_index, config
|
2023-02-14 21:50:51 +01:00
|
|
|
from khoj.search_type import text_search, image_search
|
|
|
|
from khoj.utils.rawconfig import ContentConfig, SearchConfig
|
|
|
|
from khoj.processor.org_mode.org_to_jsonl import OrgToJsonl
|
2023-10-26 18:42:29 +02:00
|
|
|
from database.models import KhojUser
|
|
|
|
from database.adapters import EmbeddingsAdapters
|
2021-08-17 12:27:16 +02:00
|
|
|
|
|
|
|
|
2021-08-22 04:21:38 +02:00
|
|
|
# Test
|
2021-09-30 04:02:55 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 02:27:12 +01:00
|
|
|
def test_search_with_invalid_content_type(client):
|
2021-09-30 04:02:55 +02:00
|
|
|
# Arrange
|
2022-09-12 15:42:41 +02:00
|
|
|
user_query = quote("How to call Khoj from Emacs?")
|
2021-09-30 04:02:55 +02:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q={user_query}&t=invalid_content_type")
|
2021-09-30 04:02:55 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 02:27:12 +01:00
|
|
|
def test_search_with_valid_content_type(client):
|
2023-10-26 18:42:29 +02:00
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "github", "notion"]:
|
2021-09-30 04:02:55 +02:00
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q=random&t={content_type}")
|
2021-09-30 04:02:55 +02:00
|
|
|
# Assert
|
2023-07-19 04:34:09 +02:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2021-09-30 04:02:55 +02:00
|
|
|
|
|
|
|
|
2022-06-29 21:09:48 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 02:27:12 +01:00
|
|
|
def test_update_with_invalid_content_type(client):
|
2022-06-29 21:09:48 +02:00
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/update?t=invalid_content_type")
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-08-31 21:55:17 +02:00
|
|
|
def test_regenerate_with_invalid_content_type(client):
|
2023-07-29 03:47:56 +02:00
|
|
|
# Act
|
2023-08-31 21:55:17 +02:00
|
|
|
response = client.get(f"/api/update?force=true&t=invalid_content_type")
|
2023-07-29 03:47:56 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-08-31 21:55:17 +02:00
|
|
|
assert response.status_code == 422
|
2023-07-29 03:47:56 +02:00
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-17 13:30:27 +02:00
|
|
|
def test_index_update(client):
|
2023-08-31 21:55:17 +02:00
|
|
|
# Arrange
|
2023-10-13 01:16:51 +02:00
|
|
|
files = get_sample_files_data()
|
2023-08-31 21:55:17 +02:00
|
|
|
headers = {"x-api-key": "secret"}
|
|
|
|
|
2021-09-30 04:02:55 +02:00
|
|
|
# Act
|
2023-10-17 13:30:27 +02:00
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
2021-09-30 04:02:55 +02:00
|
|
|
|
|
|
|
# Assert
|
2023-08-31 21:55:17 +02:00
|
|
|
assert response.status_code == 200
|
2021-09-30 04:02:55 +02:00
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 02:27:12 +01:00
|
|
|
def test_regenerate_with_valid_content_type(client):
|
2023-10-26 18:42:29 +02:00
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "notion"]:
|
2023-08-31 21:55:17 +02:00
|
|
|
# Arrange
|
2023-10-13 01:16:51 +02:00
|
|
|
files = get_sample_files_data()
|
2023-08-31 21:55:17 +02:00
|
|
|
headers = {"x-api-key": "secret"}
|
|
|
|
|
2021-09-30 04:02:55 +02:00
|
|
|
# Act
|
2023-10-17 13:42:04 +02:00
|
|
|
response = client.post(f"/api/v1/index/update?t={content_type}", files=files, headers=headers)
|
2021-09-30 04:02:55 +02:00
|
|
|
# Assert
|
2023-07-19 04:34:09 +02:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2021-09-30 04:02:55 +02:00
|
|
|
|
|
|
|
|
2023-07-29 03:47:56 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_regenerate_with_github_fails_without_pat(client):
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/update?force=true&t=github")
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
# Arrange
|
2023-10-13 01:16:51 +02:00
|
|
|
files = get_sample_files_data()
|
2023-08-31 21:55:17 +02:00
|
|
|
headers = {"x-api-key": "secret"}
|
|
|
|
|
|
|
|
# Act
|
2023-10-17 13:42:04 +02:00
|
|
|
response = client.post(f"/api/v1/index/update?t=github", files=files, headers=headers)
|
2023-07-29 03:47:56 +02:00
|
|
|
# Assert
|
2023-08-31 21:55:17 +02:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: github"
|
2023-07-29 03:47:56 +02:00
|
|
|
|
|
|
|
|
2023-03-01 05:08:26 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db
|
2023-10-17 14:55:39 +02:00
|
|
|
@pytest.mark.skip(reason="Flaky test on parallel test runs")
|
2023-10-26 18:42:29 +02:00
|
|
|
def test_get_configured_types_via_api(client, sample_org_data):
|
2023-03-01 05:08:26 +01:00
|
|
|
# Act
|
2023-10-26 18:42:29 +02:00
|
|
|
text_search.setup(OrgToJsonl, sample_org_data, regenerate=False)
|
2023-03-01 07:35:21 +01:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
enabled_types = EmbeddingsAdapters.get_unique_file_types(user=None).all().values_list("file_type", flat=True)
|
2023-03-01 07:35:21 +01:00
|
|
|
|
|
|
|
# Assert
|
2023-10-26 18:42:29 +02:00
|
|
|
assert list(enabled_types) == ["org"]
|
2023-03-01 07:35:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:17:29 +02:00
|
|
|
def test_get_api_config_types(client, search_config: SearchConfig, sample_org_data, default_user2: KhojUser):
|
2023-03-01 07:35:21 +01:00
|
|
|
# Arrange
|
2023-10-26 19:17:29 +02:00
|
|
|
text_search.setup(OrgToJsonl, sample_org_data, regenerate=False, user=default_user2)
|
2023-10-26 20:37:41 +02:00
|
|
|
|
|
|
|
# Act
|
2023-03-01 07:35:21 +01:00
|
|
|
response = client.get(f"/api/config/types")
|
2023-10-26 20:37:41 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-10-26 19:17:29 +02:00
|
|
|
assert response.json() == ["all", "org", "image"]
|
2023-03-01 07:35:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_get_configured_types_with_no_content_config(fastapi_app: FastAPI):
|
2023-03-01 07:35:21 +01:00
|
|
|
# Arrange
|
|
|
|
state.SearchType = configure_search_types(config)
|
2023-10-26 18:42:29 +02:00
|
|
|
original_config = state.config.content_type
|
|
|
|
state.config.content_type = None
|
2023-03-01 07:35:21 +01:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
configure_routes(fastapi_app)
|
|
|
|
client = TestClient(fastapi_app)
|
2023-03-01 07:35:21 +01:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/config/types")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-06-29 05:10:57 +02:00
|
|
|
assert response.json() == ["all"]
|
2023-03-01 07:35:21 +01:00
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
# Restore
|
|
|
|
state.config.content_type = original_config
|
|
|
|
|
2023-03-01 07:35:21 +01:00
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:17:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-01 02:27:12 +01:00
|
|
|
def test_image_search(client, content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2023-07-14 10:19:38 +02:00
|
|
|
search_models.image_search = image_search.initialize_model(search_config.image)
|
|
|
|
content_index.image = image_search.setup(
|
|
|
|
content_config.image, search_models.image_search.image_encoder, regenerate=False
|
|
|
|
)
|
2023-02-17 17:04:26 +01:00
|
|
|
query_expected_image_pairs = [
|
|
|
|
("kitten", "kitten_park.jpg"),
|
|
|
|
("a horse and dog on a leash", "horse_dog.jpg"),
|
|
|
|
("A guinea pig eating grass", "guineapig_grass.jpg"),
|
|
|
|
]
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
for query, expected_image_name in query_expected_image_pairs:
|
2022-07-20 01:01:56 +02:00
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q={query}&n=1&t=image")
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-20 01:01:56 +02:00
|
|
|
actual_image = Image.open(BytesIO(client.get(response.json()[0]["entry"]).content))
|
|
|
|
expected_image = Image.open(content_config.image.input_directories[0].joinpath(expected_image_name))
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|
|
|
|
|
|
|
|
|
2021-08-17 12:27:16 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:17:29 +02:00
|
|
|
def test_notes_search(client, search_config: SearchConfig, sample_org_data, default_user2: KhojUser):
|
2021-09-30 05:24:27 +02:00
|
|
|
# Arrange
|
2023-10-26 19:17:29 +02:00
|
|
|
text_search.setup(OrgToJsonl, sample_org_data, regenerate=False, user=default_user2)
|
2022-09-12 15:42:41 +02:00
|
|
|
user_query = quote("How to git install application?")
|
2021-09-30 05:24:27 +02:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org&r=true")
|
2021-09-30 05:24:27 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-19 16:26:16 +02:00
|
|
|
# assert actual_data contains "Khoj via Emacs" entry
|
2022-07-20 18:33:27 +02:00
|
|
|
search_result = response.json()[0]["entry"]
|
2023-08-27 03:11:18 +02:00
|
|
|
assert "git clone https://github.com/khoj-ai/khoj" in search_result
|
2021-09-30 13:13:40 +02:00
|
|
|
|
|
|
|
|
2022-09-12 15:42:41 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-31 21:55:17 +02:00
|
|
|
def test_notes_search_with_only_filters(
|
2023-10-26 19:17:29 +02:00
|
|
|
client, content_config: ContentConfig, search_config: SearchConfig, sample_org_data, default_user2: KhojUser
|
2023-08-31 21:55:17 +02:00
|
|
|
):
|
2022-09-12 15:42:41 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
text_search.setup(
|
2023-08-31 21:55:17 +02:00
|
|
|
OrgToJsonl,
|
|
|
|
sample_org_data,
|
|
|
|
regenerate=False,
|
2023-10-26 19:17:29 +02:00
|
|
|
user=default_user2,
|
2023-02-17 17:04:26 +01:00
|
|
|
)
|
2022-09-12 15:42:41 +02:00
|
|
|
user_query = quote('+"Emacs" file:"*.org"')
|
|
|
|
|
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
2022-09-12 15:42:41 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual_data contains word "Emacs"
|
|
|
|
search_result = response.json()[0]["entry"]
|
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
2021-09-30 13:13:40 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:17:29 +02:00
|
|
|
def test_notes_search_with_include_filter(client, sample_org_data, default_user2: KhojUser):
|
2021-09-30 13:13:40 +02:00
|
|
|
# Arrange
|
2023-10-26 19:17:29 +02:00
|
|
|
text_search.setup(OrgToJsonl, sample_org_data, regenerate=False, user=default_user2)
|
2022-09-12 15:42:41 +02:00
|
|
|
user_query = quote('How to git install application? +"Emacs"')
|
2021-09-30 13:13:40 +02:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
2021-09-30 13:13:40 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-09-04 16:18:47 +02:00
|
|
|
# assert actual_data contains word "Emacs"
|
2022-07-20 18:33:27 +02:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 13:13:40 +02:00
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 18:42:29 +02:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:17:29 +02:00
|
|
|
def test_notes_search_with_exclude_filter(client, sample_org_data, default_user2: KhojUser):
|
2021-09-30 13:13:40 +02:00
|
|
|
# Arrange
|
2023-10-26 18:42:29 +02:00
|
|
|
text_search.setup(
|
2023-08-31 21:55:17 +02:00
|
|
|
OrgToJsonl,
|
|
|
|
sample_org_data,
|
|
|
|
regenerate=False,
|
2023-10-26 19:17:29 +02:00
|
|
|
user=default_user2,
|
2023-02-17 17:04:26 +01:00
|
|
|
)
|
2022-09-12 15:42:41 +02:00
|
|
|
user_query = quote('How to git install application? -"clone"')
|
2021-09-30 13:13:40 +02:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
2021-09-30 13:13:40 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-08-27 03:11:18 +02:00
|
|
|
# assert actual_data does not contains word "clone"
|
2022-07-20 18:33:27 +02:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 13:13:40 +02:00
|
|
|
assert "clone" not in search_result
|
2023-08-31 21:55:17 +02:00
|
|
|
|
|
|
|
|
2023-10-26 18:42:29 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_different_user_data_not_accessed(client, sample_org_data, default_user: KhojUser):
|
|
|
|
# Arrange
|
|
|
|
text_search.setup(OrgToJsonl, sample_org_data, regenerate=False, user=default_user)
|
|
|
|
user_query = quote("How to git install application?")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual response has no data as the default_user is different from the user making the query (anonymous)
|
|
|
|
assert len(response.json()) == 0
|
|
|
|
|
|
|
|
|
2023-08-31 21:55:17 +02:00
|
|
|
def get_sample_files_data():
|
|
|
|
return {
|
2023-10-13 01:16:51 +02:00
|
|
|
"files": ("path/to/filename.org", "* practicing piano", "text/org"),
|
|
|
|
"files": ("path/to/filename1.org", "** top 3 reasons why I moved to SF", "text/org"),
|
|
|
|
"files": ("path/to/filename2.org", "* how to build a search engine", "text/org"),
|
|
|
|
"files": ("path/to/filename.pdf", "Moore's law does not apply to consumer hardware", "application/pdf"),
|
|
|
|
"files": ("path/to/filename1.pdf", "The sun is a ball of helium", "application/pdf"),
|
|
|
|
"files": ("path/to/filename2.pdf", "Effect of sunshine on baseline human happiness", "application/pdf"),
|
|
|
|
"files": ("path/to/filename.txt", "data,column,value", "text/plain"),
|
|
|
|
"files": ("path/to/filename1.txt", "<html>my first web page</html>", "text/plain"),
|
|
|
|
"files": ("path/to/filename2.txt", "2021-02-02 Journal Entry", "text/plain"),
|
|
|
|
"files": ("path/to/filename.md", "# Notes from client call", "text/markdown"),
|
|
|
|
"files": (
|
|
|
|
"path/to/filename1.md",
|
|
|
|
"## Studying anthropological records from the Fatimid caliphate",
|
|
|
|
"text/markdown",
|
|
|
|
),
|
|
|
|
"files": ("path/to/filename2.md", "**Understanding science through the lens of art**", "text/markdown"),
|
2023-08-31 21:55:17 +02:00
|
|
|
}
|