def test_value_is_normalized_on_full_clean(self):
        input_value = relativedelta(years=1, months=3, weeks=1, days=4.5, hours=5, minutes=70.5, seconds=80.100005,
                                    microseconds=5)
        obj = Interval(value=input_value)
        obj.full_clean()

        self.assertNotEqual(input_value, obj.value)
        self.assertStrictEqual(input_value.normalized(), obj.value)

        # Quick sanity check to ensure the input isn't mutated.
        # Take into account that weeks are added to days though!
        self.assertStrictEqual(11.5, input_value.days)
        self.assertStrictEqual(1, input_value.weeks)

        # Check that the values are normalized
        self.assertStrictEqual(1, obj.value.years)
        self.assertStrictEqual(3, obj.value.months)
        self.assertStrictEqual(11, obj.value.days)
        self.assertStrictEqual(18, obj.value.hours)
        self.assertStrictEqual(11, obj.value.minutes)
        self.assertStrictEqual(50, obj.value.seconds)
        self.assertStrictEqual(100010, obj.value.microseconds)

        # Derived value, from the number of days (see #2); but it's no
        # longer converted to a floating-point number in newer
        # versions of relativedelta....
        self.assertStrictEqual(1, obj.value.weeks)
    def test_weeks_value_is_derived_as_int_when_normalizing_on_full_clean(self):
        input_value = relativedelta(years=1, months=3, weeks=1, days=11.5, hours=5, minutes=70.5, seconds=80.100005,
                                    microseconds=5)
        obj = Interval(value=input_value)
        obj.full_clean()

        self.assertNotEqual(input_value, obj.value)
        self.assertStrictEqual(input_value.normalized(), obj.value)

        # Quick sanity check to ensure the input isn't mutated.
        # Take into account that weeks are added to days though!
        self.assertStrictEqual(18.5, input_value.days)
        self.assertStrictEqual(2, input_value.weeks)

        # Derived value, from the number of days (see #2)
        self.assertStrictEqual(2, obj.value.weeks)
    def test_string_input(self):
        obj = Interval(value='P1Y3M1W4.5DT5H70.5M80.10001S')
        obj.full_clean()

        self.assertIsInstance(obj.value, relativedelta)

        # Check that the values are normalized
        self.assertStrictEqual(1, obj.value.years)
        self.assertStrictEqual(3, obj.value.months)
        self.assertStrictEqual(11, obj.value.days)
        self.assertStrictEqual(18, obj.value.hours)
        self.assertStrictEqual(11, obj.value.minutes)
        self.assertStrictEqual(50, obj.value.seconds)
        self.assertStrictEqual(100010, obj.value.microseconds)

        # Derived value, from the number of days (see #2)
        self.assertStrictEqual(1, obj.value.weeks)
    def test_timedelta_input(self):
        td = timedelta(weeks=1, days=4.5, hours=5, minutes=70.5, seconds=80.100005, microseconds=5)
        obj = Interval(value=td)
        obj.full_clean()

        self.assertIsInstance(obj.value, relativedelta)

        # Check that the values are normalized
        self.assertStrictEqual(0, obj.value.years)
        self.assertStrictEqual(0, obj.value.months)
        self.assertStrictEqual(11, obj.value.days)
        self.assertStrictEqual(18, obj.value.hours)
        self.assertStrictEqual(11, obj.value.minutes)
        self.assertStrictEqual(50, obj.value.seconds)
        self.assertStrictEqual(100010, int(obj.value.microseconds))

        # Derived value, from the number of days (see #2)
        self.assertStrictEqual(1, obj.value.weeks)
    def test_invalid_objects_raise_validation_errors(self):
        obj = Interval()

        obj.value = True
        with self.assertRaises(ValidationError) as cm:
            obj.full_clean()
        self.assertEqual(set(['value']), set(cm.exception.message_dict.keys()))

        obj.value = 1
        with self.assertRaises(ValidationError) as cm:
            obj.full_clean()
        self.assertEqual(set(['value']), set(cm.exception.message_dict.keys()))

        obj.value = 'P1M'  # Check that the error is cleared when made valid again
        obj.full_clean()
    def test_invalid_string_inputs_raise_validation_error(self):
        obj = Interval()

        obj.value = 'blabla'
        with self.assertRaises(ValidationError) as cm:
            obj.full_clean()
        self.assertEqual(set(['value']), set(cm.exception.message_dict.keys()))

        obj.value = 'P1.5M'  # not allowed by relativedelta because it is supposedly ambiguous
        with self.assertRaises(ValidationError) as cm:
            obj.full_clean()
        self.assertEqual(set(['value']), set(cm.exception.message_dict.keys()))

        obj.value = 'P1M'  # Check that the error is cleared when made valid again
        obj.full_clean()
 def test_none_value_survives_full_clean(self):
     obj = Interval(value=None)
     obj.full_clean()
     self.assertIsNone(obj.value)