|
| 1 | +""" |
| 2 | +Tests for model_mixins.py. |
| 3 | +""" |
| 4 | + |
| 5 | +from unittest import TestCase, mock |
| 6 | + |
| 7 | +import ddt |
| 8 | + |
| 9 | +from openedx.core.djangolib.model_mixins import DeletableByUserValue |
| 10 | + |
| 11 | + |
| 12 | +@ddt.ddt |
| 13 | +class TestDeletableByUserValue(TestCase): |
| 14 | + """ |
| 15 | + Unit tests for DeletableByUserValue. |
| 16 | + """ |
| 17 | + |
| 18 | + class NonRedactingModel(DeletableByUserValue): |
| 19 | + """ |
| 20 | + Dummy model that uses default redaction behavior. |
| 21 | + """ |
| 22 | + |
| 23 | + class RedactingModel(DeletableByUserValue): |
| 24 | + """ |
| 25 | + Dummy model that overrides redaction fields. |
| 26 | + """ |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def redact_before_delete_fields(cls): |
| 30 | + return {'email': 'redacted@retired.invalid'} |
| 31 | + |
| 32 | + def _make_queryset(self, exists): |
| 33 | + """ |
| 34 | + Return a mock queryset with ``exists`` and ``values_list`` pre-configured. |
| 35 | + """ |
| 36 | + queryset = mock.Mock() |
| 37 | + queryset.exists.return_value = exists |
| 38 | + queryset.values_list.return_value = [11, 12] if exists else [] |
| 39 | + return queryset |
| 40 | + |
| 41 | + def test_redact_before_delete_fields_defaults_to_empty_dict(self): |
| 42 | + """ |
| 43 | + Verify the default redaction hook does not request any field updates. |
| 44 | + """ |
| 45 | + assert not self.NonRedactingModel.redact_before_delete_fields() |
| 46 | + |
| 47 | + def test_delete_by_user_value_returns_false_when_no_matches(self): |
| 48 | + """ |
| 49 | + Verify no updates or deletes occur when no rows match the filter. |
| 50 | + """ |
| 51 | + queryset = self._make_queryset(exists=False) |
| 52 | + with mock.patch.object(self.NonRedactingModel, 'objects', create=True) as mock_objects: |
| 53 | + mock_objects.filter.return_value = queryset |
| 54 | + |
| 55 | + was_deleted = self.NonRedactingModel.delete_by_user_value(value='missing@example.com', field='email') |
| 56 | + |
| 57 | + assert not was_deleted |
| 58 | + mock_objects.filter.assert_called_once_with(email='missing@example.com') |
| 59 | + queryset.update.assert_not_called() |
| 60 | + queryset.delete.assert_not_called() |
| 61 | + |
| 62 | + @ddt.data( |
| 63 | + ('NonRedactingModel', None), |
| 64 | + ('RedactingModel', {'email': 'redacted@retired.invalid'}), |
| 65 | + ) |
| 66 | + @ddt.unpack |
| 67 | + def test_delete_by_user_value(self, model_name, expected_redact_fields): |
| 68 | + """ |
| 69 | + Verify delete behavior with and without redaction configured. |
| 70 | +
|
| 71 | + When no redaction hook is set, rows are deleted directly. |
| 72 | + When a redaction hook is set, fields are updated before deletion. |
| 73 | + """ |
| 74 | + model_cls = getattr(self, model_name) |
| 75 | + queryset = self._make_queryset(exists=True) |
| 76 | + with mock.patch.object(model_cls, 'objects', create=True) as mock_objects: |
| 77 | + mock_objects.filter.return_value = queryset |
| 78 | + |
| 79 | + was_deleted = model_cls.delete_by_user_value(value='learner@example.com', field='email') |
| 80 | + |
| 81 | + assert was_deleted |
| 82 | + assert mock_objects.filter.call_args_list == [ |
| 83 | + mock.call(email='learner@example.com'), |
| 84 | + mock.call(id__in=[11, 12]), |
| 85 | + ] |
| 86 | + if expected_redact_fields: |
| 87 | + queryset.update.assert_called_once_with(**expected_redact_fields) |
| 88 | + else: |
| 89 | + queryset.update.assert_not_called() |
| 90 | + queryset.delete.assert_called_once_with() |
0 commit comments