diff options
| author | Alex Gaynor <alex.gaynor@gmail.com> | 2010-11-26 14:24:08 +0000 |
|---|---|---|
| committer | Alex Gaynor <alex.gaynor@gmail.com> | 2010-11-26 14:24:08 +0000 |
| commit | 678f626c24f389b03d4bfe2c552c33e9bdcc9379 (patch) | |
| tree | d98434d8ebc96ef7b191a12091f3a8268609ea0e /tests | |
| parent | 3cbaf3c2b66ada38bb1ca01b6404350ef5345f9e (diff) | |
Fixed a suite of errors in the ORM -- a) fixed calling values_list().values_list() and changing whether the results are flat, b) fixed an issue with fields on the left-hand side of what becomes the HAVING clause not being included in the GROUP BY clause, and c) fixed a bug with fields from values() calls not being included in the GROUP BY clause. This fixed the recent test failures under postgresql.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14715 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/aggregation_regress/tests.py | 19 | ||||
| -rw-r--r-- | tests/regressiontests/queries/tests.py | 31 |
2 files changed, 40 insertions, 10 deletions
diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py index 9b9d5d4be8..33e37d6a9a 100644 --- a/tests/regressiontests/aggregation_regress/tests.py +++ b/tests/regressiontests/aggregation_regress/tests.py @@ -654,6 +654,25 @@ class AggregationTests(TestCase): attrgetter("name") ) + def test_values_annotate_values(self): + qs = Book.objects.values("name").annotate( + n_authors=Count("authors") + ).values_list("pk", flat=True) + self.assertEqual(list(qs), list(Book.objects.values_list("pk", flat=True))) + + def test_having_group_by(self): + # Test that when a field occurs on the LHS of a HAVING clause that it + # appears correctly in the GROUP BY clause + qs = Book.objects.values_list("name").annotate( + n_authors=Count("authors") + ).filter( + pages__gt=F("n_authors") + ).values_list("name", flat=True) + # Results should be the same, all Books have more pages than authors + self.assertEqual( + list(qs), list(Book.objects.values_list("name", flat=True)) + ) + @skipUnlessDBFeature('supports_stddev') def test_stddev(self): self.assertEqual( diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py index 7a7705d6bd..ab75eb62ca 100644 --- a/tests/regressiontests/queries/tests.py +++ b/tests/regressiontests/queries/tests.py @@ -1093,11 +1093,12 @@ class Queries5Tests(TestCase): # Extra tables used to crash SQL construction on the second use. qs = Ranking.objects.extra(tables=['django_site']) qs.query.get_compiler(qs.db).as_sql() - qs.query.get_compiler(qs.db).as_sql() # test passes if this doesn't raise an exception. + # test passes if this doesn't raise an exception. + qs.query.get_compiler(qs.db).as_sql() def test_ticket9848(self): - # Make sure that updates which only filter on sub-tables don't inadvertently - # update the wrong records (bug #9848). + # Make sure that updates which only filter on sub-tables don't + # inadvertently update the wrong records (bug #9848). # Make sure that the IDs from different tables don't happen to match. self.assertQuerysetEqual( @@ -1283,15 +1284,15 @@ class Queries6Tests(TestCase): ) # The annotation->tag link is single values and tag->children links is - # multi-valued. So we have to split the exclude filter in the middle and then - # optimise the inner query without losing results. + # multi-valued. So we have to split the exclude filter in the middle + # and then optimise the inner query without losing results. self.assertQuerysetEqual( Annotation.objects.exclude(tag__children__name="t2"), ['<Annotation: a2>'] ) - # Nested queries are possible (although should be used with care, since they have - # performance problems on backends like MySQL. + # Nested queries are possible (although should be used with care, since + # they have performance problems on backends like MySQL. self.assertQuerysetEqual( Annotation.objects.filter(notes__in=Note.objects.filter(note="n1")), @@ -1301,7 +1302,7 @@ class Queries6Tests(TestCase): def test_ticket3739(self): # The all() method on querysets returns a copy of the queryset. q1 = Tag.objects.order_by('name') - self.assertNotEqual(id(q1), id(q1.all())) + self.assertIsNot(q1, q1.all()) class GeneratorExpressionTests(TestCase): @@ -1452,6 +1453,16 @@ class EmptyQuerySetTests(TestCase): ) +class ValuesQuerysetTests(BaseQuerysetTest): + def test_flat_values_lits(self): + Number.objects.create(num=72) + qs = Number.objects.values_list("num") + qs = qs.values_list("num", flat=True) + self.assertValueQuerysetEqual( + qs, [72] + ) + + class WeirdQuerysetSlicingTests(BaseQuerysetTest): def setUp(self): Number.objects.create(num=1) @@ -1481,8 +1492,8 @@ class WeirdQuerysetSlicingTests(BaseQuerysetTest): class EscapingTests(TestCase): def test_ticket_7302(self): # Reserved names are appropriately escaped - _ = ReservedName.objects.create(name='a',order=42) - ReservedName.objects.create(name='b',order=37) + _ = ReservedName.objects.create(name='a', order=42) + ReservedName.objects.create(name='b', order=37) self.assertQuerysetEqual( ReservedName.objects.all().order_by('order'), ['<ReservedName: b>', '<ReservedName: a>'] |
