Skip to content

Commit

Permalink
fix: Raise error on failure for demographic_from_name (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
hdelc committed Apr 10, 2023
1 parent ce16ef5 commit 760f157
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions panel_api/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def censor_keyword_search_output(
return response_data


def demographic_from_name(name) -> Demographic | None:
def demographic_from_name(name: str) -> Demographic:
"""
Translate human-readable names for demographics to the Demographic enumeration.
"""
Expand All @@ -208,7 +208,7 @@ def demographic_from_name(name) -> Demographic | None:
return Demographic.AGE
if name in [str(Demographic.STATE), "state"]:
return Demographic.STATE
return None
raise ValueError(f"Name '{name}' is not a recognized demographic")


def fill_zeros(results):
Expand Down
19 changes: 17 additions & 2 deletions test/test_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
import pytest

from panel_api import api_utils
from panel_api.api_utils import KeywordQuery
from panel_api.api_utils import KeywordQuery, demographic_from_name
from panel_api.api_values import Demographic

from .utils import list_equals_ignore_order, period_equals


def test_demographic_from_name():
tests = {
"race": Demographic.RACE,
"voterbase_race": Demographic.RACE,
"gender": Demographic.GENDER,
"voterbase_gender": Demographic.GENDER,
"state": Demographic.STATE,
"tsmart_state": Demographic.STATE,
"age": Demographic.AGE,
"vb_age_decade": Demographic.AGE,
}
for name, dem in tests.items():
assert demographic_from_name(name) == dem


def test_parse_query_valid():
valid_inputs = [
{"keyword_query": "keyword", "aggregate_time_period": "day"},
Expand Down Expand Up @@ -76,7 +91,7 @@ def test_parse_query_invalid():
{
"keyword_query": "key word",
"time_agg": "week",
"cross_sections": ["age", "v_gender"],
"cross_sections": ["age", "gender"],
}, # Invalid demographic
{
"keyword_query": "keyword",
Expand Down

0 comments on commit 760f157

Please sign in to comment.