Skip to content

Commit

Permalink
Merge pull request #3084 from snbianco/ASB-28248-invalid-args
Browse files Browse the repository at this point in the history
Check that parameters are valid when running criteria queries
  • Loading branch information
bsipocz authored Aug 12, 2024
2 parents 1c1588e + 8b122a2 commit d347559
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ mast
- Increased the speed of ``mast.Observations.get_cloud_uris`` by obtaining multiple
URIs from MAST at once. [#3064]

- Present users with an error rather than a warning when nonexistent query criteria are used in ``mast.Observations.query_criteria``
and ``mast.Catalogs.query_criteria``. [#3084]


0.4.7 (2024-03-08)
==================
Expand Down
9 changes: 6 additions & 3 deletions astroquery/mast/discovery_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
This module contains various methods for querying the MAST Discovery Portal API.
"""

import difflib
import warnings
import uuid
import json
Expand All @@ -20,7 +21,7 @@
from ..query import BaseQuery
from ..utils import async_to_sync
from ..utils.class_or_instance import class_or_instance
from ..exceptions import InputWarning, NoResultsWarning, RemoteServiceError
from ..exceptions import InputWarning, InvalidQueryError, NoResultsWarning, RemoteServiceError

from . import conf, utils

Expand Down Expand Up @@ -406,8 +407,10 @@ def build_filter_set(self, column_config_name, service_name=None, **filters):
# Get the column type and separator
col_info = caom_col_config.get(colname)
if not col_info:
warnings.warn("Filter {} does not exist. This filter will be skipped.".format(colname), InputWarning)
continue
closest_match = difflib.get_close_matches(colname, caom_col_config.keys(), n=1)
error_msg = f"Filter '{colname}' does not exist. Did you mean '{closest_match[0]}'?" if closest_match \
else f"Filter '{colname}' does not exist."
raise InvalidQueryError(error_msg)

colType = "discrete"
if (col_info.get("vot.datatype", col_info.get("type")) in ("double", "float", "numeric")) \
Expand Down
22 changes: 22 additions & 0 deletions astroquery/mast/tests/test_mast_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ def test_observations_query_criteria(self):
intentType="calibration")
assert (result["intentType"] == "calibration").all()

def test_observations_query_criteria_invalid_keyword(self):
# attempt to make a criteria query with invalid keyword
with pytest.raises(InvalidQueryError) as err_no_alt:
Observations.query_criteria_count(not_a_keyword='TESS')
assert "Filter 'not_a_keyword' does not exist." in str(err_no_alt.value)

# keyword is close enough for difflib to offer alternative
with pytest.raises(InvalidQueryError) as err_with_alt:
Observations.query_criteria_count(oops_collection='TESS')
assert 'obs_collection' in str(err_with_alt.value)

# count functions
def test_observations_query_region_count(self):
maxRes = Observations.query_criteria_count()
Expand Down Expand Up @@ -881,6 +892,17 @@ def check_result(result, exp_vals):
assert isinstance(result, Table)
assert result['distance'][0] <= result['distance'][1]

def test_catalogs_query_criteria_invalid_keyword(self):
# attempt to make a criteria query with invalid keyword
with pytest.raises(InvalidQueryError) as err_no_alt:
Catalogs.query_criteria(catalog='tic', not_a_keyword='TESS')
assert "Filter 'not_a_keyword' does not exist." in str(err_no_alt.value)

# keyword is close enough for difflib to offer alternative
with pytest.raises(InvalidQueryError) as err_with_alt:
Catalogs.query_criteria(catalog='ctl', objectType="STAR")
assert 'objType' in str(err_with_alt.value)

def test_catalogs_query_hsc_matchid_async(self):
catalogData = Catalogs.query_object("M10",
radius=.001,
Expand Down

0 comments on commit d347559

Please sign in to comment.