Skip to content

Commit

Permalink
Refactor test_create_activity_item_conditional_logic and updated Sing…
Browse files Browse the repository at this point in the history
…lePayload to consider min and max socores with numeric values, 0 being a feasible option
  • Loading branch information
AlejandroCoronadoN committed Oct 15, 2024
1 parent 3dbf416 commit 47c1e17
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 59 deletions.
20 changes: 10 additions & 10 deletions src/apps/activities/domain/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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]:
Expand Down
174 changes: 129 additions & 45 deletions src/apps/activities/tests/unit/domain/test_activity_item_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 47c1e17

Please sign in to comment.