|
1 | | -from unittest.mock import patch |
| 1 | +from unittest.mock import patch, MagicMock |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from hope_flex_fields.models import DataChecker |
|
13 | 13 | from country_workspace.contrib.hope.validators import FullHouseholdValidator |
14 | 14 | from country_workspace.signals import ( |
15 | 15 | _process_datachecker_change, |
| 16 | + invalidate_fieldset_fields_admin_cache, |
16 | 17 | ) |
17 | 18 | from country_workspace.validators.registry import NoopValidator |
18 | 19 | from tests.extras.testutils.factories import ( |
|
23 | 24 | FieldsetFactory, |
24 | 25 | ) |
25 | 26 | from tests.extras.testutils.factories.program import BeneficiaryGroupFactory |
26 | | -from tests.extras.testutils.factories.smart_fields import DataCheckerFactory |
| 27 | +from tests.extras.testutils.factories.smart_fields import DataCheckerFactory, FlexFieldFactory |
27 | 28 |
|
28 | 29 |
|
29 | 30 | @pytest.fixture |
@@ -207,3 +208,30 @@ def test_no_invalidation_on_other_field_change(program): |
207 | 208 | for hh in HouseholdFactory._meta.model.objects.all(): |
208 | 209 | hh.refresh_from_db() |
209 | 210 | assert hh.errors == {"x": "1"} |
| 211 | + |
| 212 | + |
| 213 | +def test_invalidate_fieldset_fields_admin_cache_on_flexfield_save(): |
| 214 | + fieldset = FieldsetFactory.create() |
| 215 | + |
| 216 | + with patch("country_workspace.signals.cache_manager.invalidate_containing") as mocked: |
| 217 | + FlexFieldFactory.create(fieldset=fieldset, name="test_field") |
| 218 | + mocked.assert_called_once_with(f"adminhope_flex_fieldsfieldset{fieldset.pk}") |
| 219 | + |
| 220 | + |
| 221 | +def test_invalidate_fieldset_fields_admin_cache_on_flexfield_update(): |
| 222 | + fieldset = FieldsetFactory.create() |
| 223 | + flex_field = FlexFieldFactory.create(fieldset=fieldset, name="test_field") |
| 224 | + |
| 225 | + with patch("country_workspace.signals.cache_manager.invalidate_containing") as mocked: |
| 226 | + flex_field.name = "updated_name" |
| 227 | + flex_field.save() |
| 228 | + mocked.assert_called_once_with(f"adminhope_flex_fieldsfieldset{fieldset.pk}") |
| 229 | + |
| 230 | + |
| 231 | +def test_invalidate_fieldset_fields_admin_cache_no_fieldset(): |
| 232 | + instance = MagicMock() |
| 233 | + instance.fieldset = None |
| 234 | + |
| 235 | + with patch("country_workspace.signals.cache_manager.invalidate_containing") as mocked: |
| 236 | + invalidate_fieldset_fields_admin_cache(sender=MagicMock, instance=instance) |
| 237 | + mocked.assert_not_called() |
0 commit comments