Skip to content

Commit e5c8a64

Browse files
committed
Improved as_of() test case
1 parent 08001c9 commit e5c8a64

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

simple_history/tests/tests/test_manager.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
User = get_user_model()
1313

1414

15-
class AsOfTest(TestCase):
15+
class AsOfTestCase(TestCase):
1616
model = Document
1717

1818
def setUp(self):
@@ -69,7 +69,7 @@ def test_modified(self):
6969
self.assertEqual(as_of_list[0].changed_by, self.obj.changed_by)
7070

7171

72-
class AsOfAdditionalTestCase(TestCase):
72+
class AsOfTestCaseWithoutSetUp(TestCase):
7373
def test_create_and_delete(self):
7474
document = Document.objects.create()
7575
now = datetime.now()
@@ -150,42 +150,57 @@ def test_historical_query_set(self):
150150
"""
151151
Demonstrates how the HistoricalQuerySet works to provide as_of functionality.
152152
"""
153-
document1 = RankedDocument.objects.create(rank=42)
154-
document2 = RankedDocument.objects.create(rank=84)
155-
document2.rank = 51
153+
document1 = RankedDocument.objects.create(rank=10)
154+
document2 = RankedDocument.objects.create(rank=20)
155+
document2.rank = 21
156156
document2.save()
157157
document1.delete()
158+
t1 = datetime.now()
159+
document3 = RankedDocument.objects.create(rank=30) # noqa: F841
160+
document2.rank = 22
161+
document2.save()
158162
t2 = datetime.now()
159163

160-
# look for historical records, get back a queryset
161-
with self.assertNumQueries(1):
162-
queryset = RankedDocument.history.filter(history_date__lte=t2)
163-
self.assertEqual(queryset.count(), 4)
164+
# 4 records before `t1` (for document 1 and 2), 2 after (for document 2 and 3)
165+
queryset = RankedDocument.history.filter(history_date__lte=t1)
166+
self.assertEqual(queryset.count(), 4)
167+
self.assertEqual(RankedDocument.history.filter(history_date__gt=t1).count(), 2)
164168

165-
# only want the most recend records (provided by HistoricalQuerySet)
166-
self.assertEqual(queryset.latest_of_each().count(), 2)
169+
# `latest_of_each()` returns the most recent record of each document
170+
with self.assertNumQueries(1):
171+
self.assertEqual(queryset.latest_of_each().count(), 2)
167172

168-
# want to see the instances as of that time?
169-
self.assertEqual(queryset.latest_of_each().as_instances().count(), 1)
173+
# `as_instances()` returns the historical instances as of each record's time,
174+
# but excludes deletion records (i.e. document 1's most recent record)
175+
with self.assertNumQueries(1):
176+
self.assertEqual(queryset.latest_of_each().as_instances().count(), 1)
170177

171-
# these new methods are idempotent
172-
self.assertEqual(
173-
queryset.latest_of_each()
174-
.latest_of_each()
175-
.as_instances()
176-
.as_instances()
177-
.count(),
178-
1,
179-
)
178+
# (Duplicate calls to these methods should not change the number of queries,
179+
# since they're idempotent)
180+
with self.assertNumQueries(1):
181+
self.assertEqual(
182+
queryset.latest_of_each()
183+
.latest_of_each()
184+
.as_instances()
185+
.as_instances()
186+
.count(),
187+
1,
188+
)
180189

181-
# that was all the same as calling as_of!
182-
self.assertEqual(
190+
self.assertSetEqual(
191+
# In conclusion, all of these methods combined...
183192
set(
184-
RankedDocument.history.filter(history_date__lte=t2)
193+
RankedDocument.history.filter(history_date__lte=t1)
185194
.latest_of_each()
186195
.as_instances()
187196
),
188-
set(RankedDocument.history.as_of(t2)),
197+
# ...are equivalent to calling `as_of()`!
198+
set(RankedDocument.history.as_of(t1)),
199+
)
200+
201+
self.assertEqual(RankedDocument.history.as_of(t1).get().rank, 21)
202+
self.assertListEqual(
203+
[d.rank for d in RankedDocument.history.as_of(t2)], [22, 30]
189204
)
190205

191206

0 commit comments

Comments
 (0)