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

Related Entities: add tests for links from drug warnings

parent 4b9b286d
......@@ -3,10 +3,13 @@ Module with functions to make joins of entities
"""
import json
import base64
from datetime import datetime
from app.entities_joiner import standardisation
from app.entities_joiner import ids_loader
from app.url_shortening import url_shortener
from app.config import RUN_CONFIG
from app.usage_statistics import statistics_saver
class EntitiesJoinerError(Exception):
......@@ -76,6 +79,7 @@ def get_tiny_hash_to_related_items(destination_entity_browser_state_template,
destination_entity_browser_state_template,
previous_hash)
save_statistics(entity_from, entity_to, len(ids))
return join_state_hash
......@@ -139,3 +143,20 @@ def get_destination_url_hash(hashable_part):
shortening_response = url_shortener.shorten_url(hashable_part)
return shortening_response['hash']
def save_statistics(raw_entity_from, raw_entity_to, num_ids):
"""
:param raw_entity_from: source entity of the items
:param raw_entity_to: destination entity of the join
:param num_ids: number of ids joined
"""
statistics_document = {
'entity_from': raw_entity_from,
'entity_to': raw_entity_to,
'num_ids': num_ids,
'request_date': datetime.utcnow().timestamp() * 1000,
}
index_name = RUN_CONFIG.get('usage_statistics').get('entities_join_statistics_index')
statistics_saver.save_record_to_elasticsearch(statistics_document, index_name)
......@@ -30,6 +30,9 @@ class PossibleEntitiesTo(Enum):
CHEMBL_CELL_LINES = 'CHEMBL_CELL_LINES'
CHEMBL_TISSUES = 'CHEMBL_TISSUES'
CHEMBL_DRUG_WARNINGS = 'CHEMBL_DRUG_WARNINGS'
CHEMBL_DRUGS = 'CHEMBL_DRUGS'
CHEMBL_DRUG_MECHANISMS = 'CHEMBL_DRUG_MECHANISMS'
CHEMBL_DRUG_INDICATIONS = 'CHEMBL_DRUG_INDICATIONS'
class SelectionModes(Enum):
......@@ -73,6 +76,18 @@ JOIN_PROPERTIES = {
PossibleEntitiesTo.CHEMBL_COMPOUNDS: {
'from_property': 'drug_warning.molecule_chembl_id',
'to_property': 'molecule_chembl_id'
},
PossibleEntitiesTo.CHEMBL_DRUGS: {
'from_property': 'drug_warning.parent_molecule_chembl_id',
'to_property': 'molecule_chembl_id'
},
PossibleEntitiesTo.CHEMBL_DRUG_MECHANISMS: {
'from_property': 'drug_warning.parent_molecule_chembl_id',
'to_property': 'molecule_chembl_id'
},
PossibleEntitiesTo.CHEMBL_DRUG_INDICATIONS: {
'from_property': 'drug_warning.parent_molecule_chembl_id',
'to_property': 'molecule_chembl_id'
}
}
}
......@@ -82,12 +97,18 @@ JOIN_PROPERTIES = {
# This is used by the frontend to know which settings use to load the page, should be discarded in the future
SETTINGS_PATHS_FOR_TO_ENTITIES = {
PossibleEntitiesTo.CHEMBL_ACTIVITIES: 'ES_INDEXES_NO_MAIN_SEARCH.ACTIVITY',
PossibleEntitiesTo.CHEMBL_COMPOUNDS: 'ES_INDEXES_NO_MAIN_SEARCH.COMPOUND_COOL_CARDS'
PossibleEntitiesTo.CHEMBL_COMPOUNDS: 'ES_INDEXES_NO_MAIN_SEARCH.COMPOUND_COOL_CARDS',
PossibleEntitiesTo.CHEMBL_DRUGS: 'ES_INDEXES_NO_MAIN_SEARCH.DRUGS_LIST',
PossibleEntitiesTo.CHEMBL_DRUG_MECHANISMS: 'ES_INDEXES_NO_MAIN_SEARCH.MECHANISMS_OF_ACTION',
PossibleEntitiesTo.CHEMBL_DRUG_INDICATIONS: 'ES_INDEXES_NO_MAIN_SEARCH.DRUG_INDICATIONS'
}
BROWSER_NAMES_FOR_TO_ENTITIES = {
PossibleEntitiesTo.CHEMBL_ACTIVITIES: 'activities',
PossibleEntitiesTo.CHEMBL_COMPOUNDS: 'compounds'
PossibleEntitiesTo.CHEMBL_COMPOUNDS: 'compounds',
PossibleEntitiesTo.CHEMBL_DRUGS: 'drugs',
PossibleEntitiesTo.CHEMBL_DRUG_MECHANISMS: 'mechanisms_of_action',
PossibleEntitiesTo.CHEMBL_DRUG_INDICATIONS: 'drug_indications'
}
......
......@@ -515,14 +515,14 @@ paths:
description: 'Source entity for the join.'
required: true
type: 'string'
enum: [ 'CHEMBL_ACTIVITIES', 'CHEMBL_COMPOUNDS', 'CHEMBL_TARGETS', 'CHEMBL_ASSAYS', 'CHEMBL_DOCUMENTS', 'CHEMBL_CELL_LINES', 'CHEMBL_TISSUES', 'CHEMBL_DRUG_WARNINGS' ]
enum: [ 'CHEMBL_ACTIVITIES', 'CHEMBL_COMPOUNDS', 'CHEMBL_TARGETS', 'CHEMBL_ASSAYS', 'CHEMBL_DOCUMENTS', 'CHEMBL_CELL_LINES', 'CHEMBL_TISSUES', 'CHEMBL_DRUG_WARNINGS', 'CHEMBL_DRUGS' ]
default: 'CHEMBL_COMPOUNDS'
- name: 'entity_to'
in: 'formData'
description: 'The entity in ChEMBL to do the join with.'
required: true
type: 'string'
enum: [ 'CHEMBL_ACTIVITIES', 'CHEMBL_COMPOUNDS', 'CHEMBL_TARGETS', 'CHEMBL_ASSAYS', 'CHEMBL_DOCUMENTS', 'CHEMBL_CELL_LINES', 'CHEMBL_TISSUES', 'CHEMBL_DRUG_WARNINGS' ]
enum: [ 'CHEMBL_ACTIVITIES', 'CHEMBL_COMPOUNDS', 'CHEMBL_TARGETS', 'CHEMBL_ASSAYS', 'CHEMBL_DOCUMENTS', 'CHEMBL_CELL_LINES', 'CHEMBL_TISSUES', 'CHEMBL_DRUG_WARNINGS', 'CHEMBL_DRUGS', 'CHEMBL_DRUG_MECHANISMS', 'CHEMBL_DRUG_INDICATIONS' ]
default: 'CHEMBL_COMPOUNDS'
- name: 'es_query'
in: 'formData'
......
# pylint: disable=import-error
"""
Module that tests the endpoints to do joins among entities selecting no ids except some
Module that tests the endpoints to do joins among entities from drug warnings to compounds
"""
from specific_tests import utils
from specific_tests.entities_join import utils as entities_join_utils
......@@ -8,7 +8,7 @@ from specific_tests.entities_join import utils as entities_join_utils
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests doing a join among different entities selecting no ids except some
Tests doing a join among different entities from drug warnings to compounds
: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
"""
......
# pylint: disable=import-error
"""
Module that tests the endpoints to do joins among entities from drug warnings to compounds
"""
from specific_tests import utils
from specific_tests.entities_join import utils as entities_join_utils
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests doing a join among different entities from drug warnings to compounds
: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 joins among entities selecting all none except')
print('-------------------------------------------')
dataset_query = utils.load_json_data('functional_tests/specific_tests/data/entities_join_query_0.json')
selection_description = {"selectionMode": "allItemsExcept", "exceptions": []}
entities_join_utils.test_entities_join(dataset_query, selection_description, server_base_url,
entity_from='CHEMBL_DRUG_WARNINGS', entity_to='CHEMBL_DRUGS')
# pylint: disable=import-error
"""
Module that tests the endpoints to do joins among entities from drug warnings to drug indications
"""
from specific_tests import utils
from specific_tests.entities_join import utils as entities_join_utils
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests doing a join among different entities from drug warnings to drug indications
: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 joins among entities selecting all none except')
print('-------------------------------------------')
dataset_query = utils.load_json_data('functional_tests/specific_tests/data/entities_join_query_0.json')
selection_description = {"selectionMode": "allItemsExcept", "exceptions": []}
entities_join_utils.test_entities_join(dataset_query, selection_description, server_base_url,
entity_from='CHEMBL_DRUG_WARNINGS', entity_to='CHEMBL_DRUG_INDICATIONS')
# pylint: disable=import-error
"""
Module that tests the endpoints to do joins among entities from drug warnings to drug indications
"""
from specific_tests import utils
from specific_tests.entities_join import utils as entities_join_utils
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests doing a join among different entities from drug warnings to drug mechanisms
: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 joins among entities selecting all none except')
print('-------------------------------------------')
dataset_query = utils.load_json_data('functional_tests/specific_tests/data/entities_join_query_0.json')
selection_description = {"selectionMode": "allItemsExcept", "exceptions": []}
entities_join_utils.test_entities_join(dataset_query, selection_description, server_base_url,
entity_from='CHEMBL_DRUG_WARNINGS', entity_to='CHEMBL_DRUG_MECHANISMS')
......@@ -2,7 +2,7 @@
Module that runs all the tests related to the entities join
"""
from specific_tests.entities_join import fun_test_entities_join_0, fun_test_entities_join_1, fun_test_entities_join_2, \
fun_test_entities_join_3
fun_test_entities_join_3, fun_test_entities_join_4, fun_test_entities_join_5
def run_test(server_base_url, delayed_jobs_server_base_path):
......@@ -12,5 +12,5 @@ def run_test(server_base_url, delayed_jobs_server_base_path):
:param delayed_jobs_server_base_path: base path for the delayed_jobs
"""
for test_module in [fun_test_entities_join_0, fun_test_entities_join_1, fun_test_entities_join_2,
fun_test_entities_join_3]:
fun_test_entities_join_3, fun_test_entities_join_4, fun_test_entities_join_5]:
test_module.run_test(server_base_url, delayed_jobs_server_base_path)
......@@ -30,20 +30,16 @@ def test_entities_join(dataset_query, selection_description, server_base_url, en
request = requests.post(url, data=join_params)
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!'
url_hash_got = request.json()['tiny_hash']
print('url_hash_got: ', url_hash_got)
expansion_url = f'{server_base_url}/url_shortening/expand_url/{url_hash_got}'
print(f'Checking tiny hash: {expansion_url}')
request = requests.get(expansion_url)
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!'
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