From 69a46c5ca7d4e6819096af88cd8d51174efd46df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Mon, 17 Dec 2012 23:48:23 +0200 Subject: [PATCH] Tests for various emptyqs tickets The tickets are either about different signature between qs.none() and qs or problems with subclass types (either EmptyQS overrided the custom qs class, or EmptyQS was overridden by another class - values() did this). Fixed #15959, fixed #17271, fixed #17712, fixed #19426 --- tests/modeltests/basic/tests.py | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py index dba9a686d9..42375ceed9 100644 --- a/tests/modeltests/basic/tests.py +++ b/tests/modeltests/basic/tests.py @@ -4,7 +4,7 @@ from datetime import datetime from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.db.models.fields import Field, FieldDoesNotExist -from django.db.models.query import EmptyQuerySet +from django.db.models.query import QuerySet, EmptyQuerySet, ValuesListQuerySet from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature from django.utils import six from django.utils.translation import ugettext_lazy @@ -646,3 +646,43 @@ class ModelTest(TestCase): with self.assertRaises(TypeError): EmptyQuerySet() self.assertTrue(isinstance(Article.objects.none(), EmptyQuerySet)) + + def test_emptyqs_values(self): + # test for #15959 + Article.objects.create(headline='foo', pub_date=datetime.now()) + with self.assertNumQueries(0): + qs = Article.objects.none().values_list('pk') + self.assertTrue(isinstance(qs, EmptyQuerySet)) + self.assertTrue(isinstance(qs, ValuesListQuerySet)) + self.assertEqual(len(qs), 0) + + def test_emptyqs_customqs(self): + # A hacky test for custom QuerySet subclass - refs #17271 + Article.objects.create(headline='foo', pub_date=datetime.now()) + class CustomQuerySet(QuerySet): + def do_something(self): + return 'did something' + + qs = Article.objects.all() + qs.__class__ = CustomQuerySet + qs = qs.none() + with self.assertNumQueries(0): + self.assertEqual(len(qs), 0) + self.assertTrue(isinstance(qs, EmptyQuerySet)) + self.assertEqual(qs.do_something(), 'did something') + + def test_emptyqs_values_order(self): + # Tests for ticket #17712 + Article.objects.create(headline='foo', pub_date=datetime.now()) + with self.assertNumQueries(0): + self.assertEqual(len(Article.objects.none().values_list('id').order_by('id')), 0) + with self.assertNumQueries(0): + self.assertEqual(len(Article.objects.none().filter( + id__in=Article.objects.values_list('id', flat=True))), 0) + + @skipUnlessDBFeature('can_distinct_on_fields') + def test_emptyqs_distinct(self): + # Tests for #19426 + Article.objects.create(headline='foo', pub_date=datetime.now()) + with self.assertNumQueries(0): + self.assertEqual(len(Article.objects.none().distinct('headline', 'pub_date')), 0)