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

Entities Joiner: implement getting ids query for none except some

parent 23b2075a
......@@ -49,4 +49,5 @@ def get_tiny_hash_to_related_items(destination_entity_browser_state_template,
if entity_to == entity_from:
raise EntitiesJoinerError(f'entity_to ({entity_to}) and entity_from ({entity_from}) cannot be the same!')
print('get_tiny_hash_to_related_items')
return 'holaaaa'
......@@ -17,22 +17,65 @@ def get_ids_query(es_query, selection_description, from_property):
if parsed_selection_mode == standardisation.SelectionModes.ALL_ITEMS_EXCEPT:
if len(exceptions) == 0:
return {
'query': es_query.get('query'),
"_source": [from_property],
}
return get_ids_query_for_all_items(es_query, from_property)
else:
dataset_query = es_query.get('query')
if dataset_query.get('bool') is None:
dataset_query['bool'] = {}
if dataset_query.get('bool').get('must_not') is None:
dataset_query['bool']['must_not'] = []
dataset_query['bool']['must_not'].append({
'terms': {
from_property: exceptions
}
})
return {
'query': dataset_query,
"_source": [from_property],
return get_ids_query_for_all_items_except_some(es_query, from_property, exceptions)
# Selecting none except some
return get_ids_query_for_no_items_except_some(es_query, from_property, exceptions)
def get_ids_query_for_all_items(es_query, from_property):
"""
:param es_query: uery for the dataset
:param from_property: property to get to to the join
:return: the ids query for all items
"""
return {
'query': es_query.get('query'),
"_source": [from_property],
}
def get_ids_query_for_all_items_except_some(es_query, from_property, exceptions):
"""
:param es_query: query for the dataset
:param from_property: property to get to to the join
:param exceptions: selection exceptions
:return: the ids query for all items except some
"""
dataset_query = es_query.get('query')
if dataset_query.get('bool') is None:
dataset_query['bool'] = {}
if dataset_query.get('bool').get('must_not') is None:
dataset_query['bool']['must_not'] = []
dataset_query['bool']['must_not'].append({
'terms': {
from_property: exceptions
}
})
return {
'query': dataset_query,
"_source": [from_property],
}
def get_ids_query_for_no_items_except_some(es_query, from_property, exceptions):
"""
:param es_query: query for the dataset
:param from_property: property to get to to the join
:param exceptions: selection exceptions
:return: the ids query for all items except some
"""
dataset_query = es_query.get('query')
dataset_query['bool'] = {
'filter': [{
'terms': {
from_property: exceptions
}
}]
}
return {
'query': dataset_query,
"_source": [from_property],
}
{
"query": {
"bool": {
"filter": [],
"should": [],
"must_not": []
}
},
"track_total_hits": true,
"size": 20,
"from": 0,
"_source": [
"drug_warning.molecule_chembl_id",
"drug_warning.parent_molecule_chembl_id",
"drug_warning.warning_type",
"drug_warning.warning_class",
"drug_warning.warning_description",
"drug_warning.where",
"drug_warning.warning_refs"
],
"sort": []
}
\ No newline at end of file
......@@ -2,10 +2,23 @@
Module to test the ids loader
"""
import unittest
import os
import json
from app.entities_joiner import ids_loader
def load_sample_query(filename):
"""
:param filename: name of the file to read
:return: the dict with the query contained in the file in the data directory of these tests
"""
filepath = os.path.join(os.path.dirname(__file__), 'data', filename)
with open(filepath) as query_file:
return json.loads(query_file.read())
class TestIDsLoader(unittest.TestCase):
"""
Class to test the ids loader module
......@@ -15,28 +28,7 @@ class TestIDsLoader(unittest.TestCase):
"""
test that it generates the required query when selected all items in dataset
"""
es_query = {
"query": {
"bool": {
"filter": [],
"should": [],
"must_not": []
}
},
"track_total_hits": True,
"size": 20,
"from": 0,
"_source": [
"drug_warning.molecule_chembl_id",
"drug_warning.parent_molecule_chembl_id",
"drug_warning.warning_type",
"drug_warning.warning_class",
"drug_warning.warning_description",
"drug_warning.where",
"drug_warning.warning_refs"
],
"sort": []
}
es_query = load_sample_query('sample_query_0.json')
selection_description = {
"selectionMode": "allItemsExcept",
......@@ -64,28 +56,7 @@ class TestIDsLoader(unittest.TestCase):
"""
test that it generates the required query when selected all items in dataset
"""
es_query = {
"query": {
"bool": {
"filter": [],
"should": [],
"must_not": []
}
},
"track_total_hits": True,
"size": 20,
"from": 0,
"_source": [
"drug_warning.molecule_chembl_id",
"drug_warning.parent_molecule_chembl_id",
"drug_warning.warning_type",
"drug_warning.warning_class",
"drug_warning.warning_description",
"drug_warning.where",
"drug_warning.warning_refs"
],
"sort": []
}
es_query = load_sample_query('sample_query_0.json')
selection_description = {
"selectionMode": "allItemsExcept",
......@@ -115,7 +86,32 @@ class TestIDsLoader(unittest.TestCase):
self.assertDictEqual(ids_query_must_be, ids_query_got,
msg='The query was not generated correctly when selecting all items except some!')
def test_generates_query_to_get_ids_when_selecting_none(self):
def test_generates_query_to_get_ids_when_selecting_none_except_some(self):
"""
test that it generates the required query when select in dataset
test that it generates the required query when selecting none except zone
"""
es_query = load_sample_query('sample_query_0.json')
selection_description = {
"selectionMode": "noItemsExcept",
"exceptions": ['CHEMBL3989861', 'CHEMBL3989724', 'CHEMBL64']
}
from_property = 'drug_warning.molecule_chembl_id'
ids_query_must_be = {
"query": {
"bool": {
"filter": [{
"terms": {
from_property: selection_description['exceptions']
}
}],
}
},
"_source": [from_property],
}
ids_query_got = ids_loader.get_ids_query(es_query, selection_description, from_property)
self.assertDictEqual(ids_query_must_be, ids_query_got,
msg='The query was not generated correctly when selecting no items except some!')
......@@ -12,7 +12,7 @@ from specific_tests import fun_test_simple_query, fun_test_query_with_context, \
fun_test_go_slim_target_classification, fun_test_in_vivo_assay_classification, \
fun_test_organism_taxonomy_target_classification, fun_test_protein_target_classification, \
fun_test_covid_entities_records, fun_test_database_summary, fun_test_entities_records, \
fun_test_get_all_properties, fun_test_identify_separator
fun_test_get_all_properties, fun_test_identify_separator, fun_test_entities_join_0
PARSER = argparse.ArgumentParser()
PARSER.add_argument('server_base_path', help='server base path to run the tests against',
......@@ -28,15 +28,7 @@ def run():
"""
print(f'Running functional tests on {ARGS.server_base_path}')
for test_module in [fun_test_simple_query, fun_test_query_with_context, fun_test_query_with_search_by_ids_context,
fun_test_property_config,
fun_test_group_config, fun_test_facets_group_config, fun_test_get_document,
fun_test_id_properties, fun_test_get_context_data, fun_test_search_parsing,
fun_test_url_shortening, fun_test_element_usage, fun_test_go_slim_target_classification,
fun_test_in_vivo_assay_classification, fun_test_organism_taxonomy_target_classification,
fun_test_protein_target_classification, fun_test_covid_entities_records,
fun_test_database_summary, fun_test_entities_records, fun_test_get_all_properties,
fun_test_identify_separator]:
for test_module in [fun_test_entities_join_0]:
test_module.run_test(ARGS.server_base_path, ARGS.delayed_jobs_server_base_path)
......
# pylint: disable=import-error
"""
Module that tests the endpoints to do joins among entities
Module that tests the endpoints to do joins among entities selecting all ids
"""
import json
import requests
def run_test(server_base_url, delayed_jobs_server_base_path):
"""
Tests doing a join among different entities
Tests doing a join among different entities selecting all ids
: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
"""
......@@ -15,12 +17,67 @@ def run_test(server_base_url, delayed_jobs_server_base_path):
print('-------------------------------------------')
print('Testing joins among entities')
print('-------------------------------------------')
dataset_query = {
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"terms": {
"drug_warning.warning_class": [
"Hepatotoxicity"
]
}
}
]
}
},
{
"bool": {
"should": [
{
"terms": {
"drug_warning.where.country": [
"United Kingdom"
]
}
}
]
}
}
],
"should": [],
"must_not": []
}
},
"track_total_hits": True,
"size": 20,
"from": 0,
"_source": [
"drug_warning.molecule_chembl_id",
"drug_warning.parent_molecule_chembl_id",
"drug_warning.warning_type",
"drug_warning.warning_class",
"drug_warning.warning_description",
"drug_warning.where",
"drug_warning.warning_refs"
],
"sort": []
}
selection_description = {"selectionMode": "allItemsExcept", "exceptions": []}
join_params = {
'destination_entity_browser_state_template': 'wwwdev.ebi.ac.uk/chembl/g/#browse/<BROWSER_NAME>/full_state/<GENERATED_STATE>',
'entity_from': 'CHEMBL_DRUG_WARNINGS',
'entity_to': 'CHEMBL_ACTIVITIES',
'es_query': '',
'es_query': json.dumps(dataset_query),
'selection_description': json.dumps(selection_description)
}
url = f'{server_base_url}/entities_join/get_link_to_related_items'
print('doing post')
request = requests.post(url, data=join_params)
print('post done!')
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