Skip to content

Commit 887d44d

Browse files
committed
Double check for deleted items since history_date can match
1 parent 27959c8 commit 887d44d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

simple_history/manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def _as_of_set(self, date):
8787
queryset = self.filter(history_date__lte=date)
8888
for original_pk in set(
8989
queryset.order_by().values_list(pk_attr, flat=True)):
90-
last_change = queryset.filter(
91-
**{pk_attr: original_pk}).latest('history_date')
92-
if last_change.history_type != '-':
93-
yield last_change.instance
90+
changes = queryset.filter(**{pk_attr: original_pk})
91+
last_change = changes.latest('history_date')
92+
if changes.filter(history_date=last_change.history_date, history_type='-').exists():
93+
continue
94+
yield last_change.instance

simple_history/tests/tests/test_manager.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,16 @@ def test_modified(self):
6666
as_of_list = list(
6767
self.model.history.as_of(self.now - timedelta(days=1)))
6868
self.assertEqual(as_of_list[0].changed_by, self.obj.changed_by)
69+
70+
71+
class AsOfAdditionalTestCase(TestCase):
72+
73+
def test_create_and_delete(self):
74+
now = datetime.now()
75+
document = models.Document.objects.create()
76+
document.delete()
77+
for doc_change in models.Document.history.all():
78+
doc_change.history_date = now
79+
doc_change.save()
80+
docs_as_of_tmw = models.Document.history.as_of(now + timedelta(days=1))
81+
self.assertFalse(list(docs_as_of_tmw))

0 commit comments

Comments
 (0)