Assume i have the following function:
def select_queryset(value_to_decide_upon):
""" this function returns either any of some querysets or nothing """
if value_to_decide_upon == 1:
return User._default_manager.all()
elif value_to_decide_upon == 2:
return User._default_manager.filter(pk__in=[some_more_values])
elif value_to_decide_upon == n-1:
return User._default_manager.all().order_by('?')
elif value_to_decide_upon == n:
return None
Now here is the question: this function has a randomly ordered queryset as a return value:
queryset = User._default_manager.all().order_by('?')
Now the only important thing about that function is: it has to return the correct queryset. Is there a way to access the queryset in such way that i can, ie. do something like this:
class TestQuerysetSelection(TestCase):
def test_return_value(self):
# this, of course, will always fail:
self.assertEqual(select_queryset(n-1),
User._default_manager.all().order_by('?') )
# and this is not working as well
self.assertEqual( templatetag.queryset.order_by, '?' )
So how can i test if the function returns the correct queryset, without brute force comparing to all other querysets?
I think the most starighforward way is to compare sets (which are unordered).
self.assertEqual(
set(select_queryset(n-1)),
set(User._default_manager.all())
)
You could override order by and sort by pk, so you'll have order in your querysets and not random order. I'm not sure though what's the best way to compare querysets.
class TestQuerysetSelection(TestCase):
def test_return_value(self):
# Add `order_by('id')` to sort by id
self.assertEqual(select_queryset(n-1).order_by('id'),
User._default_manager.all().order_by('id') )
Try
qs1.query.sql_with_params() == qs2.query.sql_with_params()
# or simply
str(qs1.query) == str(qs2.query)
# for your code
self.assertEqual(select_queryset(n-1).query.sql_with_params(),
User._default_manager.all().order_by('?').query.sql_with_params())
Furthermore, I think you need a more concise function to return these querysets.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.