2021-08-17 03:27:16 -07:00
|
|
|
# Standard Modules
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
# Internal Packages
|
2021-09-30 02:04:04 -07:00
|
|
|
from main import app, search_config, model
|
2021-08-17 03:27:16 -07:00
|
|
|
from search_type import asymmetric
|
2021-09-30 02:04:04 -07:00
|
|
|
from utils.config import SearchConfig, TextSearchConfig
|
2021-08-17 03:27:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Arrange
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
|
2021-08-21 19:21:38 -07:00
|
|
|
# Test
|
2021-09-29 19:02:55 -07: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-29 20:47:58 -07:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-09-29 19:02:55 -07:00
|
|
|
def test_search_with_valid_search_type():
|
|
|
|
# Arrange
|
|
|
|
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-29 20:47:58 -07:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-09-29 19:02:55 -07: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-29 20:47:58 -07:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-09-29 19:02:55 -07:00
|
|
|
def test_regenerate_with_valid_search_type():
|
|
|
|
# Arrange
|
|
|
|
for search_type in ["notes", "ledger", "music", "image"]:
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/regenerate?t={search_type}")
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-08-17 03:27:16 -07:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-09-29 20:24:27 -07:00
|
|
|
def test_notes_search():
|
2021-08-21 19:21:38 -07:00
|
|
|
# Arrange
|
2021-09-30 02:04:04 -07:00
|
|
|
search_config = SearchConfig()
|
|
|
|
search_config.notes = TextSearchConfig(
|
|
|
|
input_files = [Path('tests/data/main_readme.org'), Path('tests/data/interface_emacs_readme.org')],
|
|
|
|
input_filter = None,
|
|
|
|
compressed_jsonl = Path('tests/data/.test.jsonl.gz'),
|
|
|
|
embeddings_file = Path('tests/data/.test_embeddings.pt'),
|
|
|
|
verbose = 0)
|
2021-08-21 19:21:38 -07:00
|
|
|
|
2021-08-17 03:27:16 -07:00
|
|
|
# Act
|
2021-09-29 20:24:27 -07:00
|
|
|
# Regenerate embeddings during asymmetric setup
|
2021-09-30 02:04:04 -07:00
|
|
|
notes_model = asymmetric.setup(search_config.notes, regenerate=True)
|
2021-08-17 03:27:16 -07:00
|
|
|
|
|
|
|
# Assert
|
2021-09-29 20:24:27 -07:00
|
|
|
assert len(notes_model.entries) == 10
|
|
|
|
assert len(notes_model.corpus_embeddings) == 10
|
|
|
|
|
|
|
|
# Arrange
|
|
|
|
model.notes_search = notes_model
|
|
|
|
user_query = "How to call semantic search from Emacs?"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/search?q={user_query}&n=1&t=notes")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual_data contains "Semantic Search via Emacs"
|
|
|
|
search_result = response.json()[0]["Entry"]
|
|
|
|
assert "Semantic Search via Emacs" in search_result
|
|
|
|
|
|
|
|
|
2021-09-30 02:04:04 -07:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_notes_regenerate():
|
|
|
|
# Arrange
|
|
|
|
search_config = SearchConfig()
|
|
|
|
search_config.notes = TextSearchConfig(
|
|
|
|
input_files = [Path('tests/data/main_readme.org'), Path('tests/data/interface_emacs_readme.org')],
|
|
|
|
input_filter = None,
|
|
|
|
compressed_jsonl = Path('tests/data/.test.jsonl.gz'),
|
|
|
|
embeddings_file = Path('tests/data/.test_embeddings.pt'),
|
|
|
|
verbose = 0)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Regenerate embeddings during asymmetric setup
|
|
|
|
notes_model = asymmetric.setup(search_config.notes, regenerate=True)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(notes_model.entries) == 10
|
|
|
|
assert len(notes_model.corpus_embeddings) == 10
|
|
|
|
|
|
|
|
# Arrange
|
|
|
|
model.notes_search = notes_model
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/regenerate?t=notes")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|