2021-08-17 10:27:16 +00:00
|
|
|
# Standard Modules
|
2022-07-19 23:01:56 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
from fastapi.testclient import TestClient
|
2021-12-12 10:15:20 +00:00
|
|
|
import pytest
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
# Internal Packages
|
2021-12-05 14:31:39 +00:00
|
|
|
from src.main import app, model, config
|
2022-07-21 14:05:43 +00:00
|
|
|
from src.search_type import text_search, image_search
|
2022-01-15 01:54:38 +00:00
|
|
|
from src.utils.rawconfig import ContentConfig, SearchConfig
|
2022-07-21 14:05:43 +00:00
|
|
|
from src.processor.org_mode import org_to_jsonl
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Arrange
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
client = TestClient(app)
|
|
|
|
|
2021-08-22 02:21:38 +00:00
|
|
|
# Test
|
2021-09-30 02:02:55 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:13:14 +00:00
|
|
|
def test_search_with_invalid_content_type():
|
2021-09-30 02:02:55 +00:00
|
|
|
# Arrange
|
2022-07-19 14:26:16 +00:00
|
|
|
user_query = "How to call Khoj from Emacs?"
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Act
|
2022-01-15 01:13:14 +00:00
|
|
|
response = client.get(f"/search?q={user_query}&t=invalid_content_type")
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_search_with_valid_content_type(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 02:02:55 +00:00
|
|
|
# Arrange
|
2022-01-15 01:13:14 +00:00
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
|
2021-12-11 19:14:31 +00:00
|
|
|
# config.content_type.image = search_config.image
|
2022-07-21 17:57:57 +00:00
|
|
|
for content_type in ["org", "markdown", "ledger", "music", "image"]:
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2022-01-15 01:13:14 +00:00
|
|
|
response = client.get(f"/search?q=random&t={content_type}")
|
2021-09-30 02:02:55 +00:00
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2022-06-29 19:09:48 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_reload_with_invalid_content_type():
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/reload?t=invalid_content_type")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_reload_with_valid_content_type(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
|
2022-07-21 17:57:57 +00:00
|
|
|
for content_type in ["org", "markdown", "ledger", "music", "image"]:
|
2022-06-29 19:09:48 +00:00
|
|
|
# Act
|
|
|
|
response = client.get(f"/reload?t={content_type}")
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:13:14 +00:00
|
|
|
def test_regenerate_with_invalid_content_type():
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2022-01-15 01:13:14 +00:00
|
|
|
response = client.get(f"/regenerate?t=invalid_content_type")
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_regenerate_with_valid_content_type(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 02:02:55 +00:00
|
|
|
# Arrange
|
2022-01-15 01:13:14 +00:00
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
|
2022-07-21 17:57:57 +00:00
|
|
|
for content_type in ["org", "markdown", "ledger", "music", "image"]:
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2022-01-15 01:13:14 +00:00
|
|
|
response = client.get(f"/regenerate?t={content_type}")
|
2021-09-30 02:02:55 +00:00
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-10-03 03:28:33 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-12 10:15:20 +00:00
|
|
|
@pytest.mark.skip(reason="Flaky test. Search doesn't always return expected image path.")
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_image_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 03:28:33 +00:00
|
|
|
# Arrange
|
2022-01-15 01:13:14 +00:00
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
model.image_search = image_search.setup(content_config.image, search_config.image, regenerate=False)
|
2022-07-19 23:01:56 +00: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 03:28:33 +00:00
|
|
|
|
|
|
|
for query, expected_image_name in query_expected_image_pairs:
|
2022-07-19 23:01:56 +00:00
|
|
|
# Act
|
2021-10-03 03:28:33 +00:00
|
|
|
response = client.get(f"/search?q={query}&n=1&t=image")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-19 23:01:56 +00: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 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|
|
|
|
|
|
|
|
|
2021-08-17 10:27:16 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_notes_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 03:24:27 +00:00
|
|
|
# Arrange
|
2022-07-21 17:57:57 +00:00
|
|
|
model.orgmode_search = text_search.setup(org_to_jsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2021-09-30 11:13:40 +00:00
|
|
|
user_query = "How to git install application?"
|
2021-09-30 03:24:27 +00:00
|
|
|
|
|
|
|
# Act
|
2022-07-26 18:56:36 +00:00
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=org&r=true")
|
2021-09-30 03:24:27 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-19 14:26:16 +00:00
|
|
|
# assert actual_data contains "Khoj via Emacs" entry
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "git clone" in search_result
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_notes_search_with_include_filter(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 11:13:40 +00:00
|
|
|
# Arrange
|
2022-07-21 17:57:57 +00:00
|
|
|
model.orgmode_search = text_search.setup(org_to_jsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2021-09-30 11:13:40 +00:00
|
|
|
user_query = "How to git install application? +Emacs"
|
|
|
|
|
|
|
|
# Act
|
2022-07-21 17:57:57 +00:00
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=org")
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2021-10-03 02:46:29 +00:00
|
|
|
# assert actual_data contains explicitly included word "Emacs"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_notes_search_with_exclude_filter(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 11:13:40 +00:00
|
|
|
# Arrange
|
2022-07-21 17:57:57 +00:00
|
|
|
model.orgmode_search = text_search.setup(org_to_jsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2021-09-30 11:13:40 +00:00
|
|
|
user_query = "How to git install application? -clone"
|
|
|
|
|
|
|
|
# Act
|
2022-07-21 17:57:57 +00:00
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=org")
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual_data does not contains explicitly excluded word "Emacs"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "clone" not in search_result
|