Skip to content

Commit

Permalink
fix(diff_against): support diffing against deleted records
Browse files Browse the repository at this point in the history
  • Loading branch information
euriostigue committed Feb 20, 2024
1 parent ac44d22 commit f98fec8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Authors
- Dmytro Shyshov (`xahgmah <https://github.com/xahgmah>`_)
- Edouard Richard (`vied12 <https://github.com/vied12>` _)
- Eduardo Cuducos
- Eric Uriostigue (`euriostigue <https://github.com/euriostigue>`_)
- Erik van Widenfelt (`erikvw <https://github.com/erikvw>`_)
- Fábio Capuano (`fabiocapsouza <https://github.com/fabiocapsouza`_)
- Filipe Pina (@fopina)
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changes

Unreleased
----------
- Fixed `diff_against` to work with deleted objects (gh-1312)


3.5.0 (2024-02-19)
Expand Down
4 changes: 2 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,8 @@ def diff_against(self, old_history, excluded_fields=None, included_fields=None):
current_values = model_to_dict(self, fields=fields)

for field in fields:
old_value = old_values[field]
current_value = current_values[field]
old_value = None if old_history.history_type == "-" else old_values[field]
current_value = None if self.history_type == "-" else current_values[field]

if old_value != current_value:
changes.append(ModelChange(field, old_value, current_value))
Expand Down
32 changes: 27 additions & 5 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import warnings
from datetime import datetime, timedelta

import django
from django.apps import apps
from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -28,7 +27,6 @@
pre_create_historical_m2m_records,
pre_create_historical_record,
)
from simple_history.tests.custom_user.models import CustomUser
from simple_history.tests.tests.utils import (
database_router_override_settings,
database_router_override_settings_history_in_diff_db,
Expand Down Expand Up @@ -72,7 +70,6 @@
HistoricalCustomFKError,
HistoricalPoll,
HistoricalPollWithHistoricalIPAddress,
HistoricalPollWithManyToMany_places,
HistoricalState,
InheritedRestaurant,
Library,
Expand All @@ -85,8 +82,6 @@
ModelWithMultipleNoDBIndex,
ModelWithSingleNoDBIndexUnique,
MultiOneToOne,
MyOverrideModelNameRegisterMethod1,
OverrideModelNameAsCallable,
OverrideModelNameUsingBaseModel1,
Person,
Place,
Expand Down Expand Up @@ -800,6 +795,33 @@ def test_history_with_unknown_field(self):
with self.assertNumQueries(0):
new_record.diff_against(old_record, excluded_fields=["unknown_field"])

def test_history_with_deletion_record(self):
question = "what's up?"
p = Poll.objects.create(question=question, pub_date=today)
poll_pk = p.pk
new_record = p.history.first()
p.delete()

deletion_record = HistoricalPoll.objects.get(id=poll_pk, history_type="-")

with self.assertNumQueries(0):
delta = new_record.diff_against(
deletion_record, included_fields=["question"]
)
self.assertEqual(delta.changed_fields, ["question"])
self.assertEqual(len(delta.changes), 1)
self.assertEqual(delta.changes[0].new, question)
self.assertEqual(delta.changes[0].old, None)

with self.assertNumQueries(0):
delta = deletion_record.diff_against(
new_record, included_fields=["question"]
)
self.assertEqual(delta.changed_fields, ["question"])
self.assertEqual(len(delta.changes), 1)
self.assertEqual(delta.changes[0].new, None)
self.assertEqual(delta.changes[0].old, question)


class GetPrevRecordAndNextRecordTestCase(TestCase):
def assertRecordsMatch(self, record_a, record_b):
Expand Down

0 comments on commit f98fec8

Please sign in to comment.