diff options
| author | Simon Charette <charettes@users.noreply.github.com> | 2019-01-09 17:52:36 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-01-09 17:52:36 -0500 |
| commit | bc05547cd8c1dd511c6b6a6c873a1bc63417b111 (patch) | |
| tree | 2ba1a136e77b21df2524d5558ea0a6f25d5ff03b /tests | |
| parent | 222caab68a2a7345043d0c50161203cb2cfe70eb (diff) | |
Fixed #28658 -- Added DISTINCT handling to the Aggregate class.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/aggregation/tests.py | 4 | ||||
| -rw-r--r-- | tests/aggregation_regress/tests.py | 11 | ||||
| -rw-r--r-- | tests/backends/sqlite/tests.py | 12 | ||||
| -rw-r--r-- | tests/expressions/tests.py | 13 |
4 files changed, 35 insertions, 5 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index a55ccfbfa2..75d2ecb1c5 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -1026,7 +1026,7 @@ class AggregateTestCase(TestCase): # test completely changing how the output is rendered def lower_case_function_override(self, compiler, connection): sql, params = compiler.compile(self.source_expressions[0]) - substitutions = {'function': self.function.lower(), 'expressions': sql} + substitutions = {'function': self.function.lower(), 'expressions': sql, 'distinct': ''} substitutions.update(self.extra) return self.template % substitutions, params setattr(MySum, 'as_' + connection.vendor, lower_case_function_override) @@ -1053,7 +1053,7 @@ class AggregateTestCase(TestCase): # test overriding all parts of the template def be_evil(self, compiler, connection): - substitutions = {'function': 'MAX', 'expressions': '2'} + substitutions = {'function': 'MAX', 'expressions': '2', 'distinct': ''} substitutions.update(self.extra) return self.template % substitutions, () setattr(MySum, 'as_' + connection.vendor, be_evil) diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py index 29b32c4987..2b3948a0b4 100644 --- a/tests/aggregation_regress/tests.py +++ b/tests/aggregation_regress/tests.py @@ -11,6 +11,7 @@ from django.db.models import ( Avg, Case, Count, DecimalField, F, IntegerField, Max, Q, StdDev, Sum, Value, Variance, When, ) +from django.db.models.aggregates import Aggregate from django.test import ( TestCase, ignore_warnings, skipUnlessAnyDBFeature, skipUnlessDBFeature, ) @@ -1496,6 +1497,16 @@ class AggregationTests(TestCase): qs = Author.objects.values_list('age', flat=True).annotate(age_count=Count('age')).filter(age_count__gt=1) self.assertSequenceEqual(qs, [29]) + def test_allow_distinct(self): + class MyAggregate(Aggregate): + pass + with self.assertRaisesMessage(TypeError, 'MyAggregate does not allow distinct'): + MyAggregate('foo', distinct=True) + + class DistinctAggregate(Aggregate): + allow_distinct = True + DistinctAggregate('foo', distinct=True) + class JoinPromotionTests(TestCase): def test_ticket_21150(self): diff --git a/tests/backends/sqlite/tests.py b/tests/backends/sqlite/tests.py index bddaf8620f..c681d39775 100644 --- a/tests/backends/sqlite/tests.py +++ b/tests/backends/sqlite/tests.py @@ -4,6 +4,7 @@ import unittest from django.db import connection, transaction from django.db.models import Avg, StdDev, Sum, Variance +from django.db.models.aggregates import Aggregate from django.db.models.fields import CharField from django.db.utils import NotSupportedError from django.test import ( @@ -34,6 +35,17 @@ class Tests(TestCase): **{'complex': aggregate('last_modified') + aggregate('last_modified')} ) + def test_distinct_aggregation(self): + class DistinctAggregate(Aggregate): + allow_distinct = True + aggregate = DistinctAggregate('first', 'second', distinct=True) + msg = ( + "SQLite doesn't support DISTINCT on aggregate functions accepting " + "multiple arguments." + ) + with self.assertRaisesMessage(NotSupportedError, msg): + connection.ops.check_expression_support(aggregate) + def test_memory_db_test_name(self): """A named in-memory db should be allowed where supported.""" from django.db.backends.sqlite3.base import DatabaseWrapper diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 2ed928915a..ee3676e64a 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1481,18 +1481,22 @@ class ReprTests(SimpleTestCase): def test_aggregates(self): self.assertEqual(repr(Avg('a')), "Avg(F(a))") - self.assertEqual(repr(Count('a')), "Count(F(a), distinct=False)") - self.assertEqual(repr(Count('*')), "Count('*', distinct=False)") + self.assertEqual(repr(Count('a')), "Count(F(a))") + self.assertEqual(repr(Count('*')), "Count('*')") self.assertEqual(repr(Max('a')), "Max(F(a))") self.assertEqual(repr(Min('a')), "Min(F(a))") self.assertEqual(repr(StdDev('a')), "StdDev(F(a), sample=False)") self.assertEqual(repr(Sum('a')), "Sum(F(a))") self.assertEqual(repr(Variance('a', sample=True)), "Variance(F(a), sample=True)") + def test_distinct_aggregates(self): + self.assertEqual(repr(Count('a', distinct=True)), "Count(F(a), distinct=True)") + self.assertEqual(repr(Count('*', distinct=True)), "Count('*', distinct=True)") + def test_filtered_aggregates(self): filter = Q(a=1) self.assertEqual(repr(Avg('a', filter=filter)), "Avg(F(a), filter=(AND: ('a', 1)))") - self.assertEqual(repr(Count('a', filter=filter)), "Count(F(a), distinct=False, filter=(AND: ('a', 1)))") + self.assertEqual(repr(Count('a', filter=filter)), "Count(F(a), filter=(AND: ('a', 1)))") self.assertEqual(repr(Max('a', filter=filter)), "Max(F(a), filter=(AND: ('a', 1)))") self.assertEqual(repr(Min('a', filter=filter)), "Min(F(a), filter=(AND: ('a', 1)))") self.assertEqual(repr(StdDev('a', filter=filter)), "StdDev(F(a), filter=(AND: ('a', 1)), sample=False)") @@ -1501,6 +1505,9 @@ class ReprTests(SimpleTestCase): repr(Variance('a', sample=True, filter=filter)), "Variance(F(a), filter=(AND: ('a', 1)), sample=True)" ) + self.assertEqual( + repr(Count('a', filter=filter, distinct=True)), "Count(F(a), distinct=True, filter=(AND: ('a', 1)))" + ) class CombinableTests(SimpleTestCase): |
