-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_signals.py
More file actions
460 lines (336 loc) · 17.1 KB
/
Copy pathtest_signals.py
File metadata and controls
460 lines (336 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
from unittest.mock import patch
import pytest
from django.utils import timezone
from hope_flex_fields.models import DataChecker
from hope_flex_fields.models import DataCheckerFieldset
from hope_flex_fields.models import Fieldset
from strategy_field.utils import fqn
from django.contrib.admin.sites import AdminSite
from country_workspace.admin.flex_fields import CWFieldsetAdmin
from country_workspace.contrib.hope.constants import (
HOUSEHOLD_CHECKER_NAME,
INDIVIDUAL_CHECKER_NAME,
PEOPLE_CHECKER_NAME,
)
from country_workspace.contrib.hope.validators import FullHouseholdValidator
from country_workspace.signals import (
_process_datachecker_change,
collect_invalidations,
invalidate_entities_on_datachecker_change,
)
from country_workspace.validators.registry import NoopValidator
from tests.extras.testutils.factories import (
ProgramFactory,
BatchFactory,
HouseholdFactory,
IndividualFactory,
FieldDefinitionFactory,
FieldsetFactory,
FlexFieldFactory,
)
from tests.extras.testutils.factories.program import BeneficiaryGroupFactory
from tests.extras.testutils.factories.smart_fields import DataCheckerFactory
CHECKED = timezone.now()
@pytest.fixture
def hh_datachecker():
return DataChecker.objects.create(name=HOUSEHOLD_CHECKER_NAME)
@pytest.fixture
def ind_datachecker():
return DataChecker.objects.create(name=INDIVIDUAL_CHECKER_NAME)
@pytest.fixture
def people_datachecker():
return DataChecker.objects.create(name=PEOPLE_CHECKER_NAME)
@pytest.fixture
def program(hh_datachecker, ind_datachecker):
return ProgramFactory.create(household_checker=hh_datachecker, individual_checker=ind_datachecker)
@pytest.fixture
def batch(program):
return BatchFactory.create(program=program)
@pytest.fixture
def households(batch):
HouseholdFactory.create_batch(10, batch=batch, errors={}, removed=False, last_checked=CHECKED)
HouseholdFactory.create_batch(10, batch=batch, removed=True, last_checked=CHECKED)
HouseholdFactory.create_batch(10, batch=batch, errors={"some_error": "details"}, last_checked=CHECKED)
@pytest.fixture
def individuals(batch):
hh = HouseholdFactory.create(batch=batch, individuals=[])
IndividualFactory.create_batch(10, household=hh, errors={}, removed=False, last_checked=CHECKED)
IndividualFactory.create_batch(10, household=hh, removed=True, last_checked=CHECKED)
IndividualFactory.create_batch(10, household=hh, errors={"some_error": "details"}, last_checked=CHECKED)
@pytest.fixture
def unchecked_households(batch):
return HouseholdFactory.create_batch(3, batch=batch, errors={}, removed=False, individuals=[], last_checked=None)
@pytest.fixture
def unchecked_individuals(batch, unchecked_households):
return IndividualFactory.create_batch(
3, batch=batch, household=unchecked_households[0], errors={}, removed=False, last_checked=None
)
@pytest.fixture
def checked_household(batch):
return HouseholdFactory.create(batch=batch, errors={}, removed=False, individuals=[], last_checked=CHECKED)
@pytest.fixture
def checked_individual(batch, checked_household):
return IndividualFactory.create(
batch=batch, household=checked_household, errors={}, removed=False, last_checked=CHECKED
)
@pytest.fixture
def hh_validated_households(hh_datachecker):
program = ProgramFactory.create(household_checker=hh_datachecker)
batch = BatchFactory.create(program=program)
validated = HouseholdFactory.create_batch(
3, batch=batch, errors={}, removed=False, individuals=[], last_checked=CHECKED
)
HouseholdFactory.create(batch=batch, errors={"x": 1}, removed=False, individuals=[], last_checked=CHECKED)
HouseholdFactory.create(batch=batch, errors={}, removed=True, individuals=[], last_checked=CHECKED)
return validated
def test_process_datachecker_change_updates_households(hh_datachecker, hh_validated_households):
_process_datachecker_change(hh_datachecker)
for hh in hh_validated_households:
hh.refresh_from_db()
assert hh.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert hh.last_checked is None
@pytest.fixture
def ind_validated_individuals(ind_datachecker):
program = ProgramFactory.create(individual_checker=ind_datachecker)
batch = BatchFactory.create(program=program)
hh = HouseholdFactory.create(batch=batch, individuals=[])
validated = IndividualFactory.create_batch(3, household=hh, errors={}, removed=False, last_checked=CHECKED)
ind_1 = IndividualFactory.create(household=hh, errors={"x": 1}, removed=False, last_checked=CHECKED)
IndividualFactory.create(household=hh, errors={}, removed=True, last_checked=CHECKED)
return [*validated, ind_1]
def test_process_datachecker_change_updates_individuals(ind_datachecker, ind_validated_individuals):
_process_datachecker_change(ind_datachecker)
for ind in ind_validated_individuals:
ind.refresh_from_db()
assert ind.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert ind.last_checked is None
@pytest.fixture
def people_validated_individuals(people_datachecker):
bg = BeneficiaryGroupFactory(master_detail=False)
program = ProgramFactory.create(individual_checker=people_datachecker, beneficiary_group=bg)
batch = BatchFactory.create(program=program)
validated = IndividualFactory.create_batch(
3, batch=batch, household=None, errors={}, removed=False, last_checked=CHECKED
)
ind_1 = IndividualFactory.create(batch=batch, household=None, errors={"x": 1}, removed=False, last_checked=CHECKED)
IndividualFactory.create(batch=batch, household=None, errors={}, removed=True, last_checked=CHECKED)
return [*validated, ind_1]
def test_process_datachecker_change_updates_people(people_datachecker, people_validated_individuals):
_process_datachecker_change(people_datachecker)
for ind in people_validated_individuals:
ind.refresh_from_db()
assert ind.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert ind.last_checked is None
def test_non_validated_entities_are_not_invalidated(
hh_datachecker, ind_datachecker, unchecked_households, unchecked_individuals, checked_household, checked_individual
):
_process_datachecker_change(hh_datachecker)
_process_datachecker_change(ind_datachecker)
for hh in unchecked_households:
hh.refresh_from_db()
assert hh.errors == {}
assert hh.last_checked is None
for ind in unchecked_individuals:
ind.refresh_from_db()
assert ind.errors == {}
assert ind.last_checked is None
checked_household.refresh_from_db()
assert checked_household.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert checked_household.last_checked is None
checked_individual.refresh_from_db()
assert checked_individual.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert checked_individual.last_checked is None
def test_process_datachecker_change_ignores_unknown_checker_name():
dc = DataChecker.objects.create(name="SOME_UNKNOWN")
_process_datachecker_change(dc)
@pytest.fixture
def hh_fieldsets(hh_datachecker):
fs = FieldsetFactory.create()
fs_2 = FieldsetFactory.create()
hh_datachecker.fieldsets.add(*[fs, fs_2])
return fs, fs_2
@pytest.fixture
def ind_dcfieldset(ind_datachecker):
fs = FieldsetFactory.create()
return DataCheckerFieldset.objects.create(checker=ind_datachecker, fieldset=fs)
@pytest.fixture
def hh_flexfield(hh_datachecker):
fs = FieldsetFactory.create()
hh_datachecker.fieldsets.add(fs)
return FlexFieldFactory.create(fieldset=fs)
def test_fieldset_update_triggers_processing(hh_datachecker, hh_fieldsets):
fs, _ = hh_fieldsets
with patch("country_workspace.signals._process_datachecker_change") as mocked:
fs.description = "Updated"
fs.save(update_fields=["description"])
mocked.assert_called_once_with(dc=hh_datachecker)
def test_dcfieldset_update_triggers_processing(ind_datachecker, ind_dcfieldset):
with patch("country_workspace.signals._process_datachecker_change") as mocked:
ind_dcfieldset.prefix = "p_"
ind_dcfieldset.save(update_fields=["prefix"])
mocked.assert_called_once_with(dc=ind_datachecker)
def test_datachecker_update_does_not_trigger_processing(hh_datachecker):
with patch("country_workspace.signals._process_datachecker_change") as mocked:
hh_datachecker.description = "Updated"
hh_datachecker.save(update_fields=["description"])
mocked.assert_not_called()
def test_flexfield_update_triggers_processing(hh_datachecker, hh_flexfield):
with patch("country_workspace.signals._process_datachecker_change") as mocked:
hh_flexfield.attrs = {"label": "Updated"}
hh_flexfield.save(update_fields=["attrs"])
mocked.assert_called_once_with(dc=hh_datachecker)
def _test_invalidation_on_checker_change(program, factory, checker_field):
new_checker = DataCheckerFactory()
setattr(program, checker_field, new_checker)
program.save()
entities_invalidated = 0
for entity in factory._meta.model.objects.all():
entity.refresh_from_db()
if entity.errors:
assert entity.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert entity.last_checked is None
entities_invalidated += 1
assert entities_invalidated == 20
def test_invalidation_on_hh_checker_change(program, households):
_test_invalidation_on_checker_change(program, HouseholdFactory, checker_field="household_checker")
def test_invalidation_on_individual_checker_change(program, individuals):
_test_invalidation_on_checker_change(program, IndividualFactory, checker_field="individual_checker")
@pytest.fixture
def validator_change_program(hh_datachecker, ind_datachecker):
program = ProgramFactory.create(
household_checker=hh_datachecker, individual_checker=ind_datachecker, beneficiary_validator=fqn(NoopValidator)
)
batch = BatchFactory.create(program=program)
valid_hhs = HouseholdFactory.create_batch(3, batch=batch, individuals=[], last_checked=CHECKED)
HouseholdFactory.create_batch(4, batch=batch, individuals=[], errors={"x": 1}, removed=False, last_checked=CHECKED)
HouseholdFactory.create_batch(5, batch=batch, individuals=[], removed=True, last_checked=CHECKED)
for hh in valid_hhs:
IndividualFactory.create_batch(3, batch=batch, household=hh, errors={}, removed=False, last_checked=CHECKED)
IndividualFactory.create_batch(
4, batch=batch, household=hh, errors={"x": 1}, removed=False, last_checked=CHECKED
)
IndividualFactory.create_batch(5, batch=batch, household=hh, errors={}, removed=True, last_checked=CHECKED)
return program
def test_invalidation_on_beneficiary_validator_change(validator_change_program):
validator_change_program.beneficiary_validator = fqn(FullHouseholdValidator)
validator_change_program.save()
hh_invalidated_count = 0
ind_invalidated_count = 0
for hh in HouseholdFactory._meta.model.objects.all():
hh.refresh_from_db()
if hh.errors:
assert hh.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert hh.last_checked is None
hh_invalidated_count += 1
for ind in IndividualFactory._meta.model.objects.all():
ind.refresh_from_db()
if ind.errors:
assert ind.errors == {"data_checker": "Invalidated due to DataChecker change."}
assert ind.last_checked is None
ind_invalidated_count += 1
assert hh_invalidated_count == 7
assert ind_invalidated_count == 7 * 3
@pytest.fixture
def households_with_errors(batch):
return HouseholdFactory.create_batch(
10, batch=batch, individuals=[], errors={"x": "1"}, removed=False, last_checked=CHECKED
)
def test_no_invalidation_on_other_field_change(program, households_with_errors):
program.name = "New Program Name"
program.save()
for hh in households_with_errors:
hh.refresh_from_db()
assert hh.errors == {"x": "1"}
@pytest.fixture
def synclog_for_refresh():
from country_workspace.models import SyncLog
from hope_flex_fields.models import FieldDefinition
from django.contrib.contenttypes.models import ContentType
fd = FieldDefinitionFactory.create(name="test-fd", attrs={"choices": [["x", "X"]]})
fd.refresh_from_db()
fs = FieldsetFactory.create()
ff = FlexFieldFactory.create(fieldset=fs, definition=fd, attrs={})
ff.refresh_from_db()
ct = ContentType.objects.get_for_model(FieldDefinition)
sync = SyncLog.objects.create(content_type=ct, object_id=fd.pk, data={"remote_url": "lookups/test"})
return sync, fd, ff
def test_collect_invalidations_deduplicates_and_flushes(hh_datachecker):
program = ProgramFactory.create(household_checker=hh_datachecker)
batch = BatchFactory.create(program=program)
hh = HouseholdFactory.create(batch=batch, errors={}, removed=False, individuals=[], last_checked=CHECKED)
with collect_invalidations():
_process_datachecker_change(hh_datachecker)
_process_datachecker_change(hh_datachecker)
hh.refresh_from_db()
assert hh.last_checked == CHECKED
hh.refresh_from_db()
assert hh.last_checked is None
assert hh.errors == {"data_checker": "Invalidated due to DataChecker change."}
def test_collect_invalidations_empty():
with patch("country_workspace.signals._process_program") as mock_process:
with collect_invalidations():
pass
mock_process.assert_not_called()
def test_synclog_refresh_skips_save_when_attrs_unchanged(synclog_for_refresh):
sync, fd, ff = synclog_for_refresh
existing_choices = dict(fd.attrs.get("choices", []))
with patch("country_workspace.contrib.hope.client.HopeClient.get_lookup", return_value=existing_choices):
with patch("country_workspace.signals._process_datachecker_change"):
sync.refresh()
fd.refresh_from_db()
ff.refresh_from_db()
assert fd.attrs["choices"] == [list(pair) for pair in existing_choices.items()]
def test_synclog_refresh_saves_when_attrs_changed(synclog_for_refresh):
sync, fd, _ff = synclog_for_refresh
with patch("country_workspace.contrib.hope.client.HopeClient.get_lookup", return_value={"new": "New"}):
with patch("country_workspace.signals._process_datachecker_change"):
sync.refresh()
fd.refresh_from_db()
assert fd.attrs["choices"] == [["new", "New"]]
@pytest.fixture
def synclog_for_manager_refresh():
from country_workspace.models import SyncLog
from hope_flex_fields.models import FieldDefinition
from django.contrib.contenttypes.models import ContentType
fd = FieldDefinitionFactory.create(name="test-mgr-fd", attrs={"choices": [["a", "A"]]})
fd.refresh_from_db()
ct = ContentType.objects.get_for_model(FieldDefinition)
return SyncLog.objects.create(content_type=ct, object_id=fd.pk, data={"remote_url": "lookups/test"})
def test_synclog_manager_refresh_iterates_all_records(synclog_for_manager_refresh):
from country_workspace.models import SyncLog
with patch("country_workspace.contrib.hope.client.HopeClient.get_lookup", return_value={"b": "B"}):
SyncLog.objects.refresh()
fd = synclog_for_manager_refresh.content_object
fd.refresh_from_db()
assert fd.attrs["choices"] == [["b", "B"]]
def test_signal_handler_ignores_unrecognised_instance_type():
class _Unrelated:
pass
with patch("country_workspace.signals._process_program") as mock_process:
invalidate_entities_on_datachecker_change(sender=_Unrelated, instance=_Unrelated())
mock_process.assert_not_called()
def test_datachecker_save_does_not_invalidate_entities(hh_datachecker, checked_household):
hh_datachecker.description = "Changed description"
hh_datachecker.save()
checked_household.refresh_from_db()
assert checked_household.last_checked == CHECKED
assert checked_household.errors == {}
def test_collect_invalidations_deduplicates_multiple_signal_sources(program, hh_flexfield, checked_household):
fs = hh_flexfield.fieldset
with patch("country_workspace.signals._process_program") as mock_process:
with collect_invalidations():
hh_flexfield.attrs = {"label": "New"}
hh_flexfield.save(update_fields=["attrs"])
fs.description = "Updated"
fs.save(update_fields=["description"])
mock_process.assert_called_once_with(program=program)
def test_admin_fieldset_all_fields_post_uses_collect_invalidations(rf, hh_datachecker, hh_flexfield, program):
fs = hh_flexfield.fieldset
admin_instance = CWFieldsetAdmin(Fieldset, AdminSite())
request = rf.post(f"/admin/hope_flex_fields/fieldset/{fs.pk}/all_fields/")
request.user = type("U", (), {"is_authenticated": True, "has_perm": lambda *a: True})()
with patch("country_workspace.admin.flex_fields.FieldsetAdmin.all_fields") as mock_parent:
mock_parent.func = lambda self, request, pk: None
with patch("country_workspace.admin.flex_fields.collect_invalidations") as mock_ctx:
admin_instance.all_fields.func(admin_instance, request, str(fs.pk))
mock_ctx.assert_called_once()