Beispiel #1
0
 def test_empty_result_set(self):
     AggregateTestModel.objects.all().delete()
     tests = [
         (ArrayAgg("char_field"), []),
         (ArrayAgg("integer_field"), []),
         (ArrayAgg("boolean_field"), []),
         (BitAnd("integer_field"), None),
         (BitOr("integer_field"), None),
         (BoolAnd("boolean_field"), None),
         (BoolOr("boolean_field"), None),
         (JSONBAgg("integer_field"), []),
         (StringAgg("char_field", delimiter=";"), ""),
     ]
     if connection.features.has_bit_xor:
         tests.append((BitXor("integer_field"), None))
     for aggregation, expected_result in tests:
         with self.subTest(aggregation=aggregation):
             # Empty result with non-execution optimization.
             with self.assertNumQueries(0):
                 values = AggregateTestModel.objects.none().aggregate(
                     aggregation=aggregation,
                 )
                 self.assertEqual(values, {"aggregation": expected_result})
             # Empty result when query must be executed.
             with self.assertNumQueries(1):
                 values = AggregateTestModel.objects.aggregate(
                     aggregation=aggregation,
                 )
                 self.assertEqual(values, {"aggregation": expected_result})
Beispiel #2
0
 def test_default_argument(self):
     AggregateTestModel.objects.all().delete()
     tests = [
         (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=Value('["<empty>"]')), ["<empty>"]),
         (
             StringAgg("char_field", delimiter=";", default=Value("<empty>")),
             "<empty>",
         ),
     ]
     if connection.features.has_bit_xor:
         tests.append((BitXor("integer_field", default=0), 0))
     for aggregation, expected_result in tests:
         with self.subTest(aggregation=aggregation):
             # Empty result with non-execution optimization.
             with self.assertNumQueries(0):
                 values = AggregateTestModel.objects.none().aggregate(
                     aggregation=aggregation,
                 )
                 self.assertEqual(values, {"aggregation": expected_result})
             # Empty result when query must be executed.
             with self.assertNumQueries(1):
                 values = AggregateTestModel.objects.aggregate(
                     aggregation=aggregation,
                 )
                 self.assertEqual(values, {"aggregation": expected_result})
Beispiel #3
0
 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})
Beispiel #4
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})
Beispiel #5
0
 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})