def test_number_type_with_localized_settings(self): _type = types.NumberType(self.field) for value in [ '10,000.00', '10,000,000.23', '10.23', '1,000', '100%', '1000‰' ]: self.assertTrue(_type.cast(value)) self.field['groupChar'] = '#' _type = types.NumberType(self.field) for value in ['10#000.00', '10#000#000.23', '10.23', '1#000']: self.assertTrue(_type.cast(value)) self.field['decimalChar'] = '@' _type = types.NumberType(self.field) for value in ['10#000@00', '10#000#000@23', '10@23', '1#000']: self.assertTrue(_type.cast(value))
def test_constraints_pattern_valid_value(self): '''value is valid for pattern''' value = '7.123' field = self._make_default_field(type='number', constraints={"pattern": "7.[0-9]{3}"}) _type = types.NumberType(field) self.assertEqual(_type.cast(value), decimal.Decimal(value))
def test_constraints_enum_valid_value(self): '''value is in enum array''' value = 5 field = self._make_default_field( type='number', constraints={'enum': ["1.0", "3.0", "5.0"]}) _type = types.NumberType(field) self.assertEqual(_type.cast(value), value)
def test_number_type_with_currency_format_false(self): value1 = '10,000a.00' value2 = '10+000.00' value3 = '$10:000.00' self.field['format'] = 'currency' _type = types.NumberType(self.field) self.assertRaises(exceptions.InvalidCastError, _type.cast, value1) self.assertRaises(exceptions.InvalidCastError, _type.cast, value2) self.assertRaises(exceptions.InvalidCastError, _type.cast, value3)
def test_number_type_with_currency_format_true(self): self.field['format'] = 'currency' _type = types.NumberType(self.field) for value in [ '10,000.00', '10,000,000.00', '$10000.00', ' 10,000.00 €', ]: self.assertTrue(_type.cast(value)) self.field['decimalChar'] = ',' self.field['groupChar'] = ' ' _type = types.NumberType(self.field) for value in [ '10 000,00', '10 000 000,00', '10000,00 ₪', ' 10 000,00 £', ]: self.assertTrue(_type.cast(value))
def test_constraints_pattern_invalid_value(self): '''value is invalid for pattern''' value = '7.12' field = self._make_default_field(type='number', constraints={"pattern": "7.[0-9]{3}"}) _type = types.NumberType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must match the pattern")
def test_constraints_enum_invalid_value(self): '''value is not in enum array''' value = 2 field = self._make_default_field( type='number', constraints={'enum': ["1.0", "3.0", "5.0"]}) _type = types.NumberType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must be in the enum array")
def test_number_type_with_already_cast_value(self): for value in [1, 1.0, Decimal(1)]: for format in ['default', 'currency']: self.field['format'] = format _type = types.NumberType(self.field) self.assertEqual(_type.cast(value), value)
def test_number_type_simple_false(self): value = 'string' _type = types.NumberType(self.field) self.assertRaises(exceptions.InvalidCastError, _type.cast, value)
def test_number_type_simple_true(self): value = '10.00' _type = types.NumberType(self.field) self.assertEquals(_type.cast(value), Decimal(value))