|
1 | | -from unittest.mock import Mock |
| 1 | +from unittest.mock import Mock, call |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from django.core.files.uploadedfile import SimpleUploadedFile |
|
15 | 15 | map_fields, |
16 | 16 | extract_uuid, |
17 | 17 | ) |
18 | | -from country_workspace.utils.flex_fields import Base64ImageInput, Base64ImageField, ConsentSharingChoice |
| 18 | +from country_workspace.utils.flex_fields import ( |
| 19 | + Base64ImageInput, |
| 20 | + Base64ImageField, |
| 21 | + ConsentSharingChoice, |
| 22 | + split_consent_sharing_options, |
| 23 | +) |
19 | 24 |
|
20 | 25 |
|
21 | 26 | @pytest.mark.parametrize( |
@@ -131,17 +136,53 @@ def test_extract_uuid_errors(value: str | int, prefix: str | int | None, exc_typ |
131 | 136 |
|
132 | 137 |
|
133 | 138 | @pytest.mark.parametrize( |
134 | | - ("raw", "expected_py", "expected_prep"), |
| 139 | + ("value", "expected"), |
| 140 | + [ |
| 141 | + pytest.param("a,b,c", abc := ["a", "b", "c"], id="comma"), |
| 142 | + pytest.param("a b c", abc, id="space"), |
| 143 | + pytest.param("a, b,c ", abc, id="comma-strip"), |
| 144 | + pytest.param("a b c ", abc, id="space-strip"), |
| 145 | + pytest.param("", [], id="empty"), |
| 146 | + pytest.param("a", ["a"], id="single"), |
| 147 | + ], |
| 148 | +) |
| 149 | +def test_split_consent_sharing_options(value: str, expected: list[str]) -> None: |
| 150 | + assert split_consent_sharing_options(value) == expected |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize( |
| 154 | + "value", |
135 | 155 | [ |
136 | | - ("a,b,c", ["a", "b", "c"], ["a", "b", "c"]), |
137 | | - (" a , b ,c ", ["a", "b", "c"], [" a ", " b ", "c "]), |
138 | | - ("", [], []), |
139 | | - (None, [], None), |
140 | | - (["x", "y"], ["x", "y"], ["x", "y"]), |
| 156 | + pytest.param(None, id="none"), |
| 157 | + pytest.param(["a", "b"], id="list"), |
141 | 158 | ], |
142 | | - ids=["comma", "spaces", "empty", "none", "list"], |
143 | 159 | ) |
144 | | -def test_consent_sharing_choice_to_python_and_prepare_value(raw, expected_py, expected_prep): |
145 | | - field = ConsentSharingChoice(choices=[]) |
146 | | - assert field.to_python(raw) == expected_py |
147 | | - assert field.prepare_value(raw) == expected_prep |
| 160 | +def test_consent_sharing_choice_to_python_and_prepare_value_call_super_method( |
| 161 | + mocker: MockerFixture, value: list[str] | None |
| 162 | +) -> None: |
| 163 | + super_to_python_mock = mocker.patch("country_workspace.utils.flex_fields.forms.MultipleChoiceField.to_python") |
| 164 | + super_prepare_value_mock = mocker.patch( |
| 165 | + "country_workspace.utils.flex_fields.forms.MultipleChoiceField.prepare_value" |
| 166 | + ) |
| 167 | + instance = Mock(spec=ConsentSharingChoice) |
| 168 | + |
| 169 | + ConsentSharingChoice.prepare_value(instance, value) |
| 170 | + ConsentSharingChoice.to_python(instance, value) |
| 171 | + |
| 172 | + super_to_python_mock.assert_called_once_with(value) |
| 173 | + super_prepare_value_mock.assert_called_once_with(value) |
| 174 | + |
| 175 | + |
| 176 | +def test_consent_sharing_choice_to_python_and_prepare_value_call_split_consent_sharing_options( |
| 177 | + mocker: MockerFixture, |
| 178 | +) -> None: |
| 179 | + value = "test" |
| 180 | + split_consent_sharing_options_mock = mocker.patch( |
| 181 | + "country_workspace.utils.flex_fields.split_consent_sharing_options" |
| 182 | + ) |
| 183 | + instance = Mock(spec=ConsentSharingChoice) |
| 184 | + |
| 185 | + ConsentSharingChoice.to_python(instance, value) |
| 186 | + ConsentSharingChoice.prepare_value(instance, value) |
| 187 | + |
| 188 | + split_consent_sharing_options_mock.assert_has_calls([c := call(value), c]) |
0 commit comments