gpt4 book ai didi

python - 在 Django admin 中对内联进行单元测试验证

转载 作者:行者123 更新时间:2023-11-28 22:50:22 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com