作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 django 管理中为内联添加了一些验证,如果模型实例上的字段不存在,则不会保存内联。如果还没有截止日期,我不想添加额外的截止日期:
class DeadlineInlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
super(DeadlineInlineFormset, self).clean()
# make sure deadline is set
if not self.instance.deadline:
raise ValidationError(_("Doesn't validate"))
class DeadlineInline(admin.StackedInline):
model = AdditionalDeadline
formset = DeadlineInlineFormset
class GrantAdmin(admin.ModelAdmin):
...
inlines = [DeadlineInline]
...
这工作正常,但我正在努力对其进行单元测试。到目前为止,我有:
class GrantAdminTestCase(TestCase):
fixtures = [...]
def test_forms(self):
# create the form from a model instance.
# it should validate but it doesn't
g = Grant.objects.first()
form = GrantAdminForm(instance=g)
self.assertEqual(form.is_valid(), True)
# remove deadline and add additional deadlines
# It shouldn't be valid because deadline is None
g.deadline = None
g.additional_deadlines.add(
Deadline(deadline=datetime.now())
)
self.assertEqual(form.is_valid(), False)
即使模型实例有效,第一个断言也会失败。第二个断言通过但不应该因为它现在应该是无效的。所以肯定有问题。
我也不确定这是否是正确的方法。将验证放在模型中会更容易吗?或者甚至是表单而不是表单集?
编辑:其中有很多是错误的,我已经更改了一些内容,但仍然无法正常工作:
def test_forms(self):
g = Grant.objects.first()
form = GrantAdminForm(model_to_dict(g))
self.assertEqual(form.is_valid(), True)
DeadlineFormSet = inlineformset_factory(Grant, Deadline)
# should be invalid
g.deadline = None
g.additional_deadlines.add(Deadline(deadline=datetime.now()))
formset = DeadlineFormSet(instance=g)
self.assertEqual(formset.is_valid(), False)
# should be valid
g = Grant.objects.first()
formset = DeadlineFormSet(instance=g)
self.assertEqual(formset.is_valid(), True)
前两个断言通过,但最后一个没有通过。
最佳答案
This让我得到的答案是:
class GrantAdminTestCase(TestCase):
fixtures = [
'grants/grant_config',
'grants/grants',
]
def setUp(self):
self.g = Grant.objects.first()
self.DeadlineFormSet = inlineformset_factory(
Grant, Deadline, formset=DeadlineInlineFormset)
self.data = {
'additional_deadlines-INITIAL_FORMS': 0,
'additional_deadlines-MAX_NUM_FORMS': 1000,
'additional_deadlines-TOTAL_FORMS': 1,
'additional_deadlines-0-deadline': date.today(),
'additional_deadlines-0-grant': '2',
'additional_deadlines-0-id': '',
}
def test_formset_is_valid_if_deadline_exists(self):
formset = self.DeadlineFormSet(self.data, instance=self.g)
self.assertEqual(formset.is_valid(), True)
def test_formset_is_invalid_if_deadline_doesnt_exist(self):
# formset should be invalid if there is an additional deadline
# and no main deadline
self.g.deadline = None
formset = self.DeadlineFormSet(self.data, instance=self.g)
self.assertEqual(formset.is_valid(), False)
def test_formset_is_valid_if_deadline_blank(self):
# formset should still be valid if deadline is blank
# (it's not saved)
self.data['additional_deadlines-0-deadline'] = ''
formset = self.DeadlineFormSet(self.data, instance=self.g)
self.assertEqual(formset.is_valid(), True)
def test_formset_is_invalid_if_additional_deadlines_earlier(self):
# formset should be invalid if any additional deadlines are earlier
# than the main deadline
self.g.deadline = date.today()
yesterday = date.today() - timedelta(days=1)
self.data['additional_deadlines-0-deadline'] = yesterday
formset = self.DeadlineFormSet(self.data, instance=self.g)
self.assertEqual(formset.is_valid(), False)
关于python - 在 Django admin 中对内联进行单元测试验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735428/
我是一名优秀的程序员,十分优秀!