Debian Codegraph @

tests/db_functions/math/test_exp.py

raw
1 import math
2 from decimal import Decimal
3
4 from django.db.models import DecimalField
5 from django.db.models.functions import Exp
6 from django.test import TestCase
7 from django.test.utils import register_lookup
8
9 from ..models import DecimalModel, FloatModel, IntegerModel
10
11
12 class ExpTests(TestCase):
13 def test_null(self):
14 IntegerModel.objects.create()
15 obj = IntegerModel.objects.annotate(null_exp=Exp("normal")).first()
16 self.assertIsNone(obj.null_exp)
17
18 def test_decimal(self):
19 DecimalModel.objects.create(n1=Decimal("-12.9"), n2=Decimal("0.6"))
20 obj = DecimalModel.objects.annotate(n1_exp=Exp("n1"), n2_exp=Exp("n2")).first()
21 self.assertIsInstance(obj.n1_exp, Decimal)
22 self.assertIsInstance(obj.n2_exp, Decimal)
23 self.assertAlmostEqual(obj.n1_exp, Decimal(math.exp(obj.n1)))
24 self.assertAlmostEqual(obj.n2_exp, Decimal(math.exp(obj.n2)))
25
26 def test_float(self):
27 FloatModel.objects.create(f1=-27.5, f2=0.33)
28 obj = FloatModel.objects.annotate(f1_exp=Exp("f1"), f2_exp=Exp("f2")).first()
29 self.assertIsInstance(obj.f1_exp, float)
30 self.assertIsInstance(obj.f2_exp, float)
31 self.assertAlmostEqual(obj.f1_exp, math.exp(obj.f1))
32 self.assertAlmostEqual(obj.f2_exp, math.exp(obj.f2))
33
34 def test_integer(self):
35 IntegerModel.objects.create(small=-20, normal=15, big=-1)
36 obj = IntegerModel.objects.annotate(
37 small_exp=Exp("small"),
38 normal_exp=Exp("normal"),
39 big_exp=Exp("big"),
40 ).first()
41 self.assertIsInstance(obj.small_exp, float)
42 self.assertIsInstance(obj.normal_exp, float)
43 self.assertIsInstance(obj.big_exp, float)
44 self.assertAlmostEqual(obj.small_exp, math.exp(obj.small))
45 self.assertAlmostEqual(obj.normal_exp, math.exp(obj.normal))
46 self.assertAlmostEqual(obj.big_exp, math.exp(obj.big))
47
48 def test_transform(self):
49 with register_lookup(DecimalField, Exp):
50 DecimalModel.objects.create(n1=Decimal("12.0"), n2=Decimal("0"))
51 DecimalModel.objects.create(n1=Decimal("-1.0"), n2=Decimal("0"))
52 obj = DecimalModel.objects.filter(n1__exp__gt=10).get()
53 self.assertEqual(obj.n1, Decimal("12.0"))