| 1 |
import math |
| 2 |
from decimal import Decimal |
| 3 |
|
| 4 |
from django.db.models import DecimalField |
| 5 |
from django.db.models.functions import Ceil |
| 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 CeilTests(TestCase): |
| 13 |
def test_null(self): |
| 14 |
IntegerModel.objects.create() |
| 15 |
obj = IntegerModel.objects.annotate(null_ceil=Ceil("normal")).first() |
| 16 |
self.assertIsNone(obj.null_ceil) |
| 17 |
|
| 18 |
def test_decimal(self): |
| 19 |
DecimalModel.objects.create(n1=Decimal("12.9"), n2=Decimal("0.6")) |
| 20 |
obj = DecimalModel.objects.annotate( |
| 21 |
n1_ceil=Ceil("n1"), n2_ceil=Ceil("n2") |
| 22 |
).first() |
| 23 |
self.assertIsInstance(obj.n1_ceil, Decimal) |
| 24 |
self.assertIsInstance(obj.n2_ceil, Decimal) |
| 25 |
self.assertEqual(obj.n1_ceil, Decimal(math.ceil(obj.n1))) |
| 26 |
self.assertEqual(obj.n2_ceil, Decimal(math.ceil(obj.n2))) |
| 27 |
|
| 28 |
def test_float(self): |
| 29 |
FloatModel.objects.create(f1=-12.5, f2=21.33) |
| 30 |
obj = FloatModel.objects.annotate( |
| 31 |
f1_ceil=Ceil("f1"), f2_ceil=Ceil("f2") |
| 32 |
).first() |
| 33 |
self.assertIsInstance(obj.f1_ceil, float) |
| 34 |
self.assertIsInstance(obj.f2_ceil, float) |
| 35 |
self.assertEqual(obj.f1_ceil, math.ceil(obj.f1)) |
| 36 |
self.assertEqual(obj.f2_ceil, math.ceil(obj.f2)) |
| 37 |
|
| 38 |
def test_integer(self): |
| 39 |
IntegerModel.objects.create(small=-11, normal=0, big=-100) |
| 40 |
obj = IntegerModel.objects.annotate( |
| 41 |
small_ceil=Ceil("small"), |
| 42 |
normal_ceil=Ceil("normal"), |
| 43 |
big_ceil=Ceil("big"), |
| 44 |
).first() |
| 45 |
self.assertIsInstance(obj.small_ceil, int) |
| 46 |
self.assertIsInstance(obj.normal_ceil, int) |
| 47 |
self.assertIsInstance(obj.big_ceil, int) |
| 48 |
self.assertEqual(obj.small_ceil, math.ceil(obj.small)) |
| 49 |
self.assertEqual(obj.normal_ceil, math.ceil(obj.normal)) |
| 50 |
self.assertEqual(obj.big_ceil, math.ceil(obj.big)) |
| 51 |
|
| 52 |
def test_transform(self): |
| 53 |
with register_lookup(DecimalField, Ceil): |
| 54 |
DecimalModel.objects.create(n1=Decimal("3.12"), n2=Decimal("0")) |
| 55 |
DecimalModel.objects.create(n1=Decimal("1.25"), n2=Decimal("0")) |
| 56 |
obj = DecimalModel.objects.filter(n1__ceil__gt=3).get() |
| 57 |
self.assertEqual(obj.n1, Decimal("3.12")) |