diff options
| author | Anssi Kääriäinen <akaariai@gmail.com> | 2012-10-24 00:04:37 +0300 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-01-06 19:18:28 +0200 |
| commit | a2396a4c8f2ccd7f91adee6d8c2e9c31f13f0e3f (patch) | |
| tree | f1f67939a21dbf2ac08db770b9b6776754d05ad9 /tests | |
| parent | a843539af2f557e9bdc71b9b5ef66eabe0e39e3c (diff) | |
Fixed #19173 -- Made EmptyQuerySet a marker class only
The guarantee that no queries will be made when accessing results is
done by new EmptyWhere class which is used for query.where and having.
Thanks to Simon Charette for reviewing and valuable suggestions.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/basic/tests.py | 7 | ||||
| -rw-r--r-- | tests/modeltests/get_object_or_404/tests.py | 2 | ||||
| -rw-r--r-- | tests/modeltests/lookup/tests.py | 2 | ||||
| -rw-r--r-- | tests/regressiontests/queries/tests.py | 59 |
4 files changed, 39 insertions, 31 deletions
diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py index 1c83b980a7..dba9a686d9 100644 --- a/tests/modeltests/basic/tests.py +++ b/tests/modeltests/basic/tests.py @@ -4,6 +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.test import TestCase, skipIfDBFeature, skipUnlessDBFeature from django.utils import six from django.utils.translation import ugettext_lazy @@ -639,3 +640,9 @@ class ModelTest(TestCase): Article.objects.bulk_create([Article(headline=lazy, pub_date=datetime.now())]) article = Article.objects.get() self.assertEqual(article.headline, notlazy) + + def test_emptyqs(self): + # Can't be instantiated + with self.assertRaises(TypeError): + EmptyQuerySet() + self.assertTrue(isinstance(Article.objects.none(), EmptyQuerySet)) diff --git a/tests/modeltests/get_object_or_404/tests.py b/tests/modeltests/get_object_or_404/tests.py index 3b234c6cd3..38ebeb4f8c 100644 --- a/tests/modeltests/get_object_or_404/tests.py +++ b/tests/modeltests/get_object_or_404/tests.py @@ -53,7 +53,7 @@ class GetObjectOr404Tests(TestCase): get_object_or_404, Author.objects.all() ) - # Using an EmptyQuerySet raises a Http404 error. + # Using an empty QuerySet raises a Http404 error. self.assertRaises(Http404, get_object_or_404, Article.objects.none(), title__contains="Run" ) diff --git a/tests/modeltests/lookup/tests.py b/tests/modeltests/lookup/tests.py index 98358e3d10..de7105f92d 100644 --- a/tests/modeltests/lookup/tests.py +++ b/tests/modeltests/lookup/tests.py @@ -436,7 +436,7 @@ class LookupTests(TestCase): ]) def test_none(self): - # none() returns an EmptyQuerySet that behaves like any other QuerySet object + # none() returns a QuerySet that behaves like any other QuerySet object self.assertQuerysetEqual(Article.objects.none(), []) self.assertQuerysetEqual( Article.objects.none().filter(headline__startswith='Article'), []) diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py index e3e515025c..7d01c16255 100644 --- a/tests/regressiontests/queries/tests.py +++ b/tests/regressiontests/queries/tests.py @@ -9,7 +9,7 @@ from django.conf import settings from django.core.exceptions import FieldError from django.db import DatabaseError, connection, connections, DEFAULT_DB_ALIAS from django.db.models import Count, F, Q -from django.db.models.query import ITER_CHUNK_SIZE, EmptyQuerySet +from django.db.models.query import ITER_CHUNK_SIZE from django.db.models.sql.where import WhereNode, EverythingNode, NothingNode from django.db.models.sql.datastructures import EmptyResultSet from django.test import TestCase, skipUnlessDBFeature @@ -663,31 +663,32 @@ class Queries1Tests(BaseQuerysetTest): Item.objects.filter(created__in=[self.time1, self.time2]), ['<Item: one>', '<Item: two>'] ) - def test_ticket7235(self): # An EmptyQuerySet should not raise exceptions if it is filtered. - q = EmptyQuerySet() - self.assertQuerysetEqual(q.all(), []) - self.assertQuerysetEqual(q.filter(x=10), []) - self.assertQuerysetEqual(q.exclude(y=3), []) - self.assertQuerysetEqual(q.complex_filter({'pk': 1}), []) - self.assertQuerysetEqual(q.select_related('spam', 'eggs'), []) - self.assertQuerysetEqual(q.annotate(Count('eggs')), []) - self.assertQuerysetEqual(q.order_by('-pub_date', 'headline'), []) - self.assertQuerysetEqual(q.distinct(), []) - self.assertQuerysetEqual( - q.extra(select={'is_recent': "pub_date > '2006-01-01'"}), - [] - ) - q.query.low_mark = 1 - self.assertRaisesMessage( - AssertionError, - 'Cannot change a query once a slice has been taken', - q.extra, select={'is_recent': "pub_date > '2006-01-01'"} - ) - self.assertQuerysetEqual(q.reverse(), []) - self.assertQuerysetEqual(q.defer('spam', 'eggs'), []) - self.assertQuerysetEqual(q.only('spam', 'eggs'), []) + Eaten.objects.create(meal='m') + q = Eaten.objects.none() + with self.assertNumQueries(0): + self.assertQuerysetEqual(q.all(), []) + self.assertQuerysetEqual(q.filter(meal='m'), []) + self.assertQuerysetEqual(q.exclude(meal='m'), []) + self.assertQuerysetEqual(q.complex_filter({'pk': 1}), []) + self.assertQuerysetEqual(q.select_related('food'), []) + self.assertQuerysetEqual(q.annotate(Count('food')), []) + self.assertQuerysetEqual(q.order_by('meal', 'food'), []) + self.assertQuerysetEqual(q.distinct(), []) + self.assertQuerysetEqual( + q.extra(select={'foo': "1"}), + [] + ) + q.query.low_mark = 1 + self.assertRaisesMessage( + AssertionError, + 'Cannot change a query once a slice has been taken', + q.extra, select={'foo': "1"} + ) + self.assertQuerysetEqual(q.reverse(), []) + self.assertQuerysetEqual(q.defer('meal'), []) + self.assertQuerysetEqual(q.only('meal'), []) def test_ticket7791(self): # There were "issues" when ordering and distinct-ing on fields related @@ -1935,8 +1936,8 @@ class CloneTests(TestCase): class EmptyQuerySetTests(TestCase): def test_emptyqueryset_values(self): - # #14366 -- Calling .values() on an EmptyQuerySet and then cloning that - # should not cause an error" + # #14366 -- Calling .values() on an empty QuerySet and then cloning + # that should not cause an error self.assertQuerysetEqual( Number.objects.none().values('num').order_by('num'), [] ) @@ -1952,9 +1953,9 @@ class EmptyQuerySetTests(TestCase): ) def test_ticket_19151(self): - # #19151 -- Calling .values() or .values_list() on an EmptyQuerySet - # should return EmptyQuerySet and not cause an error. - q = EmptyQuerySet() + # #19151 -- Calling .values() or .values_list() on an empty QuerySet + # should return an empty QuerySet and not cause an error. + q = Author.objects.none() self.assertQuerysetEqual(q.values(), []) self.assertQuerysetEqual(q.values_list(), []) |
