diff --git a/src/dashboard/get_chart_data/get_chart_data.py b/src/dashboard/get_chart_data/get_chart_data.py index 1348dc9..38ec8b7 100644 --- a/src/dashboard/get_chart_data/get_chart_data.py +++ b/src/dashboard/get_chart_data/get_chart_data.py @@ -159,7 +159,7 @@ def _build_query(query_params: dict, filter_groups: list, path_params: dict) -> config_params.append(params) if filter_config[1] in NONE_FILTERS or filter_config[0] != query_params["column"]: if params.get("bound", "").casefold() == "none": - params["bound"] == "cumulus__none" + params["bound"] = "cumulus__none" none_params.append(params) else: raise errors.AggregatorFilterError( diff --git a/tests/dashboard/test_get_chart_data.py b/tests/dashboard/test_get_chart_data.py index c6ebcaf..fb52f12 100644 --- a/tests/dashboard/test_get_chart_data.py +++ b/tests/dashboard/test_get_chart_data.py @@ -1,7 +1,9 @@ import json import os +from contextlib import nullcontext as does_not_raise from unittest import mock +import botocore import pandas import pytest @@ -29,7 +31,7 @@ def mock_data_frame(filter_param): @mock.patch("src.dashboard.get_chart_data.get_chart_data._get_table_cols", mock_get_table_cols) @mock.patch.dict(os.environ, MOCK_ENV) @pytest.mark.parametrize( - "query_params,filter_groups,path_params,query_str", + "query_params,filter_groups,path_params,query_str, raises", [ ( {"column": "gender"}, @@ -54,6 +56,7 @@ def mock_data_frame(filter_param): ) ORDER BY "gender\"""", + does_not_raise(), ), ( {"column": "gender", "stratifier": "race"}, @@ -77,6 +80,7 @@ def mock_data_frame(filter_param): AND "race" IS NOT NULL ORDER BY "race", "gender\"""", + does_not_raise(), ), ( {"column": "gender"}, @@ -115,10 +119,11 @@ def mock_data_frame(filter_param): ) ORDER BY "gender\"""", + does_not_raise(), ), ( {"column": "gender", "stratifier": "race"}, - ["gender:strEq:female"], + ["gender:strEq:none"], {"data_package_id": "test_study"}, f"""SELECT "race", @@ -134,7 +139,7 @@ def mock_data_frame(filter_param): ( ( - "gender" LIKE 'female' + "gender" LIKE 'cumulus__none' ) ) ) @@ -144,7 +149,7 @@ def mock_data_frame(filter_param): ( ( - "gender" LIKE 'female' + "gender" LIKE 'cumulus__none' ) ) ) @@ -152,6 +157,7 @@ def mock_data_frame(filter_param): AND "race" IS NOT NULL ORDER BY "race", "gender\"""", + does_not_raise(), ), ( {"column": "gender", "stratifier": "race"}, @@ -195,6 +201,7 @@ def mock_data_frame(filter_param): AND "race" IS NOT NULL ORDER BY "race", "gender\"""", + does_not_raise(), ), ( {"column": "gender", "stratifier": "race"}, @@ -243,12 +250,25 @@ def mock_data_frame(filter_param): AND "race" IS NOT NULL ORDER BY "race", "gender\"""", + does_not_raise(), + ), + ( + {"column": "gender", "stratifier": "race"}, + [ + "gender:invalid:a", + ], + {"data_package_id": "test_study"}, + "", + # The deployed class vs testing module approach makes getting + # the actual error raised here fussy. + pytest.raises(Exception), ), ], ) -def test_build_query(query_params, filter_groups, path_params, query_str): - query, _ = get_chart_data._build_query(query_params, filter_groups, path_params) - assert query == query_str +def test_build_query(query_params, filter_groups, path_params, query_str, raises): + with raises: + query, _ = get_chart_data._build_query(query_params, filter_groups, path_params) + assert query == query_str @pytest.mark.parametrize( @@ -289,6 +309,18 @@ def test_get_data_cols(mock_bucket): assert res == list(cols) +@mock.patch("botocore.client") +def test_get_data_cols_err(mock_client): + mock_clientobj = mock_client.ClientCreator.return_value.create_client.return_value + mock_clientobj.get_object.side_effect = [ + None, + botocore.exceptions.ClientError({}, {}), + ] + with pytest.raises(Exception): + table_id = f"{EXISTING_STUDY}__{EXISTING_DATA_P}__{EXISTING_VERSION}" + get_chart_data._get_table_cols(table_id) + + @mock.patch( "src.dashboard.get_chart_data.get_chart_data._build_query", lambda query_params, filter_groups, path_params: ( @@ -320,3 +352,14 @@ def test_handler(): '"rowCount": 2, "totalCount": 20, "data": [{"rows": [["male", 10], ' '["female", 10]]}]}' ) + event = { + "queryStringParameters": {"column": "gender", "filter": "gender:strEq:female"}, + "multiValueQueryStringParameters": {}, + "pathParameters": {}, + } + res = get_chart_data.chart_data_handler(event, {}) + assert res["body"] == ( + '{"column": "gender", "filters": ["gender:strEq:female"], ' + '"rowCount": 2, "totalCount": 20, "data": [{"rows": [["male", 10], ' + '["female", 10]]}]}' + )