以下是models.py中的类。
class Unit(models.Model):
name = models.CharField(max_length=200)
start = models.DateTimeField()
end = models.DateTimeField()
description = models.TextField()
deleted = models.BooleanField(default=False)
def clean(self):
if self.end and self.start and self.end <= self.start:
raise ValidationError({
'end': _('End date should be after start date')
})
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('decentmark:unit_view', kwargs={'unit_id': self.pk})
我已经编写了用于验证日期,字符串长度等的测试。以下是上述课程的测试。
from django.test import TestCase
from decentmark.models import *
class UnitModelTest(TestCase):
@classmethod
def setUpTestData(cls):
Unit.objects.create(name='Python', start='2018-10-25 14:30:59',
end='2017-10-25 14:30:59', description='111',
deleted='False')
def test_name_label(self):
unit = Unit.objects.get(id=1) # type: Unit
field_label = unit._meta.get_field('name').verbose_name
self.assertEquals(field_label, 'name')
def test_name_max_length(self):
unit = Unit.objects.get(id=1)
max_length = unit._meta.get_field('name').max_length
self.assertEquals(max_length, 200)
def test_str(self):
unit = Unit.objects.get(id=1)
expected_object_name = unit.name
self.assertEquals(expected_object_name, str(unit))
def test_date(self):
unit = Unit.objects.get(id=1)
with self.assertRaises(ValidationError):
unit.full_clean()
是否需要为models.py中的上述类编写其他测试。