diff options
| author | Nick Pope <nick.pope@flightdataservices.com> | 2019-03-20 08:27:34 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-03-22 12:52:39 +0100 |
| commit | d26b2424437dabeeca94d7900b37d2df4410da0c (patch) | |
| tree | cb08383bf787f14b91e82664ebedf84c92748e51 /tests/db_functions | |
| parent | 5935a9aeade517aebdceea989467d2b46c44d96f (diff) | |
Fixed #30271 -- Added the Sign database function.
Diffstat (limited to 'tests/db_functions')
| -rw-r--r-- | tests/db_functions/math/test_sign.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/db_functions/math/test_sign.py b/tests/db_functions/math/test_sign.py new file mode 100644 index 0000000000..0458932cee --- /dev/null +++ b/tests/db_functions/math/test_sign.py @@ -0,0 +1,53 @@ +from decimal import Decimal + +from django.db.models import DecimalField +from django.db.models.functions import Sign +from django.test import TestCase +from django.test.utils import register_lookup + +from ..models import DecimalModel, FloatModel, IntegerModel + + +class SignTests(TestCase): + + def test_null(self): + IntegerModel.objects.create() + obj = IntegerModel.objects.annotate(null_sign=Sign('normal')).first() + self.assertIsNone(obj.null_sign) + + def test_decimal(self): + DecimalModel.objects.create(n1=Decimal('-12.9'), n2=Decimal('0.6')) + obj = DecimalModel.objects.annotate(n1_sign=Sign('n1'), n2_sign=Sign('n2')).first() + self.assertIsInstance(obj.n1_sign, Decimal) + self.assertIsInstance(obj.n2_sign, Decimal) + self.assertEqual(obj.n1_sign, Decimal('-1')) + self.assertEqual(obj.n2_sign, Decimal('1')) + + def test_float(self): + FloatModel.objects.create(f1=-27.5, f2=0.33) + obj = FloatModel.objects.annotate(f1_sign=Sign('f1'), f2_sign=Sign('f2')).first() + self.assertIsInstance(obj.f1_sign, float) + self.assertIsInstance(obj.f2_sign, float) + self.assertEqual(obj.f1_sign, -1.0) + self.assertEqual(obj.f2_sign, 1.0) + + def test_integer(self): + IntegerModel.objects.create(small=-20, normal=0, big=20) + obj = IntegerModel.objects.annotate( + small_sign=Sign('small'), + normal_sign=Sign('normal'), + big_sign=Sign('big'), + ).first() + self.assertIsInstance(obj.small_sign, int) + self.assertIsInstance(obj.normal_sign, int) + self.assertIsInstance(obj.big_sign, int) + self.assertEqual(obj.small_sign, -1) + self.assertEqual(obj.normal_sign, 0) + self.assertEqual(obj.big_sign, 1) + + def test_transform(self): + with register_lookup(DecimalField, Sign): + DecimalModel.objects.create(n1=Decimal('5.4'), n2=Decimal('0')) + DecimalModel.objects.create(n1=Decimal('-0.1'), n2=Decimal('0')) + obj = DecimalModel.objects.filter(n1__sign__lt=0, n2__sign=0).get() + self.assertEqual(obj.n1, Decimal('-0.1')) |
