def test_ordering(self): # Create some instances, not in date order sep_1 = YearlessDateModel.objects.create( yearless_date=YearlessDate(1, 9)) nov_10 = YearlessDateModel.objects.create( yearless_date=YearlessDate(10, 11)) jan_29 = YearlessDateModel.objects.create( yearless_date=YearlessDate(29, 1)) jan_9 = YearlessDateModel.objects.create( yearless_date=YearlessDate(9, 1)) aug_5 = YearlessDateModel.objects.create( yearless_date=YearlessDate(5, 8)) model_ids_in_order = [ jan_9.id, jan_29.id, aug_5.id, sep_1.id, nov_10.id ] # Pull the models from the database, ordering by date retrieved_models = YearlessDateModel.objects.filter( id__in=model_ids_in_order).order_by('yearless_date').values_list( 'id', flat=True) # Check the lists are the same assert all( [a == b for a, b in zip(retrieved_models, model_ids_in_order)])
def test_deconstruct(self): yearless_date = YearlessDate(30, 9) serialized = yearless_date.deconstruct() # Deserialize it. cls = import_string(serialized[0]) deserialized = cls(*serialized[1], **serialized[2]) assert deserialized == yearless_date
def test_deconstruct(self): yearless_date = YearlessDate(30, 9) serialized = yearless_date.deconstruct() # Deserialize it. cls = import_string(serialized[0]) deserialized = cls(*serialized[1], **serialized[2]) assert deserialized == yearless_date
def test_invalid_day_raises_exception(self): # February never has 30 days with self.assertRaisesRegexp( Exception, 'Cannot create DateInYear object - invalid day value 30 for month February.' ): YearlessDate(30, 2)
def test_yearless_date_form_has_changed_value_to_empty(self): form = YearlessDateForm(initial={'yearless_date': YearlessDate(1, 9)}, data={ 'yearless_date_0': '', 'yearless_date_1': '' }) assert form.has_changed()
def test_valid_yearless_date(self): form = YearlessDateForm(data={ 'yearless_date_0': '1', 'yearless_date_1': '9' }) assert form.is_valid() assert form.cleaned_data['yearless_date'] == YearlessDate(1, 9)
def test_yearless_date_form_has_not_changed(self): form = YearlessDateForm(initial={'yearless_date': YearlessDate(1, 9)}, data={ 'yearless_date_0': '1', 'yearless_date_1': '9' }) assert not form.has_changed()
def test_yearless_date_form_has_changed_value_to_new_value(self): form = YearlessDateForm( initial={'yearless_date': YearlessDate(29, 10)}, data={ 'yearless_date_0': '18', 'yearless_date_1': '3' }) assert form.has_changed()
def test_leap_year_date_is_valid(self): yearless_date = YearlessDate(29, 2) self.assertEqual(yearless_date.day, 29) self.assertEqual(yearless_date.month, 2)
def test_value_to_string(self): yearless_date = YearlessDate(3, 7) model = YearlessDateModel(yearless_date=yearless_date) field = model._meta.get_field('yearless_date') assert field.value_to_string(model) == '0703'
def setUpClass(cls): cls.earlier = YearlessDate(30, 9) cls.later = YearlessDate(1, 10)
def test_month_name(self): self.assertEqual(YearlessDate(30, 12).month_name, 'December')
def test_non_yearless_dates_are_not_equal(self): self.assert_not_equal_thorough(YearlessDate(9, 6), 'foo')
def test_same_day_different_months_are_not_equal(self): self.assert_not_equal_thorough(YearlessDate(15, 3), YearlessDate(15, 6))
def test_storage_and_retrieval(self): yearless_date = YearlessDate(3, 7) model = YearlessDateModel.objects.create(yearless_date=yearless_date) retrieved_model = YearlessDateModel.objects.get(id=model.id) assert retrieved_model.yearless_date == yearless_date
def test_form_field_can_be_overridden(self): model = YearlessDateModel(yearless_date=YearlessDate(1, 9)) field = model._meta.get_field('yearless_date') assert isinstance(field.formfield(form_class=MyCustomFormField), MyCustomFormField)
def test_form_field_default(self): model = YearlessDateModel(yearless_date=YearlessDate(1, 9)) field = model._meta.get_field('yearless_date') assert isinstance(field.formfield(), forms.YearlessDateField)
def test_str(self): self.assertEqual(str(YearlessDate(15, 6)), '15 June')
def test_identical_dates_are_equal(self): self.assert_equal_thorough(YearlessDate(15, 6), YearlessDate(15, 6))
def test_decompress_with_value(self): assert YearlessDateSelect().decompress(YearlessDate(22, 6)) == [22, 6]
def test_different_days_same_month_are_not_equal(self): self.assert_not_equal_thorough(YearlessDate(12, 6), YearlessDate(15, 6))
def test_equal_dates_are_not_less_than(self): self.assertFalse(YearlessDate(30, 9) < YearlessDate(30, 9))
def test_equal_dates_are_not_greater_than(self): self.assertFalse(YearlessDate(30, 9) > YearlessDate(30, 9))
class YearlessDateModel(models.Model): """Sample model for testing YearlessDateField. """ yearless_date = YearlessDateField() optional_yearless_date = YearlessDateField(blank=True, null=True) yearless_date_with_default = YearlessDateField(default=YearlessDate(day=3, month=5))
def test_equal_dates_are_greater_than_or_equal_to(self): self.assertTrue(YearlessDate(30, 9) >= YearlessDate(30, 9))
def test_invalid_month_raises_exception(self): with self.assertRaisesRegexp( Exception, 'Cannot create DateInYear object with a month value of 0.'): YearlessDate(1, 0)
def test_equal_dates_are_less_than_or_equal_to(self): self.assertTrue(YearlessDate(30, 9) <= YearlessDate(30, 9))
def test_empty_values(self): model = YearlessDateModel.objects.create( yearless_date=YearlessDate(1, 1)) retrieved_model = YearlessDateModel.objects.get(id=model.id) assert retrieved_model.optional_yearless_date == None