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
|
|
|
|
|
2021-08-17 12:27:16 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
# Internal Packages
|
2022-08-06 01:47:52 +02:00
|
|
|
from src.main import app
|
2022-08-06 02:05:35 +02:00
|
|
|
from src.utils.state import model, config
|
2022-07-21 16:05:43 +02:00
|
|
|
from src.search_type import text_search, image_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
|
2022-09-05 00:05:13 +02:00
|
|
|
from src.search_filter.word_filter import WordFilter
|
2022-09-12 15:42:41 +02:00
|
|
|
from src.search_filter.file_filter import FileFilter
|
2021-08-17 12:27:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Arrange
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
client = TestClient(app)
|
|
|
|
|
2021-08-22 04:21:38 +02:00
|
|
|
# Test
|
2021-09-30 04:02:55 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:13:14 +01:00
|
|
|
def test_search_with_invalid_content_type():
|
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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_search_with_valid_content_type(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 04:02:55 +02:00
|
|
|
# Arrange
|
2022-01-15 02:13:14 +01:00
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
|
2021-12-11 20:14:31 +01:00
|
|
|
# config.content_type.image = search_config.image
|
2022-08-06 01:47:52 +02:00
|
|
|
for content_type in ["org", "markdown", "ledger", "music"]:
|
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
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2022-06-29 21:09:48 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-09-14 20:22:20 +02:00
|
|
|
def test_update_with_invalid_content_type():
|
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
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-09-14 20:22:20 +02:00
|
|
|
def test_update_with_valid_content_type(content_config: ContentConfig, search_config: SearchConfig):
|
2022-06-29 21:09:48 +02:00
|
|
|
# Arrange
|
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
|
2022-08-06 01:47:52 +02:00
|
|
|
for content_type in ["org", "markdown", "ledger", "music"]:
|
2022-06-29 21:09:48 +02:00
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/update?t={content_type}")
|
2022-06-29 21:09:48 +02:00
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:13:14 +01:00
|
|
|
def test_regenerate_with_invalid_content_type():
|
2021-09-30 04:02:55 +02:00
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/update?force=true&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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_regenerate_with_valid_content_type(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 04:02:55 +02:00
|
|
|
# Arrange
|
2022-01-15 02:13:14 +01:00
|
|
|
config.content_type = content_config
|
|
|
|
config.search_type = search_config
|
|
|
|
|
2022-07-21 19:57:57 +02:00
|
|
|
for content_type in ["org", "markdown", "ledger", "music", "image"]:
|
2021-09-30 04:02:55 +02:00
|
|
|
# Act
|
2022-10-08 12:16:08 +02:00
|
|
|
response = client.get(f"/api/update?force=true&t={content_type}")
|
2021-09-30 04:02:55 +02:00
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_image_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2022-01-15 02:13:14 +01: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-20 01:01:56 +02: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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_notes_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 05:24:27 +02:00
|
|
|
# Arrange
|
2022-09-14 09:53:43 +02:00
|
|
|
model.orgmode_search = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
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"]
|
2021-09-30 13:13:40 +02:00
|
|
|
assert "git clone" in search_result
|
|
|
|
|
|
|
|
|
2022-09-12 15:42:41 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_notes_search_with_only_filters(content_config: ContentConfig, search_config: SearchConfig):
|
|
|
|
# Arrange
|
|
|
|
filters = [WordFilter(), FileFilter()]
|
2022-09-14 09:53:43 +02:00
|
|
|
model.orgmode_search = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False, filters=filters)
|
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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_notes_search_with_include_filter(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 13:13:40 +02:00
|
|
|
# Arrange
|
2022-09-07 01:43:58 +02:00
|
|
|
filters = [WordFilter()]
|
2022-09-14 09:53:43 +02:00
|
|
|
model.orgmode_search = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False, filters=filters)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_notes_search_with_exclude_filter(content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 13:13:40 +02:00
|
|
|
# Arrange
|
2022-09-07 01:43:58 +02:00
|
|
|
filters = [WordFilter()]
|
2022-09-14 09:53:43 +02:00
|
|
|
model.orgmode_search = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False, filters=filters)
|
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
|
2022-09-04 16:18:47 +02:00
|
|
|
# assert actual_data does not 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 "clone" not in search_result
|