Skip to content

Commit

Permalink
fix: handle None type numbers (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnagro authored Oct 3, 2023
1 parent bb6d940 commit f0bb260
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
36 changes: 20 additions & 16 deletions enterprise_catalog/apps/catalog/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,26 @@ def field_comparison(query_value, content_value, comparison_kind):
compre the fields based on the comparison kind
python 3.10 has match (like switch)
"""
if comparison_kind == 'exact':
return content_value == query_value
elif comparison_kind == 'not':
return content_value != query_value
elif comparison_kind == 'exclude':
return content_value != query_value
elif comparison_kind == 'gt':
return float(content_value) > float(query_value)
elif comparison_kind == 'gte':
return float(content_value) >= float(query_value)
elif comparison_kind == 'lt':
return float(content_value) < float(query_value)
elif comparison_kind == 'lte':
return float(content_value) <= float(query_value)
else:
raise QueryFilterException(f'invalid comparison kind "{comparison_kind}"')
try:
if comparison_kind == 'exact':
return content_value == query_value
elif comparison_kind == 'not':
return content_value != query_value
elif comparison_kind == 'exclude':
return content_value != query_value
elif comparison_kind == 'gt':
return float(content_value) > float(query_value)
elif comparison_kind == 'gte':
return float(content_value) >= float(query_value)
elif comparison_kind == 'lt':
return float(content_value) < float(query_value)
elif comparison_kind == 'lte':
return float(content_value) <= float(query_value)
else:
raise QueryFilterException(f'invalid comparison kind "{comparison_kind}"')
except TypeError:
# if content_value or query_value are None float() cannot parse
return False


def does_query_match_content(query_dict, content_metadata_dict):
Expand Down
21 changes: 21 additions & 0 deletions enterprise_catalog/apps/catalog/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,27 @@ def test_non_matching_gt_key(self):
content_metadata = json.loads(content_metadata_json)
assert not filters.does_query_match_content(query_data, content_metadata)

def test_exceptional_gt_key(self):
"""
A non-matching query using an gt key.
"""
query_json = """
{
"content_type":"course",
"first_enrollable_paid_seat_price__gt":"301"
}
"""

content_metadata_json = """
{
"content_type": "course"
}
"""

query_data = json.loads(query_json)
content_metadata = json.loads(content_metadata_json)
assert not filters.does_query_match_content(query_data, content_metadata)

def test_exact_list(self):
"""
A matching query using a list exact key (aka include)
Expand Down

0 comments on commit f0bb260

Please sign in to comment.