Skip to content

Commit

Permalink
fix(documents): fix get_indexing_queryset() returning unpredictable r…
Browse files Browse the repository at this point in the history
…esults with unordered querysets

Fixes Codoc-os#29
  • Loading branch information
cedricraud committed Nov 9, 2022
1 parent e6d691e commit 386916c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions django_opensearch_dsl/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def get_indexing_queryset(
"""Divide the queryset into chunks."""
chunk_size = self.django.queryset_pagination
qs = self.get_queryset(filter_=filter_, exclude=exclude, count=count)
qs = qs.order_by('pk') if not qs.query.is_sliced else qs
count = qs.count()
model = self.django.model.__name__
action = action.present_participle.title()
Expand Down
15 changes: 15 additions & 0 deletions tests/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from django_opensearch_dsl.exceptions import ModelFieldNotMappedError, RedeclaredFieldError
from django_opensearch_dsl.registries import DocumentRegistry

from django_dummy_app.models import Continent
from django_dummy_app.documents import ContinentDocument

registry = DocumentRegistry()


Expand Down Expand Up @@ -72,6 +75,8 @@ class Index:


class DocumentTestCase(TestCase):
fixtures = ["tests/django_dummy_app/geography_data.json"]

def test_model_class_added(self):
self.assertEqual(CarDocument.django.model, Car)

Expand Down Expand Up @@ -150,6 +155,16 @@ def test_get_queryset(self):
self.assertIsInstance(qs, models.QuerySet)
self.assertEqual(qs.model, Car)

def test_get_indexing_queryset(self):
doc = ContinentDocument()
unordered_qs = doc.get_queryset().order_by('?')

with patch("django_opensearch_dsl.documents.Document.get_queryset") as mock_qs:
mock_qs.return_value = unordered_qs
ordered_continents = list(doc.get_queryset().order_by('pk'))
indexing_continents = list(doc.get_indexing_queryset())
self.assertEqual(ordered_continents, indexing_continents)

def test_prepare(self):
car = Car(name="Type 57", price=5400000.0, not_indexed="not_indexex")
doc = CarDocument()
Expand Down

0 comments on commit 386916c

Please sign in to comment.