wagtail / django-modelcluster

Django extension to allow working with 'clusters' of models as a single unit, independently of the database
BSD 3-Clause "New" or "Revised" License
485 stars 66 forks source link

Add support for randomized ordering of fakequeryset #187

Closed KIRA009 closed 9 months ago

KIRA009 commented 9 months ago

Fixes #111

This PR randomizes the queryset by using random.random() values as the key during sorting of the results.

KIRA009 commented 9 months ago

I am wondering how do we test this behaviour?

I thought of something like this, where we run the query n number of times, and if the ordering is different from what was seen previously, the test passes.

recognized_order = ' '.join(tuple(album.name for album in beatles.albums.order_by('?')))
for _ in range(100):
    new_order = ' '.join(tuple(album.name for album in beatles.albums.order_by('?')))
    if new_order != recognized_order:
        break
else:
    self.fail("Ordering was the same in all 100 queries")

But I am not sure if this is the best way to do this

gasman commented 9 months ago

It's probably fine to just run it once and verify that it contains the expected items - since that's the only thing you can rely on when using it in the real world.

gasman commented 9 months ago

Merged in df4c6be8ed3de4da81921d78ece7ce95f61ca50a - thanks @KIRA009!