class TestDecimalRangeFieldWithChildAttribute(FieldValues): serializer_class = DecimalRangeSerializerWithChildAttribute field = DecimalRangeField(child=DecimalField(max_digits=5, decimal_places=2)) valid_inputs = [ ({'lower': '1', 'upper': 2., 'bounds': '[)'}, NumericRange(**{'lower': 1., 'upper': 2., 'bounds': '[)'})), ({'lower': 1., 'upper': 2.}, NumericRange(**{'lower': 1, 'upper': 2})), ({'lower': 1}, NumericRange(**{'lower': 1})), ({'upper': 1}, NumericRange(**{'upper': 1})), ({'empty': True}, NumericRange(**{'empty': True})), ({}, NumericRange()), ] invalid_inputs = [ ({'lower': 'a'}, ['A valid number is required.']), ({'upper': '123456'}, ['Ensure that there are no more than 5 digits in total.']), ({'lower': '9.123'}, ['Ensure that there are no more than 2 decimal places.']), ('not a dict', ['Expected a dictionary of items but got type "str".']), ] outputs = [ (NumericRange(**{'lower': '1.1', 'upper': '2'}), {'lower': '1.10', 'upper': '2.00', 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), ]
def test_no_source_on_child(self): with pytest.raises(AssertionError) as exc_info: DecimalRangeField(child=serializers.DecimalField(source='other', max_digits=None, decimal_places=None)) assert str(exc_info.value) == ( "The `source` argument is not meaningful when applied to a `child=` field. " "Remove `source=` from the field declaration." )
class TestDecimalRangeField(FieldValues): serializer_class = DecimalRangeSerializer valid_inputs = [ ({'lower': '1', 'upper': 2., 'bounds': '[)'}, NumericRange(**{'lower': 1., 'upper': 2., 'bounds': '[)'})), ({'lower': 1., 'upper': 2.}, NumericRange(**{'lower': 1, 'upper': 2})), ({'lower': 1}, NumericRange(**{'lower': 1})), ({'upper': 1}, NumericRange(**{'upper': 1})), ({'empty': True}, NumericRange(**{'empty': True})), ({}, NumericRange()), ] invalid_inputs = [ ({'lower': 'a'}, ['A valid number is required.']), ('not a dict', ['Expected a dictionary of items but got type "str".']), ({'lower': 2., 'upper': 1.}, ['The start of the range must not exceed the end of the range.']), ] outputs = [ (NumericRange(**{'lower': '1.1', 'upper': '2'}), {'lower': '1.1', 'upper': '2', 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), ({'lower': Decimal('1.1'), 'upper': "2.3", 'bounds': '[)'}, {'lower': "1.1", 'upper': "2.3", 'bounds': '[)'}), ({'lower': Decimal('1.1'), 'upper': "2.3"}, {'lower': "1.1", 'upper': "2.3", 'bounds': None}), ({'lower': 1}, {'lower': "1", 'upper': None, 'bounds': None}), ({'upper': 1}, {'lower': None, 'upper': "1", 'bounds': None}), ({}, {}), ] field = DecimalRangeField() def test_no_source_on_child(self): with pytest.raises(AssertionError) as exc_info: DecimalRangeField(child=serializers.DecimalField(source='other', max_digits=None, decimal_places=None)) assert str(exc_info.value) == ( "The `source` argument is not meaningful when applied to a `child=` field. " "Remove `source=` from the field declaration." )
class DecimalRangeSerializerWithChildAttribute(serializers.Serializer): range = DecimalRangeField(child=DecimalField(max_digits=5, decimal_places=2))
class DecimalRangeSerializer(serializers.Serializer): range = DecimalRangeField()