My models.py is looking like this:
def validate_yearofbirth(value):
text = str(value)
if len(text) < 4 or len(text) > 4 or value < 1900:
raise ValidationError('Please insert your year of birth!')
class Personal(models.Model):
firstname = models.CharField(max_length = 100)
lastname = models.CharField(max_length = 100)
yearofbirth = models.PositiveIntegerField(validators = [validate_yearofbirth], null = True)
@property
def fullname(self):
return '{}, {}'.format(self.lastname, self.firstname)
def __str__(self):
return self.fullname
@property
def age(self):
year = datetime.now().year
age = year - self.yearofbirth
return age
Anyone know how to write a test for def validate_yearofbirth and for def age? I only managed to write a test for def fullname, but I haven't figured out how to write a test for a function which is not def __str__(self).
My test_models.py file for this class is looking like this:
class TestModel(TestCase):
def test_personal_str(self):
fullname = Personal.objects.create(nachname = 'Last Name', vorname = 'First Name')
self.assertEqual(str(fullname), 'Last Name, First Name')
Also you can ignore the fact that return age doesn't always return the "real" age - this isn't important for my class.