mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-24 23:19:47 +03:00
Added Unit test for float field overflow error catch
This commit is contained in:
parent
9aee257861
commit
cc25b099d3
|
@ -919,7 +919,8 @@ class FloatField(Field):
|
||||||
'invalid': _('A valid number is required.'),
|
'invalid': _('A valid number is required.'),
|
||||||
'max_value': _('Ensure this value is less than or equal to {max_value}.'),
|
'max_value': _('Ensure this value is less than or equal to {max_value}.'),
|
||||||
'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
|
'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
|
||||||
'max_string_length': _('String value too large.')
|
'max_string_length': _('String value too large.'),
|
||||||
|
'overflow': _('int too large to convert to float')
|
||||||
}
|
}
|
||||||
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
|
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
|
||||||
|
|
||||||
|
@ -943,8 +944,10 @@ class FloatField(Field):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return float(data)
|
return float(data)
|
||||||
except (TypeError, ValueError, OverflowError):
|
except (TypeError, ValueError):
|
||||||
self.fail('invalid')
|
self.fail('invalid')
|
||||||
|
except OverflowError:
|
||||||
|
self.fail('overflow')
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
return float(value)
|
return float(value)
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import math
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
import re
|
import re
|
||||||
import uuid
|
import uuid
|
||||||
from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
||||||
|
@ -13,6 +15,7 @@ from django.utils.timezone import activate, deactivate, override
|
||||||
|
|
||||||
import rest_framework
|
import rest_framework
|
||||||
from rest_framework import exceptions, serializers
|
from rest_framework import exceptions, serializers
|
||||||
|
from rest_framework.exceptions import ValidationError
|
||||||
from rest_framework.fields import (
|
from rest_framework.fields import (
|
||||||
BuiltinSignatureError, DjangoImageField, is_simple_callable
|
BuiltinSignatureError, DjangoImageField, is_simple_callable
|
||||||
)
|
)
|
||||||
|
@ -1072,6 +1075,14 @@ class TestMinMaxFloatField(FieldValues):
|
||||||
field = serializers.FloatField(min_value=1, max_value=3)
|
field = serializers.FloatField(min_value=1, max_value=3)
|
||||||
|
|
||||||
|
|
||||||
|
class TestFloatFieldOverFlowError(TestCase):
|
||||||
|
def test_overflow_error_float_field(self):
|
||||||
|
field = serializers.FloatField()
|
||||||
|
with pytest.raises(serializers.ValidationError) as exec_info:
|
||||||
|
field.to_internal_value(data=math.factorial(171))
|
||||||
|
assert "int too large to convert to float" in str(exec_info.value.detail)
|
||||||
|
|
||||||
|
|
||||||
class TestDecimalField(FieldValues):
|
class TestDecimalField(FieldValues):
|
||||||
"""
|
"""
|
||||||
Valid and invalid values for `DecimalField`.
|
Valid and invalid values for `DecimalField`.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user