Ticket #27729: evaluate.diff

File evaluate.diff, 2.5 KB (added by AlbinLindskog, 8 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index e059c68..6ed6e08 100644
    a b class QuerySet(object):  
    992992        clone._db = alias
    993993        return clone
    994994
     995    def evaluate(self):
     996        """
     997        Returns a new, evaluated QuerySet instance.
     998        """
     999        clone = self._clone()
     1000        clone._fetch_all()
     1001        return clone
     1002
     1003
    9951004    ###################################
    9961005    # PUBLIC INTROSPECTION ATTRIBUTES #
    9971006    ###################################
  • tests/queries/tests.py

    diff --git a/tests/queries/tests.py b/tests/queries/tests.py
    index 8f0af65..3921510 100644
    a b from operator import attrgetter  
    88
    99from django.core.exceptions import EmptyResultSet, FieldError
    1010from django.db import DEFAULT_DB_ALIAS, connection
    11 from django.db.models import Count, F, Q
     11from django.db.models import Count, F, Q, QuerySet
    1212from django.db.models.sql.constants import LOUTER
    1313from django.db.models.sql.where import NothingNode, WhereNode
    1414from django.test import TestCase, skipUnlessDBFeature
    class Ticket23622Tests(TestCase):  
    37973797            set(Ticket23605A.objects.filter(qy).values_list('pk', flat=True))
    37983798        )
    37993799        self.assertSequenceEqual(Ticket23605A.objects.filter(qx), [a2])
     3800
     3801
     3802class EvaluateMethodTests(TestCase):
     3803    @classmethod
     3804    def setUpTestData(cls):
     3805        Number.objects.create(num=4)
     3806        Number.objects.create(num=4)
     3807        Number.objects.create(num=8)
     3808
     3809    def test_evaluate_allows_chain_methods(self):
     3810        """
     3811        Tests that evaluate indeed returns a QuerySet.
     3812        """
     3813        numbers = Number.objects.evaluate()
     3814        self.assertIsInstance(numbers, QuerySet)
     3815
     3816    def test_evaluate_populates_result_cache(self):
     3817        """
     3818        Tests that evaluate() populates the result cache.
     3819        """
     3820        numbers = Number.objects.filter(num__lte=6)
     3821        self.assertIsNone(numbers._result_cache)
     3822        numbers = numbers.evaluate()
     3823        self.assertTrue(numbers._result_cache)
     3824
     3825    def test_evaluate_does_not_query_database(self):
     3826        """
     3827        Tests that subsequent QuerySet operations does not query the database after evaluate have been called.
     3828        """
     3829        def test_function():
     3830            numbers = Number.objects.filter(num__lte=6)
     3831            numbers = numbers.evaluate()
     3832            numbers = numbers.filter(num__lte=5)
     3833
     3834        self.assertNumQueries(1, lambda: test_function())
Back to Top