Commit 4a319dda authored by Viacheslav Sukhovieiev's avatar Viacheslav Sukhovieiev
Browse files

Revert precision check changes

parent 42ccea51
import decimal
import json
import operator
......@@ -43,8 +42,6 @@ class DecimalIntegerType(NumberType):
def convert(self, value, context=None):
if not isinstance(value, (int, float)):
raise ValidationError(f'Require int or float type, but {type(value).__name__} found')
if not self.is_precision_valid(value):
raise ValidationError(f"Value should have less than or equal to {self.strict} numbers after decimal point")
return super().convert(value, context)
def to_native(self, value, context=None):
......@@ -52,21 +49,6 @@ class DecimalIntegerType(NumberType):
return round(value * self.get_strict())
return value
def is_precision_valid(self, value):
"""
Check if value has less than `strict` decimal numbers
"""
try:
value = decimal.Decimal(str(value).strip())
except (TypeError, decimal.InvalidOperation):
raise ValidationError(
self.messages['number_coerce'].format(value, self.number_type.lower())
)
_, _, exponent = value.as_tuple()
if exponent <= 0:
return abs(exponent) <= self.strict
return True
def validate_range(self, value, context=None):
"""
validate value greater than min_value and less than max_value
......
......@@ -129,50 +129,6 @@ class TestDecimalIntegerType:
assert isinstance(amount_field, types.DecimalIntegerType)
assert 'exclusiveMinimum' in amount_field.metadata
@pytest.mark.parametrize("value, strict", [
[-655.31, 2],
[0.31, 2],
[5 ** (10*100), 2],
[1000.99999999999996, 2], # float rounding cases
[100.999999999999996, 2], # float rounding cases
[10.9999999999999996, 2], # float rounding cases
[1.99999999999999996, 2], # float rounding cases
[1999999999999999.96, 2], # float rounding cases
[199999999999999.996, 2], # float rounding cases
])
def test_value_precision(self, value, strict):
test_field = types.DecimalIntegerType(strict=strict)
assert test_field.is_precision_valid(value)
@pytest.mark.parametrize("value, strict, expectation", [
(10, 2, does_not_raise()), # default
(10., 2, does_not_raise()),
(10.0, 2, does_not_raise()),
(10.000, 2, does_not_raise()),
(10.1, 2, does_not_raise()),
(10.001, 2, pytest.raises(exceptions.ValidationError)),
(10.009, 2, pytest.raises(exceptions.ValidationError)),
(10.9999999, 2, pytest.raises(exceptions.ValidationError)),
(10.9999999999, 2, pytest.raises(exceptions.ValidationError)),
(10.9999999999995, 2, pytest.raises(exceptions.ValidationError)),
(3.55, 2, does_not_raise()),
(3, 0, does_not_raise()),
(3., 0, pytest.raises(exceptions.ValidationError)),
(3.00000, 0, pytest.raises(exceptions.ValidationError)),
(92.1, 2, does_not_raise()),
(5 ** (10*100), 0, does_not_raise()),
(-(5 ** (10*100)), 0, does_not_raise()),
(-(5 ** (10*100)), 0, does_not_raise()),
(1234575889578.63, 2, does_not_raise()),
(1234575889578.91, 2, does_not_raise()),
(types.DecimalIntegerType._max_int/100, 2, does_not_raise()),
(-types.DecimalIntegerType._max_int/100, 2, does_not_raise()),
])
def test_precision_validation(self, value, strict, expectation):
test_field = types.DecimalIntegerType(strict=strict)
with expectation:
assert test_field.convert(value)
def test_xstrict_swagger_generator(self, make_model_spec):
expected_strict = 1
props = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment