|
12 | 12 | User = get_user_model()
|
13 | 13 |
|
14 | 14 |
|
15 |
| -class AsOfTest(TestCase): |
| 15 | +class AsOfTestCase(TestCase): |
16 | 16 | model = Document
|
17 | 17 |
|
18 | 18 | def setUp(self):
|
@@ -69,7 +69,7 @@ def test_modified(self):
|
69 | 69 | self.assertEqual(as_of_list[0].changed_by, self.obj.changed_by)
|
70 | 70 |
|
71 | 71 |
|
72 |
| -class AsOfAdditionalTestCase(TestCase): |
| 72 | +class AsOfTestCaseWithoutSetUp(TestCase): |
73 | 73 | def test_create_and_delete(self):
|
74 | 74 | document = Document.objects.create()
|
75 | 75 | now = datetime.now()
|
@@ -150,42 +150,57 @@ def test_historical_query_set(self):
|
150 | 150 | """
|
151 | 151 | Demonstrates how the HistoricalQuerySet works to provide as_of functionality.
|
152 | 152 | """
|
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 |
156 | 156 | document2.save()
|
157 | 157 | document1.delete()
|
| 158 | + t1 = datetime.now() |
| 159 | + document3 = RankedDocument.objects.create(rank=30) # noqa: F841 |
| 160 | + document2.rank = 22 |
| 161 | + document2.save() |
158 | 162 | t2 = datetime.now()
|
159 | 163 |
|
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) |
164 | 168 |
|
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) |
167 | 172 |
|
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) |
170 | 177 |
|
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 | + ) |
180 | 189 |
|
181 |
| - # that was all the same as calling as_of! |
182 |
| - self.assertEqual( |
| 190 | + self.assertSetEqual( |
| 191 | + # In conclusion, all of these methods combined... |
183 | 192 | set(
|
184 |
| - RankedDocument.history.filter(history_date__lte=t2) |
| 193 | + RankedDocument.history.filter(history_date__lte=t1) |
185 | 194 | .latest_of_each()
|
186 | 195 | .as_instances()
|
187 | 196 | ),
|
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] |
189 | 204 | )
|
190 | 205 |
|
191 | 206 |
|
|
0 commit comments