| 1 |
import math |
| 2 |
from decimal import Decimal |
| 3 |
|
| 4 |
from django.db.models.functions import ATan2 |
| 5 |
from django.test import TestCase |
| 6 |
|
| 7 |
from ..models import DecimalModel, FloatModel, IntegerModel |
| 8 |
|
| 9 |
|
| 10 |
class ATan2Tests(TestCase): |
| 11 |
def test_null(self): |
| 12 |
IntegerModel.objects.create(big=100) |
| 13 |
obj = IntegerModel.objects.annotate( |
| 14 |
null_atan2_sn=ATan2("small", "normal"), |
| 15 |
null_atan2_nb=ATan2("normal", "big"), |
| 16 |
null_atan2_bn=ATan2("big", "normal"), |
| 17 |
).first() |
| 18 |
self.assertIsNone(obj.null_atan2_sn) |
| 19 |
self.assertIsNone(obj.null_atan2_nb) |
| 20 |
self.assertIsNone(obj.null_atan2_bn) |
| 21 |
|
| 22 |
def test_decimal(self): |
| 23 |
DecimalModel.objects.create(n1=Decimal("-9.9"), n2=Decimal("4.6")) |
| 24 |
obj = DecimalModel.objects.annotate(n_atan2=ATan2("n1", "n2")).first() |
| 25 |
self.assertIsInstance(obj.n_atan2, Decimal) |
| 26 |
self.assertAlmostEqual(obj.n_atan2, Decimal(math.atan2(obj.n1, obj.n2))) |
| 27 |
|
| 28 |
def test_float(self): |
| 29 |
FloatModel.objects.create(f1=-25, f2=0.33) |
| 30 |
obj = FloatModel.objects.annotate(f_atan2=ATan2("f1", "f2")).first() |
| 31 |
self.assertIsInstance(obj.f_atan2, float) |
| 32 |
self.assertAlmostEqual(obj.f_atan2, math.atan2(obj.f1, obj.f2)) |
| 33 |
|
| 34 |
def test_integer(self): |
| 35 |
IntegerModel.objects.create(small=0, normal=1, big=10) |
| 36 |
obj = IntegerModel.objects.annotate( |
| 37 |
atan2_sn=ATan2("small", "normal"), |
| 38 |
atan2_nb=ATan2("normal", "big"), |
| 39 |
).first() |
| 40 |
self.assertIsInstance(obj.atan2_sn, float) |
| 41 |
self.assertIsInstance(obj.atan2_nb, float) |
| 42 |
self.assertAlmostEqual(obj.atan2_sn, math.atan2(obj.small, obj.normal)) |
| 43 |
self.assertAlmostEqual(obj.atan2_nb, math.atan2(obj.normal, obj.big)) |