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

@@ -118,3 +118,23 @@ class TestDecimalField(MongoDBTestCase):
assert 2 == Person.objects(money__gt="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)