Skip to content

Commit

Permalink
Fail hard if an unknown field is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Sep 22, 2021
1 parent ed7dd7d commit 72199cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 2 additions & 5 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 12 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 72199cb

Please sign in to comment.