Skip to content

Commit

Permalink
Add check for valid comparator before looking at None
Browse files Browse the repository at this point in the history
  • Loading branch information
echeng06 committed Nov 19, 2024
1 parent 8d23e9e commit 4c1e594
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nacc_form_validator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def compare_values(comparator: str, value: object, base_value: object) -> bool:
if comparator == "!=":
return value != base_value

if comparator not in ["<=", ">=", "<", ">"]:
raise TypeError(f"Unrecognized comparator: {comparator}")

# for < and >, follow same convention as jsonlogic for null values
# for >= and <=, allow equality case (both None)
if value is None and base_value is None:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ def test_compare_values_type_error():
compare_values("*", 5, 10)
assert str(e.value) == "Unrecognized comparator: *"

with pytest.raises(TypeError) as e:
compare_values("+", None, None)
assert str(e.value) == "Unrecognized comparator: +"

with pytest.raises(TypeError) as e:
compare_values("<", 5, parser.parse("01/01/2000"))
assert str(e.value) == "'<' not supported between instances of 'int' and 'datetime.datetime'"
Expand Down

0 comments on commit 4c1e594

Please sign in to comment.