1. import math
    
  2. from decimal import Decimal
    
  3. 
    
  4. from django.db.models import DecimalField
    
  5. from django.db.models.functions import Cot
    
  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 CotTests(TestCase):
    
  13.     def test_null(self):
    
  14.         IntegerModel.objects.create()
    
  15.         obj = IntegerModel.objects.annotate(null_cot=Cot("normal")).first()
    
  16.         self.assertIsNone(obj.null_cot)
    
  17. 
    
  18.     def test_decimal(self):
    
  19.         DecimalModel.objects.create(n1=Decimal("-12.9"), n2=Decimal("0.6"))
    
  20.         obj = DecimalModel.objects.annotate(n1_cot=Cot("n1"), n2_cot=Cot("n2")).first()
    
  21.         self.assertIsInstance(obj.n1_cot, Decimal)
    
  22.         self.assertIsInstance(obj.n2_cot, Decimal)
    
  23.         self.assertAlmostEqual(obj.n1_cot, Decimal(1 / math.tan(obj.n1)))
    
  24.         self.assertAlmostEqual(obj.n2_cot, Decimal(1 / 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_cot=Cot("f1"), f2_cot=Cot("f2")).first()
    
  29.         self.assertIsInstance(obj.f1_cot, float)
    
  30.         self.assertIsInstance(obj.f2_cot, float)
    
  31.         self.assertAlmostEqual(obj.f1_cot, 1 / math.tan(obj.f1))
    
  32.         self.assertAlmostEqual(obj.f2_cot, 1 / math.tan(obj.f2))
    
  33. 
    
  34.     def test_integer(self):
    
  35.         IntegerModel.objects.create(small=-5, normal=15, big=-1)
    
  36.         obj = IntegerModel.objects.annotate(
    
  37.             small_cot=Cot("small"),
    
  38.             normal_cot=Cot("normal"),
    
  39.             big_cot=Cot("big"),
    
  40.         ).first()
    
  41.         self.assertIsInstance(obj.small_cot, float)
    
  42.         self.assertIsInstance(obj.normal_cot, float)
    
  43.         self.assertIsInstance(obj.big_cot, float)
    
  44.         self.assertAlmostEqual(obj.small_cot, 1 / math.tan(obj.small))
    
  45.         self.assertAlmostEqual(obj.normal_cot, 1 / math.tan(obj.normal))
    
  46.         self.assertAlmostEqual(obj.big_cot, 1 / math.tan(obj.big))
    
  47. 
    
  48.     def test_transform(self):
    
  49.         with register_lookup(DecimalField, Cot):
    
  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__cot__gt=0).get()
    
  53.             self.assertEqual(obj.n1, Decimal("1.0"))