def test_get_sorted_compounds_with_ri_and_pmz(requireMocking, compound_list): from cis import compounds conf = list( filter(lambda x: x['target_type'] == 'CONFIRMED', compound_list)) without = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(conf[0]['method']), 'version': urllib.parse.quote('fixed'), 'tgt_type': 'confirmed' # only type with identified name, ATM }, 'queryStringParameters': {} }, {}) assert without['statusCode'] == 200 filtered = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(conf[0]['method']), 'version': urllib.parse.quote('fixed'), 'tgt_type': 'confirmed' # only type with identified name, ATM }, 'queryStringParameters': { 'rivalue': conf[0]['ri'], 'pmzvalue': conf[0]['mass'] } }, {}) assert filtered['statusCode'] == 200 assert json.loads(without['body']) != json.loads(filtered['body'])
def test_query_silly1(requireMocking, libraries_with_confirmed): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']), 'tgt_type': urllib.parse.quote('CONFIRMED'), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'limit': 50, 'offset': 0, 'order_by': 'name', 'identified': 'true', 'name': 'unknown', 'direction': 'asc' } }, {}) assert response['statusCode'] in [200, 404] splashes = json.loads(response['body']) assert len(splashes) >= 0
def test_query_silly2(requireMocking, libraries_with_confirmed): from cis import compounds # querying unidentified compounds with name, which should NOT happen response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']), 'tgt_type': urllib.parse.quote('CONFIRMED'), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'limit': 50, 'offset': 0, 'order_by': 'name', 'identified': 'false', # this is the default 'name': 'test', 'direction': 'asc' } }, {}) assert response['statusCode'] == 404
def test_get_sorted_compounds_with_range(requireMocking, compound_list): from cis import compounds conf = list( filter(lambda x: x['target_type'] == 'UNCONFIRMED', compound_list)) splashes = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(conf[0]['method']), 'version': urllib.parse.quote('fixed'), 'tgt_type': urllib.parse.quote('unconfirmed') }, 'queryStringParameters': { 'limit': 10, 'order_by': 'pmz', 'pmzvalue': float(compound_list[0]['mass']) + 0.004, 'pmzaccuracy': 0.01 } }, {}) assert splashes['statusCode'] == 200 comps_obj = json.loads(splashes['body']) assert len(comps_obj) > 0 assert comps_obj[0]['precursor_mass'] >= float( compound_list[0]['mass']) - 0.05 assert comps_obj[-1]['precursor_mass'] <= float( compound_list[0]['mass']) + 0.05
def test_get_sorted_compounds_defaults(requireMocking, libraries_with_different_tgt_types): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_different_tgt_types[1]), 'version': 'fixed', 'tgt_type': 'unconfirmed' }, 'queryStringParameters': { 'limit': 150, 'offset': 0, 'order_by': 'ri', 'direction': 'asc' } }, {}) assert response['statusCode'] == 200 compounds = json.loads(response['body']) assert len(compounds) > 0
def test_query_bug2(requireMocking, libraries_with_unconfirmed): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_unconfirmed[0]['library']), 'version': urllib.parse.quote('fixed'), 'tgt_type': urllib.parse.quote('UNCONFIRMED') }, 'queryStringParameters': { 'limit': 150, 'offset': 0, 'order_by': 'name', 'identified': 'false', 'direction': 'asc' } }, {}) assert response['statusCode'] == 200 splashes = json.loads(response['body']) assert len(splashes) > 0 ids = list(map(lambda x: x['id'], splashes)) assert len(ids) == len(set(ids))
def test_get_sorted_compounds_names_any_identified_confirmed( requireMocking, libraries_with_confirmed): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']) }, 'queryStringParameters': { 'order_by': 'name', 'direction': 'desc' } }, {}) if response['statusCode'] == 404: pytest.skip( 'It is possible that we do not have confirmed compounds in the db') else: assert response['statusCode'] == 200 result = json.loads(response['body']) names = list(map(lambda x: x['preferred_name'], result)) assert any([n.lower().startswith('unknown') for n in names])
def test_get_sorted_compounds_ranges_gibberish(requireMocking, libraries_list, compound_list): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_list[0]), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'pmzvalue': 'boom' } }, {}) assert response['statusCode'] == 400 response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_list[0]), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'rivalue': 'blah - blah' } }, {}) assert response['statusCode'] == 400 response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_list[0]), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'rivalue': float(compound_list[0]['ri']), 'riaccuracy': 'hacked' } }, {}) assert response['statusCode'] == 400
def test_get_sorted_compounds_no_library(requireMocking): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': {}, 'queryStringParameters': {} }, {}) assert response['statusCode'] == 400 assert json.loads( response['body'])['error'] == 'you need to provide a "library" name'
def test_get_sorted_compounds_second_page(requireMocking, libraries_with_confirmed): from cis import compounds first = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'limit': 10, 'offset': 0 } }, {}) assert first['statusCode'] == 200 second = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'limit': 10, 'offset': 10 } }, {}) assert second['statusCode'] == 200 assert json.loads(first['body']) != json.loads(second['body'])
def test_get_sorted_compounds_filtered_version(requireMocking, libraries_list): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': libraries_list[0], 'version': '1' }, 'queryStringParameters': {} }, {}) assert response['statusCode'] in [200, 404] splashes = json.loads(response['body']) assert len(splashes) >= 0
def test_get_sorted_compounds_invalid_identified_names(requireMocking, libraries_list): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_list[0]), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'identified': 'blah' } }, {}) assert response['statusCode'] == http.HTTPStatus.INTERNAL_SERVER_ERROR assert json.loads(response['body'])['error'] == [ "Invalid value for queryString 'identified'. Please use 'true' or 'false'." ]
def test_get_sorted_compounds_name_filtered_default_order( requireMocking, compound_list): from cis import compounds splashes = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(compound_list[0]['method']), 'version': urllib.parse.quote(compound_list[0]['version']) }, 'queryStringParameters': { 'limit': 150, 'name': compound_list[0]['name'][3:7] } }, {}) assert splashes['statusCode'] == 200 comps_obj = json.loads(splashes['body']) assert len(comps_obj) >= 1
def test_get_sorted_compounds_name_filtered(requireMocking, compound_list): from cis import compounds splashes = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(compound_list[0]['method']), 'version': urllib.parse.quote('fixed'), 'tgt_type': compound_list[0]['target_type'] }, 'queryStringParameters': { 'limit': 30, 'order_by': 'pmz', 'name': compound_list[0]['name'] } }, {}) assert splashes['statusCode'] == 200 comps_obj = json.loads(splashes['body']) assert len(comps_obj) >= 1
def test_get_sorted_compounds_filtered_adduct_only(requireMocking, comps_with_adducts): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(comps_with_adducts[0]['method']), 'version': urllib.parse.quote('fixed'), 'tgt_type': comps_with_adducts[0]['target_type'] }, 'queryStringParameters': { 'adduct': '[M-H]-' } }, {}) assert response['statusCode'] == 200 splashes = json.loads(response['body']) assert len(splashes) >= 1
def test_get_sorted_compounds_queryString_none( requireMocking, libraries_with_different_tgt_types): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_different_tgt_types[-1]), 'version': urllib.parse.quote('fixed'), 'tgt_type': 'unconfirmed' }, 'queryStringParameters': None }, {}) assert response['statusCode'] == 200 compounds = json.loads(response['body']) assert len(compounds) > 0
def test_get_sorted_compounds_big_page(requireMocking, libraries_with_confirmed): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']), 'version': urllib.parse.quote('fixed') }, 'queryStringParameters': { 'limit': 100 } }, {}) assert response['statusCode'] == 200 compounds = json.loads(response['body']) assert len(compounds) > 10
def test_get_sorted_compounds_names_identified_confirmed( requireMocking, libraries_with_confirmed): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote( '5m splash one | orbitrap | beh c18 | negative') }, 'queryStringParameters': { 'identified': 'true' } }, {}) assert response['statusCode'] == 200 result = json.loads(response['body']) for c in result: assert len(c['associated_names']) > 0 or c['preferred_name'] != ''
def test_get_sorted_compounds_hidden(requireMocking, libs_with_hidden_comps): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libs_with_hidden_comps[-1]), 'version': 'fixed', 'tgt_type': 'unconfirmed' }, 'queryStringParameters': { 'hidden': 'x' # whatever value, value not used. } }, {}) assert response['statusCode'] == 200 compounds = json.loads(response['body'], use_decimal=True) assert all([x['hidden'] for x in compounds])
def test_get_sorted_compounds_filtered_lowercase_adduct_only( requireMocking, libraries_list): from cis import compounds [print(i, x) for i, x in enumerate(libraries_list)] response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_list[5]), 'version': urllib.parse.quote('fixed'), 'tgt_type': 'ISTD' }, 'queryStringParameters': { 'adduct': urllib.parse.quote('[m-h]-') } }, {}) assert response['statusCode'] in [200, 404] splashes = json.loads(response['body']) assert len(splashes) >= 0
def test_get_sorted_compounds_by_type(requireMocking, libraries_with_confirmed): from cis import compounds response = compounds.get_sorted_compounds( { 'pathParameters': { 'library': urllib.parse.quote(libraries_with_confirmed[0]['library']), 'tgt_type': urllib.parse.quote('confirmed'), 'version': urllib.parse.quote(urllib.parse.quote('fixed')) }, 'queryStringParameters': { 'limit': 150, 'order_by': 'splash' } }, {}) compounds = json.loads(response['body']) tgt_type = compounds[0]['target_type'] assert tgt_type == 'CONFIRMED'