From 4c1e5948f168f386696d46305009e0a8567520fd Mon Sep 17 00:00:00 2001 From: Emily Cheng Date: Tue, 19 Nov 2024 13:22:08 -0800 Subject: [PATCH] Add check for valid comparator before looking at None --- nacc_form_validator/utils.py | 3 +++ tests/test_utils.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/nacc_form_validator/utils.py b/nacc_form_validator/utils.py index 99d4824..bd809d0 100644 --- a/nacc_form_validator/utils.py +++ b/nacc_form_validator/utils.py @@ -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: diff --git a/tests/test_utils.py b/tests/test_utils.py index f62187a..24aed8b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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'"