2014-03-29 81 views
1

我在django管理中添加了一些內聯驗證,如果模型實例上的某個字段不存在,則不會保存內聯。我不想增加額外的最後期限,如果沒有已經是最後期限:在Django中內聯單元測試驗證admin

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) 

前兩個斷言通過,但最後卻沒有。

回答

2

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)