Added DatabaseFeatures.rounds_to_even.

This feature flag useful with MongoDB: "Rounding to the nearest even
value supports more even distribution of rounded data than always
rounding up or down."
This commit is contained in:
Tim Graham 2024-12-18 06:50:48 -05:00 committed by GitHub
parent 20f9f61805
commit bb6114ce50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 2 deletions

View File

@ -382,6 +382,9 @@ class BaseDatabaseFeatures:
# SQL to create a model instance using the database defaults.
insert_test_table_with_defaults = None
# Does the Round() database function round to even?
rounds_to_even = False
# A set of dotted paths to tests in Django's test suite that are expected
# to fail on this database.
django_test_expected_failures = set()

View File

@ -52,7 +52,8 @@ class CastTests(TestCase):
),
).get()
self.assertEqual(float_obj.cast_f1_decimal, decimal.Decimal("-1.93"))
self.assertEqual(float_obj.cast_f2_decimal, decimal.Decimal("3.5"))
expected = "3.4" if connection.features.rounds_to_even else "3.5"
self.assertEqual(float_obj.cast_f2_decimal, decimal.Decimal(expected))
author_obj = Author.objects.annotate(
cast_alias_decimal=Cast(
"alias", models.DecimalField(max_digits=8, decimal_places=2)

View File

@ -112,7 +112,8 @@ class RoundTests(TestCase):
IntegerModel.objects.create(normal=365)
obj = IntegerModel.objects.annotate(normal_round=Round("normal", -1)).first()
self.assertIsInstance(obj.normal_round, int)
self.assertEqual(obj.normal_round, 370)
expected = 360 if connection.features.rounds_to_even else 370
self.assertEqual(obj.normal_round, expected)
def test_transform(self):
with register_lookup(DecimalField, Round):