1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
import math
from decimal import Decimal
from unittest import mock
from django.core import validators
from django.core.exceptions import ValidationError
from django.db import connection, models
from django.db.models import Max
from django.test import TestCase
from .models import BigD, Foo
class DecimalFieldTests(TestCase):
def test_to_python(self):
f = models.DecimalField(max_digits=4, decimal_places=2)
self.assertEqual(f.to_python(3), Decimal("3"))
self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
# to_python() converts floats and honors max_digits.
self.assertEqual(f.to_python(3.1415926535897), Decimal("3.142"))
self.assertEqual(f.to_python(2.4), Decimal("2.400"))
# Uses default rounding of ROUND_HALF_EVEN.
self.assertEqual(f.to_python(2.0625), Decimal("2.062"))
self.assertEqual(f.to_python(2.1875), Decimal("2.188"))
def test_invalid_value(self):
field = models.DecimalField(max_digits=4, decimal_places=2)
msg = "“%s” value must be a decimal number."
tests = [
(),
[],
{},
set(),
object(),
complex(),
"non-numeric string",
b"non-numeric byte-string",
]
for value in tests:
with self.subTest(value):
with self.assertRaisesMessage(ValidationError, msg % (value,)):
field.clean(value, None)
def test_default(self):
f = models.DecimalField(default=Decimal("0.00"))
self.assertEqual(f.get_default(), Decimal("0.00"))
def test_get_prep_value(self):
f = models.DecimalField(max_digits=5, decimal_places=1)
self.assertIsNone(f.get_prep_value(None))
self.assertEqual(f.get_prep_value("2.4"), Decimal("2.4"))
def test_get_db_prep_value(self):
"""
DecimalField.get_db_prep_value() must call
DatabaseOperations.adapt_decimalfield_value().
"""
f = models.DecimalField(max_digits=5, decimal_places=1)
# None of the built-in database backends implement
# adapt_decimalfield_value(), so this must be confirmed with mocking.
with mock.patch.object(
connection.ops.__class__, "adapt_decimalfield_value"
) as adapt_decimalfield_value:
f.get_db_prep_value("2.4", connection)
adapt_decimalfield_value.assert_called_with(Decimal("2.4"), 5, 1)
def test_filter_with_strings(self):
"""
Should be able to filter decimal fields using strings (#8023).
"""
foo = Foo.objects.create(a="abc", d=Decimal("12.34"))
self.assertEqual(list(Foo.objects.filter(d="12.34")), [foo])
def test_save_without_float_conversion(self):
"""
Ensure decimals don't go through a corrupting float conversion during
save (#5079).
"""
bd = BigD(d="12.9")
bd.save()
bd = BigD.objects.get(pk=bd.pk)
self.assertEqual(bd.d, Decimal("12.9"))
def test_save_nan_invalid(self):
msg = "“nan” value must be a decimal number."
for value in [float("nan"), math.nan, "nan"]:
with self.subTest(value), self.assertRaisesMessage(ValidationError, msg):
BigD.objects.create(d=value)
def test_save_inf_invalid(self):
msg = "“inf” value must be a decimal number."
for value in [float("inf"), math.inf, "inf"]:
with self.subTest(value), self.assertRaisesMessage(ValidationError, msg):
BigD.objects.create(d=value)
msg = "“-inf” value must be a decimal number."
for value in [float("-inf"), -math.inf, "-inf"]:
with self.subTest(value), self.assertRaisesMessage(ValidationError, msg):
BigD.objects.create(d=value)
def test_fetch_from_db_without_float_rounding(self):
big_decimal = BigD.objects.create(d=Decimal(".100000000000000000000000000005"))
big_decimal.refresh_from_db()
self.assertEqual(big_decimal.d, Decimal(".100000000000000000000000000005"))
def test_lookup_really_big_value(self):
"""
Really big values can be used in a filter statement.
"""
# This should not crash.
self.assertSequenceEqual(Foo.objects.filter(d__gte=100000000000), [])
def test_lookup_decimal_larger_than_max_digits(self):
self.assertSequenceEqual(Foo.objects.filter(d__lte=Decimal("123456")), [])
def test_max_digits_validation(self):
field = models.DecimalField(max_digits=2)
expected_message = validators.DecimalValidator.messages["max_digits"] % {
"max": 2
}
with self.assertRaisesMessage(ValidationError, expected_message):
field.clean(100, None)
def test_max_decimal_places_validation(self):
field = models.DecimalField(decimal_places=1)
expected_message = validators.DecimalValidator.messages[
"max_decimal_places"
] % {"max": 1}
with self.assertRaisesMessage(ValidationError, expected_message):
field.clean(Decimal("0.99"), None)
def test_max_whole_digits_validation(self):
field = models.DecimalField(max_digits=3, decimal_places=1)
expected_message = validators.DecimalValidator.messages["max_whole_digits"] % {
"max": 2
}
with self.assertRaisesMessage(ValidationError, expected_message):
field.clean(Decimal("999"), None)
def test_roundtrip_with_trailing_zeros(self):
"""Trailing zeros in the fractional part aren't truncated."""
obj = Foo.objects.create(a="bar", d=Decimal("8.320"))
obj.refresh_from_db()
self.assertEqual(obj.d.compare_total(Decimal("8.320")), Decimal("0"))
def test_large_integer_precision(self):
large_int_val = Decimal("9999999999999999")
obj = BigD.objects.create(large_int=large_int_val, d=Decimal("0"))
obj.refresh_from_db()
self.assertEqual(obj.large_int, large_int_val)
def test_large_integer_precision_aggregation(self):
large_int_val = Decimal("9999999999999999")
BigD.objects.create(large_int=large_int_val, d=Decimal("0"))
result = BigD.objects.aggregate(max_val=Max("large_int"))
self.assertEqual(result["max_val"], large_int_val)
def test_roundtrip_integer_with_trailing_zeros(self):
obj = Foo.objects.create(a="bar", d=Decimal("8"))
obj.refresh_from_db()
self.assertEqual(obj.d.compare_total(Decimal("8.000")), Decimal("0"))
|