|
@@ -1,6 +1,6 @@
|
|
|
from django.test import TestCase
|
|
|
|
|
|
-from ..models import CacheVersion
|
|
|
+from ...cache.models import CacheVersion
|
|
|
from ..pgutils import chunk_queryset
|
|
|
|
|
|
|
|
@@ -9,27 +9,26 @@ class ChunkQuerysetTest(TestCase):
|
|
|
# clear table
|
|
|
CacheVersion.objects.all().delete()
|
|
|
|
|
|
- # create 100 items
|
|
|
- items_ids = []
|
|
|
- for _ in range(100):
|
|
|
- obj = CacheVersion.objects.create(cache="nomatter")
|
|
|
- items_ids.append(obj.id)
|
|
|
- self.items_ids = list(reversed(items_ids))
|
|
|
+ items_pks = []
|
|
|
+ for i in range(50):
|
|
|
+ obj = CacheVersion.objects.create(cache="test%s" % i)
|
|
|
+ items_pks.append(obj.pk)
|
|
|
+ self.items_pks = list(sorted(items_pks, reverse=True))
|
|
|
|
|
|
def test_chunk_queryset(self):
|
|
|
"""chunk_queryset utility chunks queryset but returns all items"""
|
|
|
- chunked_ids = []
|
|
|
+ chunked_pks = []
|
|
|
|
|
|
- with self.assertNumQueries(21):
|
|
|
- queryset = CacheVersion.objects.all()
|
|
|
+ with self.assertNumQueries(11):
|
|
|
+ queryset = CacheVersion.objects.order_by("cache")
|
|
|
for obj in chunk_queryset(queryset, chunk_size=5):
|
|
|
- chunked_ids.append(obj.id)
|
|
|
+ chunked_pks.append(obj.pk)
|
|
|
|
|
|
- self.assertEqual(chunked_ids, self.items_ids)
|
|
|
+ self.assertEqual(chunked_pks, self.items_pks)
|
|
|
|
|
|
def test_chunk_shrinking_queryset(self):
|
|
|
"""chunk_queryset utility chunks queryset in delete action"""
|
|
|
- with self.assertNumQueries(121):
|
|
|
+ with self.assertNumQueries(61):
|
|
|
queryset = CacheVersion.objects.all()
|
|
|
for obj in chunk_queryset(queryset, chunk_size=5):
|
|
|
obj.delete()
|