Skip to content

Commit

Permalink
feat: setup new method to count number of mathing params
Browse files Browse the repository at this point in the history
  • Loading branch information
gcharest authored Sep 17, 2024
1 parent d11cfb0 commit e292fa3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions app/tests/models/test_models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down

0 comments on commit e292fa3

Please sign in to comment.