Add test for DecimalField(precision=0) and raise exception if negative/invalid values are provided

This commit is contained in:
Bastien Gerard 2021-08-07 22:38:27 +02:00
parent f6d864b6d1
commit 0af96b1323
4 changed files with 30 additions and 2 deletions

View File

@ -261,3 +261,4 @@ that much better:
* Felix Schultheiß (https://github.com/felix-smashdocs) * Felix Schultheiß (https://github.com/felix-smashdocs)
* Jan Stein (https://github.com/janste63) * Jan Stein (https://github.com/janste63)
* Timothé Perez (https://github.com/AchilleAsh) * Timothé Perez (https://github.com/AchilleAsh)
* oleksandr-l5 (https://github.com/oleksandr-l5)

View File

@ -9,6 +9,7 @@ Development
- (Fill this out as you fix issues and develop your features). - (Fill this out as you fix issues and develop your features).
- EnumField improvements: now `choices` limits the values of an enum to allow - EnumField improvements: now `choices` limits the values of an enum to allow
- Fix deepcopy of EmbeddedDocument #2202 - Fix deepcopy of EmbeddedDocument #2202
- Fix error when using precision=0 with DecimalField #2535
Changes in 0.23.1 Changes in 0.23.1
=========== ===========

View File

@ -468,6 +468,10 @@ class DecimalField(BaseField):
self.min_value = min_value self.min_value = min_value
self.max_value = max_value self.max_value = max_value
self.force_string = force_string self.force_string = force_string
if precision < 0 or not isinstance(precision, int):
self.error("precision must be a positive integer")
self.precision = precision self.precision = precision
self.rounding = rounding self.rounding = rounding
@ -483,10 +487,12 @@ class DecimalField(BaseField):
except (TypeError, ValueError, decimal.InvalidOperation): except (TypeError, ValueError, decimal.InvalidOperation):
return value return value
if self.precision > 0: if self.precision > 0:
return value.quantize(decimal.Decimal(".%s" % ("0" * self.precision)), rounding=self.rounding) return value.quantize(
decimal.Decimal(".%s" % ("0" * self.precision)), rounding=self.rounding
)
else: else:
return value.quantize(decimal.Decimal(), rounding=self.rounding) return value.quantize(decimal.Decimal(), rounding=self.rounding)
def to_mongo(self, value): def to_mongo(self, value):
if value is None: if value is None:
return value return value

View File

@ -118,3 +118,23 @@ class TestDecimalField(MongoDBTestCase):
assert 2 == Person.objects(money__gt="7").count() assert 2 == Person.objects(money__gt="7").count()
assert 3 == Person.objects(money__gte="7").count() assert 3 == Person.objects(money__gte="7").count()
def test_precision_0(self):
"""prevent regression of a bug that was raising an exception when using precision=0"""
class TestDoc(Document):
d = DecimalField(precision=0)
TestDoc.drop_collection()
td = TestDoc(d=Decimal("12.00032678131263"))
assert td.d == Decimal("12")
def test_precision_negative_raise(self):
"""prevent regression of a bug that was raising an exception when using precision=0"""
with pytest.raises(
ValidationError, match="precision must be a positive integer"
):
class TestDoc(Document):
dneg = DecimalField(precision=-1)