mirror of
https://github.com/django/django.git
synced 2025-11-25 21:22:14 +00:00
Refs #28643 -- Added math database functions.
Thanks Nick Pope for much review.
This commit is contained in:
parent
48aeca44d8
commit
a0b19a0f5b
30 changed files with 1644 additions and 8 deletions
33
tests/db_functions/math/test_atan2.py
Normal file
33
tests/db_functions/math/test_atan2.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import math
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db.models.functions import ATan2
|
||||
from django.test import TestCase
|
||||
|
||||
from ..models import DecimalModel, FloatModel, IntegerModel
|
||||
|
||||
|
||||
class ATan2Tests(TestCase):
|
||||
|
||||
def test_decimal(self):
|
||||
DecimalModel.objects.create(n1=Decimal('-9.9'), n2=Decimal('4.6'))
|
||||
obj = DecimalModel.objects.annotate(n_atan2=ATan2('n1', 'n2')).first()
|
||||
self.assertIsInstance(obj.n_atan2, Decimal)
|
||||
self.assertAlmostEqual(obj.n_atan2, Decimal(math.atan2(obj.n1, obj.n2)))
|
||||
|
||||
def test_float(self):
|
||||
FloatModel.objects.create(f1=-25, f2=0.33)
|
||||
obj = FloatModel.objects.annotate(f_atan2=ATan2('f1', 'f2')).first()
|
||||
self.assertIsInstance(obj.f_atan2, float)
|
||||
self.assertAlmostEqual(obj.f_atan2, math.atan2(obj.f1, obj.f2))
|
||||
|
||||
def test_integer(self):
|
||||
IntegerModel.objects.create(small=0, normal=1, big=10)
|
||||
obj = IntegerModel.objects.annotate(
|
||||
atan2_sn=ATan2('small', 'normal'),
|
||||
atan2_nb=ATan2('normal', 'big'),
|
||||
).first()
|
||||
self.assertIsInstance(obj.atan2_sn, float)
|
||||
self.assertIsInstance(obj.atan2_nb, float)
|
||||
self.assertAlmostEqual(obj.atan2_sn, math.atan2(obj.small, obj.normal))
|
||||
self.assertAlmostEqual(obj.atan2_nb, math.atan2(obj.normal, obj.big))
|
||||
Loading…
Add table
Add a link
Reference in a new issue