2021-08-17 12:27:16 +02:00
|
|
|
# Standard Modules
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
# External Packages
|
|
|
|
from fastapi.testclient import TestClient
|
2021-12-12 11:15:20 +01:00
|
|
|
import pytest
|
2021-08-17 12:27:16 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2021-12-05 15:31:39 +01:00
|
|
|
from src.main import app, model, config
|
2021-09-30 13:12:14 +02:00
|
|
|
from src.search_type import asymmetric, image_search
|
|
|
|
from src.utils.helpers import resolve_absolute_path
|
2021-12-11 20:14:31 +01:00
|
|
|
from src.utils.rawconfig import ContentTypeConfig
|
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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_search_with_invalid_search_type():
|
|
|
|
# Arrange
|
|
|
|
user_query = "How to call semantic search from Emacs?"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/search?q={user_query}&t=invalid_search_type")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-11 20:14:31 +01:00
|
|
|
def test_search_with_valid_search_type(search_config: ContentTypeConfig):
|
2021-09-30 04:02:55 +02:00
|
|
|
# Arrange
|
2021-12-11 20:14:31 +01:00
|
|
|
config.content_type = search_config
|
|
|
|
# config.content_type.image = search_config.image
|
2021-09-30 04:02:55 +02:00
|
|
|
for search_type in ["notes", "ledger", "music", "image"]:
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/search?q=random&t={search_type}")
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-09-30 04:02:55 +02:00
|
|
|
def test_regenerate_with_invalid_search_type():
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/regenerate?t=invalid_search_type")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 05:47:58 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-11 20:14:31 +01:00
|
|
|
def test_regenerate_with_valid_search_type(search_config: ContentTypeConfig):
|
2021-09-30 04:02:55 +02:00
|
|
|
# Arrange
|
2021-12-11 20:14:31 +01:00
|
|
|
config.content_type = search_config
|
2021-09-30 04:02:55 +02:00
|
|
|
for search_type in ["notes", "ledger", "music", "image"]:
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/regenerate?t={search_type}")
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-12 11:15:20 +01:00
|
|
|
@pytest.mark.skip(reason="Flaky test. Search doesn't always return expected image path.")
|
2021-12-11 20:14:31 +01:00
|
|
|
def test_image_search(search_config: ContentTypeConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2021-12-11 20:14:31 +01:00
|
|
|
config.content_type = search_config
|
2021-10-03 05:28:33 +02:00
|
|
|
model.image_search = image_search.setup(search_config.image, regenerate=False)
|
|
|
|
query_expected_image_pairs = [("brown kitten next to fallen plant", "kitten_park.jpg"),
|
|
|
|
("a horse and dog on a leash", "horse_dog.jpg"),
|
|
|
|
("A guinea pig eating grass", "guineapig_grass.jpg")]
|
|
|
|
|
|
|
|
# Act
|
|
|
|
for query, expected_image_name in query_expected_image_pairs:
|
|
|
|
response = client.get(f"/search?q={query}&n=1&t=image")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
actual_image = Path(response.json()[0]["Entry"])
|
|
|
|
expected_image = resolve_absolute_path(search_config.image.input_directory.joinpath(expected_image_name))
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|
|
|
|
|
|
|
|
|
2021-08-17 12:27:16 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-11 20:14:31 +01:00
|
|
|
def test_notes_search(search_config: ContentTypeConfig):
|
2021-09-30 05:24:27 +02:00
|
|
|
# Arrange
|
2021-12-11 20:14:31 +01:00
|
|
|
model.notes_search = asymmetric.setup(search_config.org, regenerate=False)
|
2021-09-30 13:13:40 +02:00
|
|
|
user_query = "How to git install application?"
|
2021-09-30 05:24:27 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=notes")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2021-10-03 05:28:33 +02:00
|
|
|
# assert actual_data contains "Semantic Search via Emacs" entry
|
2021-09-30 05:24:27 +02:00
|
|
|
search_result = response.json()[0]["Entry"]
|
2021-09-30 13:13:40 +02:00
|
|
|
assert "git clone" in search_result
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-11 20:14:31 +01:00
|
|
|
def test_notes_search_with_include_filter(search_config: ContentTypeConfig):
|
2021-09-30 13:13:40 +02:00
|
|
|
# Arrange
|
2021-12-11 20:14:31 +01:00
|
|
|
model.notes_search = asymmetric.setup(search_config.org, regenerate=False)
|
2021-09-30 13:13:40 +02:00
|
|
|
user_query = "How to git install application? +Emacs"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=notes")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2021-10-03 04:46:29 +02:00
|
|
|
# assert actual_data contains explicitly included word "Emacs"
|
2021-09-30 13:13:40 +02:00
|
|
|
search_result = response.json()[0]["Entry"]
|
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-12-11 20:14:31 +01:00
|
|
|
def test_notes_search_with_exclude_filter(search_config: ContentTypeConfig):
|
2021-09-30 13:13:40 +02:00
|
|
|
# Arrange
|
2021-12-11 20:14:31 +01:00
|
|
|
model.notes_search = asymmetric.setup(search_config.org, regenerate=False)
|
2021-09-30 13:13:40 +02:00
|
|
|
user_query = "How to git install application? -clone"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=notes")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual_data does not contains explicitly excluded word "Emacs"
|
|
|
|
search_result = response.json()[0]["Entry"]
|
|
|
|
assert "clone" not in search_result
|