From 47c1e17acac5339f1f5ad5e536e2bd7e7bb86443 Mon Sep 17 00:00:00 2001 From: Coro Date: Tue, 15 Oct 2024 14:37:37 -0600 Subject: [PATCH] Refactor test_create_activity_item_conditional_logic and updated SinglePayload to consider min and max socores with numeric values, 0 being a feasible option --- src/apps/activities/domain/conditions.py | 20 +- .../unit/domain/test_activity_item_create.py | 174 +++++++++++++----- .../unit/domain/test_custom_validation.py | 4 - 3 files changed, 139 insertions(+), 59 deletions(-) diff --git a/src/apps/activities/domain/conditions.py b/src/apps/activities/domain/conditions.py index a092f7ff050..bc3b734af11 100644 --- a/src/apps/activities/domain/conditions.py +++ b/src/apps/activities/domain/conditions.py @@ -166,13 +166,13 @@ class SingleTimePayload(PublicModel): time: Optional[datetime.time] = None max_value: Optional[datetime.time] = None min_value: Optional[datetime.time] = None - + @root_validator(pre=True) def validate_time(cls, values: Dict[str, Any]) -> Dict[str, Any]: time_value = values.get("time") max_time_value = values.get("max_value") min_time_value = values.get("min_value") - + if isinstance(time_value, dict): values["time"] = cls._dict_to_time(time_value) elif isinstance(time_value, str): @@ -182,22 +182,22 @@ def validate_time(cls, values: Dict[str, Any]) -> Dict[str, Any]: max_time_value = cls._dict_to_time(max_time_value) elif isinstance(max_time_value, str): max_time_value = cls._string_to_time(max_time_value) - + if isinstance(min_time_value, dict): min_time_value = cls._dict_to_time(min_time_value) elif isinstance(min_time_value, str): min_time_value = cls._string_to_time(min_time_value) - + if max_time_value < min_time_value: raise IncorrectTimeRange() - - if min_time_value: - if not max_time_value: + + if min_time_value is not None: + if max_time_value is None: raise IncorrectMaxTimeRange() - if max_time_value: - if not min_time_value: + if max_time_value is not None: + if min_time_value is None: raise IncorrectMinTimeRange() - + return values def dict(self, *args, **kwargs) -> Dict[str, Any]: diff --git a/src/apps/activities/tests/unit/domain/test_activity_item_create.py b/src/apps/activities/tests/unit/domain/test_activity_item_create.py index eaa4a4a8782..7aab778d997 100644 --- a/src/apps/activities/tests/unit/domain/test_activity_item_create.py +++ b/src/apps/activities/tests/unit/domain/test_activity_item_create.py @@ -599,54 +599,138 @@ def test_create_message_item__sanitize_question(message_item_create): assert item.question["en"] == "One Two" -@pytest.mark.parametrize( - "response_type, config_fixture, cnd_logic_fixture, response_values_fixture", - ( - (ResponseType.SINGLESELECT, "single_select_config", "conditional_logic_equal", "single_select_response_values"), - (ResponseType.MULTISELECT, "multi_select_config", "conditional_logic_equal", "multi_select_response_values"), - (ResponseType.SLIDER, "slider_config", "conditional_logic_between", "slider_response_values"), - (ResponseType.TIME, "time_config", "conditional_logic_between", None), - (ResponseType.TIMERANGE, "time_range_config", "conditional_logic_between", None), - ( - ResponseType.NUMBERSELECT, - "number_selection_config", - "conditional_logic_between", - "number_selection_response_values", - ), - (ResponseType.DATE, "date_config", "conditional_logic_equal", None), - ( - ResponseType.SINGLESELECTROWS, - "single_select_row_config", - "conditional_logic_equal", - "single_select_row_response_values", - ), - ( - ResponseType.MULTISELECTROWS, - "multi_select_row_config", - "conditional_logic_equal", - "multi_select_row_response_values", - ), - ( - ResponseType.SLIDERROWS, - "slider_rows_config", - "conditional_logic_rows_outside_of", - "slider_rows_response_values", - ), - ), -) -def test_create_activity_item_conditional_logic( - base_item_data, request, response_type, config_fixture, cnd_logic_fixture, response_values_fixture -) -> None: - config = request.getfixturevalue(config_fixture) - cnd_logic = request.getfixturevalue(cnd_logic_fixture) - if response_values_fixture: - response_values = request.getfixturevalue(response_values_fixture) - else: - response_values = None +def test_single_select_logic(base_item_data, request) -> None: + config = request.getfixturevalue("single_select_config") + cnd_logic = request.getfixturevalue("conditional_logic_equal") + response_values = request.getfixturevalue("single_select_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.SINGLESELECT, + conditional_logic=cnd_logic, + response_values=response_values, + ) + + +def test_multi_select_logic(base_item_data, request) -> None: + config = request.getfixturevalue("multi_select_config") + cnd_logic = request.getfixturevalue("conditional_logic_equal") + response_values = request.getfixturevalue("multi_select_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.MULTISELECT, + conditional_logic=cnd_logic, + response_values=response_values, + ) + + +def test_slider_logic(base_item_data, request) -> None: + config = request.getfixturevalue("slider_config") + cnd_logic = request.getfixturevalue("conditional_logic_between") + response_values = request.getfixturevalue("slider_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.SLIDER, + conditional_logic=cnd_logic, + response_values=response_values, + ) + + +def test_time_logic(base_item_data, request) -> None: + config = request.getfixturevalue("time_config") + cnd_logic = request.getfixturevalue("conditional_logic_between") + ActivityItemCreate( **base_item_data.dict(), config=config, - response_type=response_type, + response_type=ResponseType.TIME, + conditional_logic=cnd_logic, + response_values=None, + ) + + +def test_time_range_logic(base_item_data, request) -> None: + config = request.getfixturevalue("time_range_config") + cnd_logic = request.getfixturevalue("conditional_logic_between") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.TIMERANGE, + conditional_logic=cnd_logic, + response_values=None, + ) + + +def test_number_select_logic(base_item_data, request) -> None: + config = request.getfixturevalue("number_selection_config") + cnd_logic = request.getfixturevalue("conditional_logic_between") + response_values = request.getfixturevalue("number_selection_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.NUMBERSELECT, + conditional_logic=cnd_logic, + response_values=response_values, + ) + + +def test_date_logic(base_item_data, request) -> None: + config = request.getfixturevalue("date_config") + cnd_logic = request.getfixturevalue("conditional_logic_equal") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.DATE, + conditional_logic=cnd_logic, + response_values=None, + ) + + +def test_single_select_row_logic(base_item_data, request) -> None: + config = request.getfixturevalue("single_select_row_config") + cnd_logic = request.getfixturevalue("conditional_logic_equal") + response_values = request.getfixturevalue("single_select_row_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.SINGLESELECTROWS, + conditional_logic=cnd_logic, + response_values=response_values, + ) + + +def test_multi_select_row_logic(base_item_data, request) -> None: + config = request.getfixturevalue("multi_select_row_config") + cnd_logic = request.getfixturevalue("conditional_logic_equal") + response_values = request.getfixturevalue("multi_select_row_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.MULTISELECTROWS, + conditional_logic=cnd_logic, + response_values=response_values, + ) + + +def test_slider_rows_logic(base_item_data, request) -> None: + config = request.getfixturevalue("slider_rows_config") + cnd_logic = request.getfixturevalue("conditional_logic_rows_outside_of") + response_values = request.getfixturevalue("slider_rows_response_values") + + ActivityItemCreate( + **base_item_data.dict(), + config=config, + response_type=ResponseType.SLIDERROWS, conditional_logic=cnd_logic, response_values=response_values, ) diff --git a/src/apps/activities/tests/unit/domain/test_custom_validation.py b/src/apps/activities/tests/unit/domain/test_custom_validation.py index 1710f15b9ff..331761bf84a 100644 --- a/src/apps/activities/tests/unit/domain/test_custom_validation.py +++ b/src/apps/activities/tests/unit/domain/test_custom_validation.py @@ -232,10 +232,6 @@ def test_single_time_payload_incorrect_time_range(self): with pytest.raises(IncorrectTimeRange): SingleTimePayload(time={"hours": 3, "minutes": 0}, min_value="03:00", max_value="02:00") - # def test_single_time_payload_valid_but_fails_validation(self): - # with pytest.raises(ValidationError): #Not sure here what you want to test. I'm assuming that it is that if min_value and max_value are set, the - # SingleTimePayload(time={"hours": 3, "minutes": 0}) - def test_single_time_payload_unknown_item_type(self): with pytest.raises(ValidationError): SingleTimePayload(time="unknown_item_type", min_value="01:00", max_value="02:00")