summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Rosner <brosner@gmail.com>2008-07-17 20:12:21 +0000
committerBrian Rosner <brosner@gmail.com>2008-07-17 20:12:21 +0000
commit91880138897886f04b8ea186582a86c1c0417b51 (patch)
tree21f91e69fb4361e404fed3f604cd31f1577e2329
parent1eed9b076c28f3cd1720743cfff5a31fcb5aef5d (diff)
newforms-admin: Merged from trunk up to [7941].
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7944 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/query.py7
-rw-r--r--django/test/utils.py5
-rw-r--r--tests/regressiontests/admin_scripts/tests.py2
-rw-r--r--tests/regressiontests/queries/models.py50
4 files changed, 41 insertions, 23 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index b1921a8e4b..f0a0cf8218 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -280,11 +280,10 @@ class QuerySet(object):
Performs a SELECT COUNT() and returns the number of records as an
integer.
- If the QuerySet is already cached (i.e. self._result_cache is set) this
- simply returns the length of the cached results set to avoid multiple
- SELECT COUNT(*) calls.
+ If the QuerySet is already fully cached this simply returns the length
+ of the cached results set to avoid multiple SELECT COUNT(*) calls.
"""
- if self._result_cache is not None:
+ if self._result_cache is not None and not self._iter:
return len(self._result_cache)
return self.query.get_count()
diff --git a/django/test/utils.py b/django/test/utils.py
index b1c528873c..41de932964 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -74,7 +74,10 @@ def teardown_test_environment():
def _set_autocommit(connection):
"Make sure a connection is in autocommit mode."
if hasattr(connection.connection, "autocommit"):
- connection.connection.autocommit(True)
+ if callable(connection.connection.autocommit):
+ connection.connection.autocommit(True)
+ else:
+ connection.connection.autocommit = True
elif hasattr(connection.connection, "set_isolation_level"):
connection.connection.set_isolation_level(0)
diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
index 3ebcfbb6cd..442f357782 100644
--- a/tests/regressiontests/admin_scripts/tests.py
+++ b/tests/regressiontests/admin_scripts/tests.py
@@ -108,7 +108,7 @@ class AdminScriptTestCase(unittest.TestCase):
self.assertEquals(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
def assertOutput(self, stream, msg):
"Utility assertion: assert that the given message exists in the output"
- self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))
+ self.failUnless(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))
##########################################################################
# DJANGO ADMIN TESTS
diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py
index fb66c6214b..847d515422 100644
--- a/tests/regressiontests/queries/models.py
+++ b/tests/regressiontests/queries/models.py
@@ -4,6 +4,7 @@ Various complex queries that have been problematic in the past.
import datetime
import pickle
+import sys
from django.db import models
from django.db.models.query import Q, ITER_CHUNK_SIZE
@@ -483,23 +484,6 @@ Bug #2076
>>> Cover.objects.all()
[<Cover: first>, <Cover: second>]
-# If you're not careful, it's possible to introduce infinite loops via default
-# ordering on foreign keys in a cycle. We detect that.
->>> LoopX.objects.all()
-Traceback (most recent call last):
-...
-FieldError: Infinite loop caused by ordering.
-
->>> LoopZ.objects.all()
-Traceback (most recent call last):
-...
-FieldError: Infinite loop caused by ordering.
-
-# ... but you can still order in a non-recursive fashion amongst linked fields
-# (the previous test failed because the default ordering was recursive).
->>> LoopX.objects.all().order_by('y__x__y__x__id')
-[]
-
# If the remote model does not have a default ordering, we order by its 'id'
# field.
>>> Item.objects.order_by('creator', 'name')
@@ -830,5 +814,37 @@ another cursor.
... obj.save()
... if i > 10: break
+Bug #7759 -- count should work with a partially read result set.
+>>> count = Number.objects.count()
+>>> qs = Number.objects.all()
+>>> for obj in qs:
+... qs.count() == count
+... break
+True
+
"""}
+# In Python 2.3, exceptions raised in __len__ are swallowed (Python issue
+# 1242657), so these cases return an empty list, rather than raising an
+# exception. Not a lot we can do about that, unfortunately, due to the way
+# Python handles list() calls internally. Thus, we skip the tests for Python
+# 2.3.
+if sys.version_info >= (2, 4):
+ __test__["API_TESTS"] += """
+# If you're not careful, it's possible to introduce infinite loops via default
+# ordering on foreign keys in a cycle. We detect that.
+>>> LoopX.objects.all()
+Traceback (most recent call last):
+...
+FieldError: Infinite loop caused by ordering.
+
+>>> LoopZ.objects.all()
+Traceback (most recent call last):
+...
+FieldError: Infinite loop caused by ordering.
+
+# ... but you can still order in a non-recursive fashion amongst linked fields
+# (the previous test failed because the default ordering was recursive).
+>>> LoopX.objects.all().order_by('y__x__y__x__id')
+[]
+"""