diff --git a/app/models/utils.py b/app/models/utils.py index 35bce404..284a9541 100644 --- a/app/models/utils.py +++ b/app/models/utils.py @@ -47,6 +47,11 @@ def is_parameter_in_model(model_params: List[str], payload: Dict[str, Any]) -> b return any(param in model_params for param in payload.keys()) +def has_parameters_in_model(model_params: List[str], payload: Dict[str, Any]) -> int: + """Returns the number of parameters in the payload that are in the model.""" + return sum(1 for param in model_params if param in payload.keys()) + + def are_all_parameters_in_model( model_params: List[str], payload: Dict[str, Any] ) -> bool: diff --git a/app/tests/models/test_models_utils.py b/app/tests/models/test_models_utils.py index 7370108a..d8aeff4d 100644 --- a/app/tests/models/test_models_utils.py +++ b/app/tests/models/test_models_utils.py @@ -47,6 +47,27 @@ def test_is_parameter_in_model(): assert not model_utils.is_parameter_in_model(model_params, non_string_keys_payload) +def test_has_parameters_in_model(): + model_params = ["field1", "field2", "field3"] + + payload = {"field1": "value", "field2": "value"} + assert model_utils.has_parameters_in_model(model_params, payload) == 2 + + payload = {"field1": "value", "non_field": "value"} + assert model_utils.has_parameters_in_model(model_params, payload) == 1 + + empty_payload = {} + assert model_utils.has_parameters_in_model(model_params, empty_payload) == 0 + + partial_payload = {"field1": "value"} + assert model_utils.has_parameters_in_model(model_params, partial_payload) == 1 + + non_string_keys_payload = {1: "value", 2: "value"} + assert ( + model_utils.has_parameters_in_model(model_params, non_string_keys_payload) == 0 + ) + + def test_are_all_parameters_in_model(): model_params = ["field1", "field2", "field3"]