1. import unittest
    
  2. from decimal import Decimal
    
  3. 
    
  4. from django.db import connection
    
  5. from django.db.models import DecimalField
    
  6. from django.db.models.functions import Pi, Round
    
  7. from django.test import TestCase
    
  8. from django.test.utils import register_lookup
    
  9. 
    
  10. from ..models import DecimalModel, FloatModel, IntegerModel
    
  11. 
    
  12. 
    
  13. class RoundTests(TestCase):
    
  14.     def test_null(self):
    
  15.         IntegerModel.objects.create()
    
  16.         obj = IntegerModel.objects.annotate(null_round=Round("normal")).first()
    
  17.         self.assertIsNone(obj.null_round)
    
  18. 
    
  19.     def test_null_with_precision(self):
    
  20.         IntegerModel.objects.create()
    
  21.         obj = IntegerModel.objects.annotate(null_round=Round("normal", 5)).first()
    
  22.         self.assertIsNone(obj.null_round)
    
  23. 
    
  24.     def test_null_with_negative_precision(self):
    
  25.         IntegerModel.objects.create()
    
  26.         obj = IntegerModel.objects.annotate(null_round=Round("normal", -1)).first()
    
  27.         self.assertIsNone(obj.null_round)
    
  28. 
    
  29.     def test_decimal(self):
    
  30.         DecimalModel.objects.create(n1=Decimal("-12.9"), n2=Decimal("0.6"))
    
  31.         obj = DecimalModel.objects.annotate(
    
  32.             n1_round=Round("n1"), n2_round=Round("n2")
    
  33.         ).first()
    
  34.         self.assertIsInstance(obj.n1_round, Decimal)
    
  35.         self.assertIsInstance(obj.n2_round, Decimal)
    
  36.         self.assertAlmostEqual(obj.n1_round, obj.n1, places=0)
    
  37.         self.assertAlmostEqual(obj.n2_round, obj.n2, places=0)
    
  38. 
    
  39.     def test_decimal_with_precision(self):
    
  40.         DecimalModel.objects.create(n1=Decimal("-5.75"), n2=Pi())
    
  41.         obj = DecimalModel.objects.annotate(
    
  42.             n1_round=Round("n1", 1),
    
  43.             n2_round=Round("n2", 5),
    
  44.         ).first()
    
  45.         self.assertIsInstance(obj.n1_round, Decimal)
    
  46.         self.assertIsInstance(obj.n2_round, Decimal)
    
  47.         self.assertAlmostEqual(obj.n1_round, obj.n1, places=1)
    
  48.         self.assertAlmostEqual(obj.n2_round, obj.n2, places=5)
    
  49. 
    
  50.     def test_decimal_with_negative_precision(self):
    
  51.         DecimalModel.objects.create(n1=Decimal("365.25"))
    
  52.         obj = DecimalModel.objects.annotate(n1_round=Round("n1", -1)).first()
    
  53.         self.assertIsInstance(obj.n1_round, Decimal)
    
  54.         self.assertEqual(obj.n1_round, 370)
    
  55. 
    
  56.     def test_float(self):
    
  57.         FloatModel.objects.create(f1=-27.55, f2=0.55)
    
  58.         obj = FloatModel.objects.annotate(
    
  59.             f1_round=Round("f1"), f2_round=Round("f2")
    
  60.         ).first()
    
  61.         self.assertIsInstance(obj.f1_round, float)
    
  62.         self.assertIsInstance(obj.f2_round, float)
    
  63.         self.assertAlmostEqual(obj.f1_round, obj.f1, places=0)
    
  64.         self.assertAlmostEqual(obj.f2_round, obj.f2, places=0)
    
  65. 
    
  66.     def test_float_with_precision(self):
    
  67.         FloatModel.objects.create(f1=-5.75, f2=Pi())
    
  68.         obj = FloatModel.objects.annotate(
    
  69.             f1_round=Round("f1", 1),
    
  70.             f2_round=Round("f2", 5),
    
  71.         ).first()
    
  72.         self.assertIsInstance(obj.f1_round, float)
    
  73.         self.assertIsInstance(obj.f2_round, float)
    
  74.         self.assertAlmostEqual(obj.f1_round, obj.f1, places=1)
    
  75.         self.assertAlmostEqual(obj.f2_round, obj.f2, places=5)
    
  76. 
    
  77.     def test_float_with_negative_precision(self):
    
  78.         FloatModel.objects.create(f1=365.25)
    
  79.         obj = FloatModel.objects.annotate(f1_round=Round("f1", -1)).first()
    
  80.         self.assertIsInstance(obj.f1_round, float)
    
  81.         self.assertEqual(obj.f1_round, 370)
    
  82. 
    
  83.     def test_integer(self):
    
  84.         IntegerModel.objects.create(small=-20, normal=15, big=-1)
    
  85.         obj = IntegerModel.objects.annotate(
    
  86.             small_round=Round("small"),
    
  87.             normal_round=Round("normal"),
    
  88.             big_round=Round("big"),
    
  89.         ).first()
    
  90.         self.assertIsInstance(obj.small_round, int)
    
  91.         self.assertIsInstance(obj.normal_round, int)
    
  92.         self.assertIsInstance(obj.big_round, int)
    
  93.         self.assertAlmostEqual(obj.small_round, obj.small, places=0)
    
  94.         self.assertAlmostEqual(obj.normal_round, obj.normal, places=0)
    
  95.         self.assertAlmostEqual(obj.big_round, obj.big, places=0)
    
  96. 
    
  97.     def test_integer_with_precision(self):
    
  98.         IntegerModel.objects.create(small=-5, normal=3, big=-100)
    
  99.         obj = IntegerModel.objects.annotate(
    
  100.             small_round=Round("small", 1),
    
  101.             normal_round=Round("normal", 5),
    
  102.             big_round=Round("big", 2),
    
  103.         ).first()
    
  104.         self.assertIsInstance(obj.small_round, int)
    
  105.         self.assertIsInstance(obj.normal_round, int)
    
  106.         self.assertIsInstance(obj.big_round, int)
    
  107.         self.assertAlmostEqual(obj.small_round, obj.small, places=1)
    
  108.         self.assertAlmostEqual(obj.normal_round, obj.normal, places=5)
    
  109.         self.assertAlmostEqual(obj.big_round, obj.big, places=2)
    
  110. 
    
  111.     def test_integer_with_negative_precision(self):
    
  112.         IntegerModel.objects.create(normal=365)
    
  113.         obj = IntegerModel.objects.annotate(normal_round=Round("normal", -1)).first()
    
  114.         self.assertIsInstance(obj.normal_round, int)
    
  115.         self.assertEqual(obj.normal_round, 370)
    
  116. 
    
  117.     def test_transform(self):
    
  118.         with register_lookup(DecimalField, Round):
    
  119.             DecimalModel.objects.create(n1=Decimal("2.0"), n2=Decimal("0"))
    
  120.             DecimalModel.objects.create(n1=Decimal("-1.0"), n2=Decimal("0"))
    
  121.             obj = DecimalModel.objects.filter(n1__round__gt=0).get()
    
  122.             self.assertEqual(obj.n1, Decimal("2.0"))
    
  123. 
    
  124.     @unittest.skipUnless(
    
  125.         connection.vendor == "sqlite",
    
  126.         "SQLite doesn't support negative precision.",
    
  127.     )
    
  128.     def test_unsupported_negative_precision(self):
    
  129.         FloatModel.objects.create(f1=123.45)
    
  130.         msg = "SQLite does not support negative precision."
    
  131.         with self.assertRaisesMessage(ValueError, msg):
    
  132.             FloatModel.objects.annotate(value=Round("f1", -1)).first()