def test_output_fallback(self): field = DecimalField(required=True, decimal_places=1, output='no_valid_output') field.set_value(23.4) field.validate() self.assertEqual(field, 23.4) self.assertEqual(field.to_python(), decimal.Decimal('23.4')) self.assertEqual(field.to_native(), 23.4)
def test_string_output(self): field = DecimalField(required=True, decimal_places=2, output=DecimalField.OUTPUT_AS_STRING) field.set_value(23.42) field.validate() self.assertEqual(field, '23.42') self.assertEqual(field.to_python(), decimal.Decimal('23.42')) self.assertEqual(field.to_native(), '23.42')
def test_default(self): field = DecimalField(required=True, default=23.23) field.validate() self.assertEqual(field.to_python(), decimal.Decimal('23.23')) self.assertEqual(field.to_native(), 23.23) field = DecimalField(required=True, default=decimal.Decimal('123.123')) field.validate() self.assertEqual(field.to_python(), decimal.Decimal('123.123')) self.assertEqual(field.to_native(), 123.123)
def test_decimal_places(self): field = DecimalField(required=True, decimal_places=1, max_value=24.5, min_value=22.1) field.set_value(23.4) field.validate() self.assertEqual(field, 23.4) self.assertEqual(field.to_python(), decimal.Decimal('23.4')) self.assertEqual(field.to_native(), 23.4) field = DecimalField(required=True, decimal_places=5) field.set_value(decimal.Decimal('9.4')) field.validate() self.assertEqual(field.to_python(), decimal.Decimal('9.40000')) self.assertEqual(field.to_native(), 9.40000)
def test_set_value_string(self): field = DecimalField(required=True, decimal_places=2) field.set_value('23.23') field.validate() self.assertEqual(field.to_python(), decimal.Decimal('23.23')) self.assertEqual(field.to_native(), 23.23)