Debian Codegraph @

tests/db_functions/math/test_tan.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 Tan
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 TanTests(TestCase):
13 def test_null(self):
14 IntegerModel.objects.create()
15 obj = IntegerModel.objects.annotate(null_tan=Tan("normal")).first()
16 self.assertIsNone(obj.null_tan)
17
18 def test_decimal(self):
19 DecimalModel.objects.create(n1=Decimal("-12.9"), n2=Decimal("0.6"))
20 obj = DecimalModel.objects.annotate(n1_tan=Tan("n1"), n2_tan=Tan("n2")).first()
21 self.assertIsInstance(obj.n1_tan, Decimal)
22 self.assertIsInstance(obj.n2_tan, Decimal)
23 self.assertAlmostEqual(obj.n1_tan, Decimal(math.tan(obj.n1)))
24 self.assertAlmostEqual(obj.n2_tan, Decimal(math.tan(obj.n2)))
25
26 def test_float(self):
27 FloatModel.objects.create(f1=-27.5, f2=0.33)
28 obj = FloatModel.objects.annotate(f1_tan=Tan("f1"), f2_tan=Tan("f2")).first()
29 self.assertIsInstance(obj.f1_tan, float)
30 self.assertIsInstance(obj.f2_tan, float)
31 self.assertAlmostEqual(obj.f1_tan, math.tan(obj.f1))
32 self.assertAlmostEqual(obj.f2_tan, math.tan(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_tan=Tan("small"),
38 normal_tan=Tan("normal"),
39 big_tan=Tan("big"),
40 ).first()
41 self.assertIsInstance(obj.small_tan, float)
42 self.assertIsInstance(obj.normal_tan, float)
43 self.assertIsInstance(obj.big_tan, float)
44 self.assertAlmostEqual(obj.small_tan, math.tan(obj.small))
45 self.assertAlmostEqual(obj.normal_tan, math.tan(obj.normal))
46 self.assertAlmostEqual(obj.big_tan, math.tan(obj.big))
47
48 def test_transform(self):
49 with register_lookup(DecimalField, Tan):
50 DecimalModel.objects.create(n1=Decimal("0.0"), n2=Decimal("0"))
51 DecimalModel.objects.create(n1=Decimal("12.0"), n2=Decimal("0"))
52 obj = DecimalModel.objects.filter(n1__tan__lt=0).get()
53 self.assertEqual(obj.n1, Decimal("12.0"))