haystack/test/test_rest_api.py
Lalit Pagaria abda994116
Pytest fix memory leak and put pytest marker on slow tests (#520)
* Clear faiss_index during teardown

* Marking slow test with pytest markers. So In future these test can be optimized. Also command line option can be added to skip them refer https://pytest.org/en/stable/example/simple.html#control-skipping-of-tests-according-to-command-line-option

* Fixing test
2020-10-26 19:19:10 +01:00

60 lines
2.0 KiB
Python

import pytest
from fastapi.testclient import TestClient
from haystack import Finder
from haystack.retriever.sparse import ElasticsearchRetriever
# TODO: Add integration tests for other APIs
def get_test_client_and_override_dependencies(reader, document_store_with_docs):
from rest_api.application import app
from rest_api.controller import search
search.document_store = document_store_with_docs
search.retriever = ElasticsearchRetriever(document_store=document_store_with_docs)
search.FINDERS = {1: Finder(reader=reader, retriever=search.retriever)}
return TestClient(app)
@pytest.mark.slow
@pytest.mark.parametrize("document_store_with_docs", ["elasticsearch"], indirect=True)
@pytest.mark.parametrize("reader", ["farm"], indirect=True)
def test_query_api(reader, document_store_with_docs):
client = get_test_client_and_override_dependencies(reader, document_store_with_docs)
query = {
"size": 1,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "Where Paul lives?"
}
}
],
"filter": [
{
"terms": {
"name": "filename2"
}
}
]
}
}
}
response = client.post(url="/models/1/query?top_k_reader=1", json=query)
assert 200 == response.status_code
response_json = response.json()
assert 1 == response_json['hits']['total']['value']
assert 1 == len(response_json['hits']['hits'])
assert response_json['hits']['hits'][0]["_score"] is not None
assert response_json['hits']['hits'][0]["_source"]["meta"] is not None
assert response_json['hits']['hits'][0]["_id"] is not None
assert "New York" == response_json['hits']['hits'][0]["_source"]["answer"]
assert "My name is Paul and I live in New York" == response_json['hits']['hits'][0]["_source"]["context"]