Added Unit test for float field overflow error catch

This commit is contained in:
reeko234 2022-10-22 21:33:35 +00:00
parent 9aee257861
commit cc25b099d3
2 changed files with 16 additions and 2 deletions

View File

@ -919,7 +919,8 @@ class FloatField(Field):
'invalid': _('A valid number is required.'),
'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}.'),
'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.
@ -943,8 +944,10 @@ class FloatField(Field):
try:
return float(data)
except (TypeError, ValueError, OverflowError):
except (TypeError, ValueError):
self.fail('invalid')
except OverflowError:
self.fail('overflow')
def to_representation(self, value):
return float(value)

View File

@ -1,5 +1,7 @@
import datetime
import math
import os
import random
import re
import uuid
from decimal import ROUND_DOWN, ROUND_UP, Decimal
@ -13,6 +15,7 @@ from django.utils.timezone import activate, deactivate, override
import rest_framework
from rest_framework import exceptions, serializers
from rest_framework.exceptions import ValidationError
from rest_framework.fields import (
BuiltinSignatureError, DjangoImageField, is_simple_callable
)
@ -1072,6 +1075,14 @@ class TestMinMaxFloatField(FieldValues):
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):
"""
Valid and invalid values for `DecimalField`.