Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(diff_against): support diffing against deleted records #1312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

- Support custom History ``Manager`` and ``QuerySet`` classes (gh-1280)

Expand Down
4 changes: 2 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,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 @@ -837,6 +832,33 @@ def test_history_with_custom_queryset(self):
{"Question 1"},
)

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
Loading