From 72199cb1a80fdf35a531804dcc70f33295cf0ea6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 22 Sep 2021 12:39:55 +0100 Subject: [PATCH] Fail hard if an unknown field is provided --- simple_history/models.py | 7 ++----- simple_history/tests/tests/test_models.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/simple_history/models.py b/simple_history/models.py index d9aea9d9d..056aa6197 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -608,11 +608,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: - try: - old_value = old_values[field] - current_value = current_values[field] - except KeyError: - continue + old_value = old_values[field] + current_value = current_values[field] if old_value != current_value: changes.append(ModelChange(field, old_value, current_value)) diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py index 0687f9413..d673f2833 100644 --- a/simple_history/tests/tests/test_models.py +++ b/simple_history/tests/tests/test_models.py @@ -687,6 +687,18 @@ def test_history_diff_with_included_fields(self): self.assertEqual(delta.changed_fields, ["question"]) self.assertEqual(len(delta.changes), 1) + def test_history_with_unknown_field(self): + p = Poll.objects.create(question="what's up?", pub_date=today) + p.question = "what's up, man?" + p.save() + new_record, old_record = p.history.all() + with self.assertRaises(KeyError): + with self.assertNumQueries(0): + new_record.diff_against(old_record, included_fields=["unknown_field"]) + + with self.assertNumQueries(0): + new_record.diff_against(old_record, excluded_fields=["unknown_field"]) + class GetPrevRecordAndNextRecordTestCase(TestCase): def assertRecordsMatch(self, record_a, record_b):