Commit 84154930 authored by David Mendez's avatar David Mendez
Browse files

Add functional test for query with context

parent 4190541c
......@@ -5,11 +5,14 @@
"""
import argparse
from specific_tests import fun_test_simple_query
from specific_tests import fun_test_simple_query, fun_test_query_with_context
PARSER = argparse.ArgumentParser()
PARSER.add_argument('server_base_path', help='server base path to run the tests against',
default='http://127.0.0.1:5000', nargs='?')
PARSER.add_argument('delayed_jobs_server_base_path', help='server base path to run the tests against',
default='https://www.ebi.ac.uk/chembl/interface_api/delayed_jobs', nargs='?')
ARGS = PARSER.parse_args()
......@@ -19,8 +22,8 @@ def run():
"""
print(f'Running functional tests on {ARGS.server_base_path}')
for test_module in [fun_test_simple_query]:
test_module.run_test(ARGS.server_base_path)
for test_module in [fun_test_simple_query, fun_test_query_with_context]:
test_module.run_test(ARGS.server_base_path, ARGS.delayed_jobs_server_base_path)
if __name__ == "__main__":
......
# pylint: disable=import-error
"""
Module that tests an es_query with context
"""
import json
import requests
from specific_tests import utils
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests that a simple query to elasticsearch
:param server_base_url: base url of the running server. E.g. http://127.0.0.1:5000
:param delayed_jobs_server_base_path: base path for the delayed_jobs
"""
print('-------------------------------------------')
print('Testing a es_query with context')
print('-------------------------------------------')
job_id = submit_similarity_search_job(delayed_jobs_server_base_path)
wait_until_job_finished(delayed_jobs_server_base_path, job_id)
print('Now going to make a request with this context')
url = utils.get_url_for_get_es_data(server_base_url)
print('url: ', url)
es_query = {
"size": 24,
"from": 0,
"query": {
"bool": {
"must": [{"query_string": {"analyze_wildcard": 'true', "query": "*"}}],
"filter": []
}
},
"sort": []
}
context_obj = {
"delayed_jobs_base_url": delayed_jobs_server_base_path,
"context_type": "SIMILARITY",
"context_id": "STRUCTURE_SEARCH-V4s_piFIe9FL7sOuJ0jl6U0APMEsoV-b4HfFE_dtojc=",
}
payload = {
'index_name': 'chembl_26_molecule',
'es_query': json.dumps(es_query),
'context_obj': json.dumps(context_obj),
'id_property': 'molecule_chembl_id',
'contextual_sort_data': {},
}
print('payload: ', payload)
request = requests.post(url, data=payload)
status_code = request.status_code
print(f'status_code: {status_code}')
response_text = request.text
utils.print_es_response(response_text)
assert status_code == 200, 'The request failed!'
response_json = request.json()
hits = response_json['es_response']['hits']['hits']
assert len(hits) > 0, 'I should have gotten hits!'
def submit_similarity_search_job(delayed_jobs_server_base_path):
"""
:param delayed_jobs_server_base_path: base path of the delayed jobs server
:return: job_id
"""
print('Launching a similarity search job...')
submission_url = utils.get_url_for_similarity_job_submission(delayed_jobs_server_base_path)
print('submission_url: ', submission_url)
payload = {
'search_type': 'SIMILARITY',
'search_term': 'NCCc1ccc(O)c(O)c1',
'threshold': 40,
'dl__ignore_cache': False
}
print('payload: ', payload)
submit_request = requests.post(submission_url, data=payload)
submission_status_code = submit_request.status_code
print(f'submission_status_code: {submission_status_code}')
assert submission_status_code == 200, 'Job could not be submitted!'
submission_response = submit_request.json()
job_id = submission_response.get('job_id')
print('job_id: ', job_id)
return job_id
def wait_until_job_finished(delayed_jobs_server_base_path, job_id):
"""
Waits until the job finished
:param delayed_jobs_server_base_path: base path of the delayed jobs server
:param job_id: id of the job
"""
print('Waiting until job finishes...')
status_url = utils.get_url_for_job_status(delayed_jobs_server_base_path, job_id)
print('status_url: ', status_url)
status_request = requests.get(status_url)
print('Status request response code: ', status_request.status_code)
job_status = None
while job_status != 'FINISHED':
status_response = status_request.json()
job_status = status_response.get('status')
job_progress = status_response.get('progress')
print('job_status: ', job_status)
print('job_progress: ', job_progress)
......@@ -9,10 +9,11 @@ import requests
from specific_tests import utils
def run_test(server_base_url):
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests that a simple query to elasticsearch
:param server_base_url: base url of the running server. E.g. http://127.0.0.1:5000
:param delayed_jobs_server_base_path: base path for the delayed_jobs
"""
print('-------------------------------------------')
......
......@@ -11,6 +11,23 @@ def get_url_for_get_es_data(server_base_url):
return f'{server_base_url}/es_data/get_es_data'
def get_url_for_job_status(delayed_jobs_base_url, job_id):
"""
:param delayed_jobs_base_url: base url for the delayed jobs. E.g. https://www.ebi.ac.uk/chembl/interface_api/delayed_jobs
:param job_id: job_id
:return: url for getting a job status
"""
return f'{delayed_jobs_base_url}/status/{job_id}'
def get_url_for_similarity_job_submission(delayed_jobs_base_url):
"""
:param delayed_jobs_base_url: base url for the delayed jobs. E.g. https://www.ebi.ac.uk/chembl/interface_api/delayed_jobs
:return: url for submitting a similarity search job
"""
return f'{delayed_jobs_base_url}/submit/structure_search_job'
def print_es_response(response_text, max_chars=200):
"""
prints the response text passed as parameter up to max_chars
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment