Example #1
0
    def test_run_validation_raises_validation_error_for_gt_max_value(self):
        instance = fields.DecimalField(max_digits=5,
                                       decimal_places=2,
                                       max_value=90)

        with self.assertRaises(ValidationError):
            instance.run_validation(99)
Example #2
0
    def test_quantize(self):
        instance = fields.DecimalField(max_digits=10, decimal_places=5)

        value = Decimal('12345.0')
        self.assertEqual(instance.quantize(value), value)
Example #3
0
 def test_to_representation_without_coerce_to_string(self):
     instance = fields.DecimalField(max_digits=10,
                                    decimal_places=5,
                                    coerce_to_string=False)
     value = Decimal('12345.0')
     self.assertEqual(instance.to_representation(value), value)
Example #4
0
 def test_to_representation_with_decimal_as_a_string(self):
     instance = fields.DecimalField(max_digits=10, decimal_places=5)
     self.assertEqual(instance.to_representation('12345.0'), '12345.00000')
Example #5
0
    def test_to_internal_value_raises_validation_error_for_infinity(self):
        instance = fields.DecimalField(max_digits=10, decimal_places=5)

        with self.assertRaises(ValidationError):
            instance.to_internal_value(float('inf'))  # positive infinite
            instance.to_internal_value(-float('inf'))  # negative infinite
Example #6
0
    def test_to_internal_value_raises_validation_error_for_NaN(self):
        instance = fields.DecimalField(max_digits=10, decimal_places=5)

        with self.assertRaises(ValidationError):
            instance.to_internal_value('NaN')
Example #7
0
 def test_to_internal_value(self):
     instance = fields.DecimalField(max_digits=10, decimal_places=5)
     self.assertEqual(instance.to_internal_value(12345.0),
                      Decimal('12345.0'))
Example #8
0
    def test_validate_precision_raise_validation_exc_max_whole_digits(self):
        instance = fields.DecimalField(max_digits=9, decimal_places=7)

        with self.assertRaises(ValidationError):
            instance.validate_precision(Decimal('1234500.0'))
Example #9
0
    def test_validate_precision_with_fraction(self):
        instance = fields.DecimalField(max_digits=7, decimal_places=5)

        value = Decimal('0.01234')
        self.assertEqual(instance.validate_precision(value), value)
Example #10
0
    def test_validate_precision_with_digittuple(self):
        instance = fields.DecimalField(max_digits=7, decimal_places=2)

        value = Decimal('12345.0')
        self.assertEqual(instance.validate_precision(value), value)
Example #11
0
    def test_validate_precision_with_exponent(self):
        instance = fields.DecimalField(max_digits=5, decimal_places=0)

        value = Decimal('12345')
        self.assertEqual(instance.validate_precision(value), value)
Example #12
0
 def test_run_validation(self):
     instance = fields.DecimalField(max_digits=5, decimal_places=2)
     self.assertEqual(instance.run_validation(99), 99)
Example #13
0
 def test_init_with_not_defined_max_whole_digits(self):
     instance = fields.DecimalField(max_digits=None, decimal_places=None)
     self.assertIsNone(instance.max_digits)
     self.assertIsNone(instance.decimal_places)
     self.assertIsNone(instance.max_whole_digits)
Example #14
0
 def test_init_default(self):
     instance = fields.DecimalField(max_digits=5, decimal_places=2)
     self.assertEqual(instance.max_digits, 5)
     self.assertEqual(instance.decimal_places, 2)
     self.assertEqual(instance.max_whole_digits, 3)