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