diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2026-04-18 08:53:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-18 08:53:21 +0200 |
| commit | ed79c5959add54b6e8ea589ec601e0d2e801517e (patch) | |
| tree | 0d00f241bea6de88203d1314c7de92cb262b3a3f /tests/postgres_tests | |
| parent | d687d412a9abd9c80e31945f16ce32c020512394 (diff) | |
Fixed #37028 -- Added BitAnd(), BitOr(), and BitXor() aggregates.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_aggregates.py | 83 |
1 files changed, 19 insertions, 64 deletions
diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index 4355eb2f98..bd57b8e338 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -21,14 +21,14 @@ from . import PostgreSQLTestCase from .models import AggregateTestModel, HotelReservation, Room, StatTestModel try: + from django.contrib.postgres.aggregates import BitAnd # RemovedInDjango70Warning + from django.contrib.postgres.aggregates import BitOr # RemovedInDjango70Warning + from django.contrib.postgres.aggregates import BitXor # RemovedInDjango70Warning from django.contrib.postgres.aggregates import ( StringAgg, # RemovedInDjango70Warning. ) from django.contrib.postgres.aggregates import ( ArrayAgg, - BitAnd, - BitOr, - BitXor, BoolAnd, BoolOr, Corr, @@ -91,12 +91,9 @@ class TestGeneralAggregate(PostgreSQLTestCase): ArrayAgg("char_field"), ArrayAgg("integer_field"), ArrayAgg("boolean_field"), - BitAnd("integer_field"), - BitOr("integer_field"), BoolAnd("boolean_field"), BoolOr("boolean_field"), JSONBAgg("integer_field"), - BitXor("integer_field"), ] for aggregation in tests: with self.subTest(aggregation=aggregation): @@ -119,8 +116,6 @@ class TestGeneralAggregate(PostgreSQLTestCase): (ArrayAgg("char_field", default=["<empty>"]), ["<empty>"]), (ArrayAgg("integer_field", default=[0]), [0]), (ArrayAgg("boolean_field", default=[False]), [False]), - (BitAnd("integer_field", default=0), 0), - (BitOr("integer_field", default=0), 0), (BoolAnd("boolean_field", default=False), False), (BoolOr("boolean_field", default=False), False), (JSONBAgg("integer_field", default=["<empty>"]), ["<empty>"]), @@ -128,7 +123,6 @@ class TestGeneralAggregate(PostgreSQLTestCase): JSONBAgg("integer_field", default=Value(["<empty>"], JSONField())), ["<empty>"], ), - (BitXor("integer_field", default=0), 0), ] for aggregation, expected_result in tests: with self.subTest(aggregation=aggregation): @@ -334,61 +328,6 @@ class TestGeneralAggregate(PostgreSQLTestCase): ) ) - def test_bit_and_general(self): - values = AggregateTestModel.objects.filter(integer_field__in=[0, 1]).aggregate( - bitand=BitAnd("integer_field") - ) - self.assertEqual(values, {"bitand": 0}) - - def test_bit_and_on_only_true_values(self): - values = AggregateTestModel.objects.filter(integer_field=1).aggregate( - bitand=BitAnd("integer_field") - ) - self.assertEqual(values, {"bitand": 1}) - - def test_bit_and_on_only_false_values(self): - values = AggregateTestModel.objects.filter(integer_field=0).aggregate( - bitand=BitAnd("integer_field") - ) - self.assertEqual(values, {"bitand": 0}) - - def test_bit_or_general(self): - values = AggregateTestModel.objects.filter(integer_field__in=[0, 1]).aggregate( - bitor=BitOr("integer_field") - ) - self.assertEqual(values, {"bitor": 1}) - - def test_bit_or_on_only_true_values(self): - values = AggregateTestModel.objects.filter(integer_field=1).aggregate( - bitor=BitOr("integer_field") - ) - self.assertEqual(values, {"bitor": 1}) - - def test_bit_or_on_only_false_values(self): - values = AggregateTestModel.objects.filter(integer_field=0).aggregate( - bitor=BitOr("integer_field") - ) - self.assertEqual(values, {"bitor": 0}) - - def test_bit_xor_general(self): - AggregateTestModel.objects.create(integer_field=3) - values = AggregateTestModel.objects.filter( - integer_field__in=[1, 3], - ).aggregate(bitxor=BitXor("integer_field")) - self.assertEqual(values, {"bitxor": 2}) - - def test_bit_xor_on_only_true_values(self): - values = AggregateTestModel.objects.filter( - integer_field=1, - ).aggregate(bitxor=BitXor("integer_field")) - self.assertEqual(values, {"bitxor": 1}) - - def test_bit_xor_on_only_false_values(self): - values = AggregateTestModel.objects.filter( - integer_field=0, - ).aggregate(bitxor=BitXor("integer_field")) - self.assertEqual(values, {"bitxor": 0}) - def test_bool_and_general(self): values = AggregateTestModel.objects.aggregate(booland=BoolAnd("boolean_field")) self.assertEqual(values, {"booland": False}) @@ -651,6 +590,22 @@ class TestGeneralAggregate(PostgreSQLTestCase): self.assertEqual(values, {"stringagg": "Foo1'Foo2'Foo4'Foo3"}) self.assertEqual(ctx.filename, __file__) + def test_bit_agg_deprecation(self): + for BitAggregate in [BitAnd, BitOr, BitXor]: + msg = ( + f"The PostgreSQL-specific {BitAggregate.__name__} function is " + f"deprecated. Use django.db.models.aggregates.{BitAggregate.__name__} " + "instead." + ) + with ( + self.subTest(BitAggregate.__name__), + self.assertWarnsMessage(RemovedInDjango70Warning, msg) as ctx, + ): + AggregateTestModel.objects.aggregate( + bitagg=BitAggregate("integer_field") + ) + self.assertEqual(ctx.filename, __file__) + class TestAggregateDistinct(PostgreSQLTestCase): @classmethod |
