Skip to content

Commit

Permalink
light coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dogversioning committed Nov 7, 2024
1 parent 23cd8a5 commit af76971
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/dashboard/get_chart_data/get_chart_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
57 changes: 50 additions & 7 deletions tests/dashboard/test_get_chart_data.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"},
Expand All @@ -54,6 +56,7 @@ def mock_data_frame(filter_param):
)
ORDER BY
"gender\"""",
does_not_raise(),
),
(
{"column": "gender", "stratifier": "race"},
Expand All @@ -77,6 +80,7 @@ def mock_data_frame(filter_param):
AND "race" IS NOT NULL
ORDER BY
"race", "gender\"""",
does_not_raise(),
),
(
{"column": "gender"},
Expand Down Expand Up @@ -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",
Expand All @@ -134,7 +139,7 @@ def mock_data_frame(filter_param):
(
(
"gender" LIKE 'female'
"gender" LIKE 'cumulus__none'
)
)
)
Expand All @@ -144,14 +149,15 @@ def mock_data_frame(filter_param):
(
(
"gender" LIKE 'female'
"gender" LIKE 'cumulus__none'
)
)
)
)
AND "race" IS NOT NULL
ORDER BY
"race", "gender\"""",
does_not_raise(),
),
(
{"column": "gender", "stratifier": "race"},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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: (
Expand Down Expand Up @@ -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]]}]}'
)

0 comments on commit af76971

Please sign in to comment.