def test_ok02(self): "Minutes" period = DatePeriodField().clean(['minutes', '5']) self.assertIsInstance(period, DatePeriod) create_dt = self.create_datetime self.assertEqual( create_dt(year=2014, month=7, day=2, hour=22, minute=14), create_dt(year=2014, month=7, day=2, hour=22, minute=9) + period.as_timedelta())
def test_period_names_2(self): field = DatePeriodField() field.period_names = names = (MinutesPeriod.name, HoursPeriod.name) self.assertListEqual( [*date_period_registry.choices(choices=names)], [*field.choices], )
def test_invalid(self): clean = DatePeriodField().clean self.assertFieldValidationError(IntegerField, 'invalid', clean, ['years', 'notint']) name = 'unknownperiod' self.assertFieldValidationError(ChoiceField, 'invalid_choice', clean, [name, '2'], message_args={'value': name}, )
def test_not_required(self): clean = DatePeriodField(required=False).clean self.assertIsNone(clean(['', ''])) self.assertIsNone(clean([''])) self.assertIsNone(clean([])) self.assertIsNone(clean(['days', ''])) self.assertIsNone(clean(['days'])) self.assertIsNone(clean(['', 2]))
def test_period_names(self): clean = DatePeriodField(period_names=('months',)).clean period = clean(['months', '5']) self.assertIsInstance(period, DatePeriod) name = 'years' self.assertFieldValidationError(ChoiceField, 'invalid_choice', clean, [name, '2'], message_args={'value': name}, )
def test_notnull(self): with self.assertRaises(ValidationError) as cm: DatePeriodField().clean(['days', '0']) self.assertEqual([ _(u'Ensure this value is greater than or equal to %(limit_value)s.' ) % { 'limit_value': 1 }, ], cm.exception.messages)
def test_choices(self): choices = [*DatePeriodField().choices] self.assertInChoices( value=MinutesPeriod.name, label=MinutesPeriod.verbose_name, choices=choices, ) self.assertInChoices( value=HoursPeriod.name, label=HoursPeriod.verbose_name, choices=choices, ) self.assertInChoices( value=DaysPeriod.name, label=DaysPeriod.verbose_name, choices=choices, )
def test_required(self): clean = DatePeriodField().clean self.assertFieldValidationError(DatePeriodField, 'required', clean, ['', '']) self.assertFieldValidationError(DatePeriodField, 'required', clean, None)
def test_period_names_2(self): field = DatePeriodField() field.period_names = names = (MinutesPeriod.name, HoursPeriod.name) self.assertEqual(list(date_period_registry.choices(choices=names)), list(field.choices))
def test_registry_1(self): self.assertListEqual([*date_period_registry.choices()], [*DatePeriodField().choices] )
def test_registry_3(self): registry = DatePeriodRegistry(MinutesPeriod, HoursPeriod) field = DatePeriodField() field.period_registry = registry self.assertEqual(list(registry.choices()), list(field.choices))
def test_period_names_1(self): names = (MinutesPeriod.name, HoursPeriod.name) self.assertEqual(list(date_period_registry.choices(choices=names)), list(DatePeriodField(period_names=names).choices))
def test_registry_2(self): registry = DatePeriodRegistry(MinutesPeriod, HoursPeriod) self.assertEqual( list(registry.choices()), list(DatePeriodField(period_registry=registry).choices))
def test_period_names_1(self): names = (MinutesPeriod.name, HoursPeriod.name) self.assertListEqual( [*date_period_registry.choices(choices=names)], [*DatePeriodField(period_names=names).choices], )
def test_empty(self): self.assertIsNone(DatePeriodField(required=False).clean(None))
def test_registry_3(self): registry = DatePeriodRegistry(MinutesPeriod, HoursPeriod) field = DatePeriodField() field.period_registry = registry self.assertListEqual([*registry.choices()], [*field.choices])
def test_registry_2(self): registry = DatePeriodRegistry(MinutesPeriod, HoursPeriod) self.assertListEqual( [*registry.choices()], [*DatePeriodField(period_registry=registry).choices] )
def test_choices(self): choices = list(DatePeriodField().choices) self.assertIn((MinutesPeriod.name, MinutesPeriod.verbose_name), choices) self.assertIn((HoursPeriod.name, HoursPeriod.verbose_name), choices) self.assertIn((DaysPeriod.name, DaysPeriod.verbose_name), choices)
def test_registry_1(self): self.assertEqual(list(date_period_registry.choices()), list(DatePeriodField().choices))