def test_to_python_anded_mixed_fraction_string(self):
     field = DecimalFractionField()
     value = '1 and 1/2'
     result = field.to_python(value)
     self.assertEqual(
         Decimal(3 / 2.0).quantize(Decimal('0.000')),
         result.quantize(Decimal('0.000')))
 def test_prepare_value_limit_denominator(self):
     """
     Test `prepare_value()` when the field has been initialized
     with the limit_denominator paramter
     """
     field = DecimalFractionField(limit_denominator=3)
     result = field.prepare_value(Decimal(1 / 3.0))
     self.assertEqual('1/3', result)
 def test_prepare_value_float(self):
     """
     Test that a :class:`float` is properly
     converted to a string fraction
     """
     field = DecimalFractionField()
     result = field.prepare_value(float(.5))
     self.assertEqual('1/2', result)
 def test_prepare_value_decimal(self):
     """
     Test that a :class:`decimal.Decimal` is properly
     converted to a string fraction
     """
     field = DecimalFractionField()
     result = field.prepare_value(Decimal('.5'))
     self.assertEqual('1/2', result)
 def test_round_decimal_value_method(self):
     field = DecimalFractionField(max_digits=5, decimal_places=2)
     self.assertEqual(Decimal('100.01'),
                      field.round_decimal_value(Decimal('100.011')))
     self.assertEqual(Decimal('1.01'),
                      field.round_decimal_value(Decimal('1.011')))
     self.assertEqual(Decimal('10001'),
                      field.round_decimal_value(Decimal('10000.6')))
 def test_prepare_value_int(self):
     """
     Test that a standard int as input is returned
     as a string of that int, so `1` is returned as `'1'`
     """
     field = DecimalFractionField()
     result = field.prepare_value(1)
     self.assertEqual('1', result)
 def test_prepare_value_float(self):
     """
     Test that a :class:`float` is properly
     converted to a string fraction
     """
     field = DecimalFractionField()
     result = field.prepare_value(float(.5))
     self.assertEqual('1/2', result)
 def test_prepare_value_int(self):
     """
     Test that a standard int as input is returned
     as a string of that int, so `1` is returned as `'1'`
     """
     field = DecimalFractionField()
     result = field.prepare_value(1)
     self.assertEqual('1', result)
 def test_prepare_value_limit_denominator(self):
     """
     Test `prepare_value()` when the field has been initialized
     with the limit_denominator paramter
     """
     field = DecimalFractionField(limit_denominator=3)
     result = field.prepare_value(Decimal(1/3.0))
     self.assertEqual('1/3', result)
 def test_prepare_value_decimal(self):
     """
     Test that a :class:`decimal.Decimal` is properly
     converted to a string fraction
     """
     field = DecimalFractionField()
     result = field.prepare_value(Decimal('.5'))
     self.assertEqual('1/2', result)
 def test_to_python_decimal(self):
     """
     Test that whena :class:`decimal.Decimal` is passed to to_python()
     the value is returned as is
     """
     field = DecimalFractionField()
     value = Decimal(.5)
     result = field.to_python(value)
     self.assertEqual(value, result)
 def test_to_python_int(self):
     """
     Test that whena :class:`int` is passed to to_python()
     the value is returned as as :class:`decimal.Decimal`
     """
     field = DecimalFractionField()
     value = 1
     result = field.to_python(value)
     self.assertEqual(Decimal(value), result)
 def test_to_python_int(self):
     """
     Test that whena :class:`int` is passed to to_python()
     the value is returned as as :class:`decimal.Decimal`
     """
     field = DecimalFractionField()
     value = 1
     result = field.to_python(value)
     self.assertEqual(Decimal(value), result)
 def test_to_python_decimal(self):
     """
     Test that whena :class:`decimal.Decimal` is passed to to_python()
     the value is returned as is
     """
     field = DecimalFractionField()
     value = Decimal(.5)
     result = field.to_python(value)
     self.assertEqual(value, result)
    def test_prepare_value_string(self):
        """
        Test string fractions are returns as is
        """
        field = DecimalFractionField()
        result = field.prepare_value('1/4')
        self.assertEqual('1/4', result)

        result = field.prepare_value('1 1/4')
        self.assertEqual('1 1/4', result)
    def test_prepare_value_string(self):
        """
        Test string fractions are returns as is
        """
        field = DecimalFractionField()
        result = field.prepare_value('1/4')
        self.assertEqual('1/4', result)

        result = field.prepare_value('1 1/4')
        self.assertEqual('1 1/4', result)
    def test_prepare_value_coerce_thirds(self):
        """
        Test that when coerce_thirds is specified, then .66, .67, and .33, etc.
        are converted properly to 1/3 and 2/3
        """
        field = DecimalFractionField(coerce_thirds=True)
        result = field.prepare_value(Decimal(1 / 3.0))
        self.assertEqual('1/3', result)

        result = field.prepare_value(Decimal(1 / 3.0))
        self.assertEqual('1/3', result)

        result = field.prepare_value(Decimal(2 / 3.0))
        self.assertEqual('2/3', result)

        result = field.prepare_value(Decimal(2 / 6.0))
        self.assertEqual('1/3', result)

        result = field.prepare_value(Decimal(4 / 6.0))
        self.assertEqual('2/3', result)

        result = field.prepare_value(Decimal(4 / 3.0))
        self.assertEqual('1 1/3', result)

        result = field.prepare_value(Decimal(5 / 3.0))
        self.assertEqual('1 2/3', result)
    def test_to_python_hyphenated_mixed_fraction_string(self):
        field = DecimalFractionField()
        value = '1-1/2'
        result = field.to_python(value)
        self.assertEqual(Decimal(3/2.0).quantize(Decimal('0.000')),
                         result.quantize(Decimal('0.000')))

        value = '1 - 1/2'
        result = field.to_python(value)
        self.assertEqual(Decimal(3/2.0).quantize(Decimal('0.000')),
                         result.quantize(Decimal('0.000')))
    def test_prepare_value_coerce_thirds(self):
        """
        Test that when coerce_thirds is specified, then .66, .67, and .33, etc.
        are converted properly to 1/3 and 2/3
        """
        field = DecimalFractionField(coerce_thirds=True)
        result = field.prepare_value(Decimal(1/3.0))
        self.assertEqual('1/3', result)

        result = field.prepare_value(Decimal(1/3.0))
        self.assertEqual('1/3', result)

        result = field.prepare_value(Decimal(2/3.0))
        self.assertEqual('2/3', result)

        result = field.prepare_value(Decimal(2/6.0))
        self.assertEqual('1/3', result)

        result = field.prepare_value(Decimal(4/6.0))
        self.assertEqual('2/3', result)

        result = field.prepare_value(Decimal(4/3.0))
        self.assertEqual('1 1/3', result)

        result = field.prepare_value(Decimal(5/3.0))
        self.assertEqual('1 2/3', result)
    def test_to_python_method_validation_errors(self):
        """
        Exceptions here are tested for and raised from within to_python()
        """
        field = DecimalFractionField()
        with self.assertRaises(ValidationError):
            field.clean('abcd')

        with self.assertRaises(ValidationError):
            field.clean('1 1 1/3')

        with self.assertRaises(ValidationError):
            field.clean('1 1')
    def test_to_python_validation_errors(self):
        field = DecimalFractionField()
        with self.assertRaises(ValidationError):
            field.to_python('abcd')

        with self.assertRaises(ValidationError):
            field.to_python('1 1 1/3')

        with self.assertRaises(ValidationError):
            field.to_python('1 1')
    def test_decimal_places_validation_errors(self):
        """
        Test decimal specific validation.  In django
        1.8 this happens in validate() but once support for 1.8
        is dropped it will be in run_validators, so this test uses clean()
        """
        field = DecimalFractionField(max_digits=3, decimal_places=2)

        with self.assertRaises(ValidationError):
            # too many non-decimal digits
            field.clean('10')

        with self.assertRaises(ValidationError):
            # too many decimal digits
            field.clean('1/100')
 def test_to_python_fraction_string(self):
     field = DecimalFractionField()
     value = '1/2'
     result = field.to_python(value)
     self.assertEqual(Decimal('.5'), result)
 def test_to_python_float_string(self):
     field = DecimalFractionField()
     value = '0.5'
     result = field.to_python(value)
     self.assertEqual(Decimal(value), result)
 def test_to_python_fraction_string(self):
     field = DecimalFractionField()
     value = '1/2'
     result = field.to_python(value)
     self.assertEqual(Decimal('.5'), result)
 def test_validate_negative_inf_raises_error(self):
     field = DecimalFractionField()
     with self.assertRaises(ValidationError):
         field.validate(Decimal("-Inf"))
 def test_validate_negative_inf_raises_error(self):
     field = DecimalFractionField()
     with self.assertRaises(ValidationError):
         field.validate(Decimal("-Inf"))
 def test_to_python_float_string(self):
     field = DecimalFractionField()
     value = '0.5'
     result = field.to_python(value)
     self.assertEqual(Decimal(value), result)