2
0
Эх сурвалжийг харах

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."
Tim Graham 3 сар өмнө
parent
commit
bb6114ce50

+ 3 - 0
django/db/backends/base/features.py

@@ -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()

+ 2 - 1
tests/db_functions/comparison/test_cast.py

@@ -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)

+ 2 - 1
tests/db_functions/math/test_round.py

@@ -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):