summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-02-16 12:28:37 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-02-16 12:28:37 +0000
commit58ea6d45618cb8b5bd4306c395d7bb8860f9a842 (patch)
tree62c4bfe83ecff6ed7e5a7c0d8d1ef9d17803f127 /tests
parent88837875f245672a5e8671a1d144c78d3e102450 (diff)
Fixed #10256 -- Corrected the interaction of extra(select=) with values() and values_list() where an explicit list of columns is requested.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9837 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/aggregation_regress/models.py2
-rw-r--r--tests/regressiontests/extra_regress/models.py76
2 files changed, 77 insertions, 1 deletions
diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
index fc2c44b09e..8eac67a0fc 100644
--- a/tests/regressiontests/aggregation_regress/models.py
+++ b/tests/regressiontests/aggregation_regress/models.py
@@ -91,7 +91,7 @@ __test__ = {'API_TESTS': """
>>> sorted(Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).values().get(pk=2).items())
[('contact_id', 3), ('id', 2), ('isbn', u'067232959'), ('manufacture_cost', ...11.545...), ('mean_auth_age', 45.0), ('name', u'Sams Teach Yourself Django in 24 Hours'), ('pages', 528), ('price', Decimal("23.09")), ('pubdate', datetime.date(2008, 3, 3)), ('publisher_id', 2), ('rating', 3.0)]
-# The order of the values, annotate and extra clauses doesn't matter
+# The order of the (empty) values, annotate and extra clauses doesn't matter
>>> sorted(Book.objects.all().values().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).get(pk=2).items())
[('contact_id', 3), ('id', 2), ('isbn', u'067232959'), ('manufacture_cost', ...11.545...), ('mean_auth_age', 45.0), ('name', u'Sams Teach Yourself Django in 24 Hours'), ('pages', 528), ('price', Decimal("23.09")), ('pubdate', datetime.date(2008, 3, 3)), ('publisher_id', 2), ('rating', 3.0)]
diff --git a/tests/regressiontests/extra_regress/models.py b/tests/regressiontests/extra_regress/models.py
index 680917b8ae..fd34982c9a 100644
--- a/tests/regressiontests/extra_regress/models.py
+++ b/tests/regressiontests/extra_regress/models.py
@@ -30,6 +30,11 @@ class Order(models.Model):
created_by = models.ForeignKey(User)
text = models.TextField()
+class TestObject(models.Model):
+ first = models.CharField(max_length=20)
+ second = models.CharField(max_length=20)
+ third = models.CharField(max_length=20)
+
__test__ = {"API_TESTS": """
# Regression tests for #7314 and #7372
@@ -115,4 +120,75 @@ True
# cause incorrect SQL to be produced otherwise.
>>> RevisionableModel.objects.extra(select={"the_answer": 'id'}).dates('when', 'month')
[datetime.datetime(2008, 9, 1, 0, 0)]
+
+# Regression test for #10256... If there is a values() clause, Extra columns are
+# only returned if they are explicitly mentioned.
+>>> TestObject(first='first', second='second', third='third').save()
+
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values()
+[{'bar': u'second', 'third': u'third', 'second': u'second', 'whiz': u'third', 'foo': u'first', 'id': 1, 'first': u'first'}]
+
+# Extra clauses after an empty values clause are still included
+>>> TestObject.objects.values().extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
+[{'bar': u'second', 'third': u'third', 'second': u'second', 'whiz': u'third', 'foo': u'first', 'id': 1, 'first': u'first'}]
+
+# Extra columns are ignored if not mentioned in the values() clause
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values('first', 'second')
+[{'second': u'second', 'first': u'first'}]
+
+# Extra columns after a non-empty values() clause are ignored
+>>> TestObject.objects.values('first', 'second').extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
+[{'second': u'second', 'first': u'first'}]
+
+# Extra columns can be partially returned
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values('first', 'second', 'foo')
+[{'second': u'second', 'foo': u'first', 'first': u'first'}]
+
+# Also works if only extra columns are included
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values('foo', 'whiz')
+[{'foo': u'first', 'whiz': u'third'}]
+
+# Values list works the same way
+# All columns are returned for an empty values_list()
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list()
+[(u'first', u'second', u'third', 1, u'first', u'second', u'third')]
+
+# Extra columns after an empty values_list() are still included
+>>> TestObject.objects.values_list().extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
+[(u'first', u'second', u'third', 1, u'first', u'second', u'third')]
+
+# Extra columns ignored completely if not mentioned in values_list()
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('first', 'second')
+[(u'first', u'second')]
+
+# Extra columns after a non-empty values_list() clause are ignored completely
+>>> TestObject.objects.values_list('first', 'second').extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
+[(u'first', u'second')]
+
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('second', flat=True)
+[u'second']
+
+# Only the extra columns specified in the values_list() are returned
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('first', 'second', 'whiz')
+[(u'first', u'second', u'third')]
+
+# ...also works if only extra columns are included
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('foo','whiz')
+[(u'first', u'third')]
+
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz', flat=True)
+[u'third']
+
+# ... and values are returned in the order they are specified
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz','foo')
+[(u'third', u'first')]
+
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('first','id')
+[(u'first', 1)]
+
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz', 'first', 'bar', 'id')
+[(u'third', u'first', u'second', 1)]
+
"""}
+
+