单元测试用django删除和更新图像

时间:2018-04-13 08:45:35

标签: django django-forms

我有一个使用我的imagemodel的inlineformset。它有一个外键给我的住宿模型定义如下:

ImageFormset = inlineformset_factory(Lodging,ImageModel,fields=('image',),
can_delete=True,form=ImageForm,extra=0)

我可以从网址获取accommodation_id,并可以使用它来创建住宿实例。然后,我将它传递给我的ImageFormset实例

lodging=Lodging.objects.get(id=ad_id)
formset = ImageFormset(instance=lodging)

通过这种方式,我将得到一些初始形式。我的问题是我应该在单元测试中作为数据传递什么。 到目前为止我尝试的是:

data = {
        'imagemodel_set-TOTAL_FORMS': 2,
        'imagemodel_set-INITIAL_FORMS': 2,
        'imagemodel_set-0-image': SimpleUploadedFile('test_image1.jpg',
                get_binary_data(self.image3_location),
                content_type='image/jpeg'),
        'imagemodel_set-1-image': SimpleUploadedFile('test_image2.jpg',
                get_binary_data(self.image4_location),
                content_type='image/jpeg'),
    }

但是这给了我错误:“['ManagementForm数据丢失或被篡改']” 任何帮助将不胜感激。

编辑:

测试代码

def test_lodging_edit_view_success(self):
    create_lodging(self)
    response = self.client.get(reverse('lodging:edit',args=[1]))
    data = {
        'imagemodel_set-TOTAL_FORMS': 2,
        'imagemodel_set-INITIAL_FORMS': 2,
        'imagemodel_set-0-image': SimpleUploadedFile('test_image1.jpg',
                get_binary_data(self.image3_location),
                content_type='image/jpeg'),
        'imagemodel_set-1-image': SimpleUploadedFile('test_image2.jpg',
                get_binary_data(self.image4_location),
                content_type='image/jpeg'),
        'rent': '4100',
        'update': 'any value'
    }
    response = self.client.post(reverse('lodging:edit',args=[1]),data)

表单代码

@login_required
def lodging_edit_view(request,ad_id):
    try:
        lodging=Lodging.objects.get(id=ad_id)
        if lodging.posted_by!=request.user:
            raise ViewException('Unauthorized access.')
        sublodging = lodging.sublodging
        images = lodging.imagemodel_set.all()
    except Lodging.DoesNotExist:
        return HttpResponseRedirect(reverse('dashboard:home'))
    sublodging_form = CommonlyUsedLodgingUpdateForm(instance=sublodging)
    uneditable_form = UneditableForm(initial={
         'address': lodging.address,
         'posted_at': lodging.posted_at,
         'updated_at': lodging.updated_at,
         'no_times_booked': lodging.no_times_booked,
         'location': sublodging.location,
         'type': sublodging.lodging_type,
         'title': sublodging.title
    })
    formset = ImageFormset(instance=lodging)
    if request.method=='POST':
        if request.session.test_cookie_worked():
            if 'delete' in request.POST:
                with transaction.atomic():
                    for image in images:
                        image.delete()
                    lodging.delete()
            elif 'update' in request.POST:
                sublodging_form = CommonlyUsedLodgingUpdateForm(request.POST,instance=sublodging)
                formset = ImageFormset(request.FILES,request.POST,instance=lodging)
                if sublodging_form.is_valid() and formset.is_valid():
                    with transaction.atomic():
                        sublodging_form.save()
                        formset.save()
        else:
            uneditable_form.add_error(None,'Cookies are not enabled.')
    else:
        request.session.set_test_cookie()
    return render(request,'lodging/edit_lodging.html',{
        'uneditable_form':uneditable_form ,
        'sublodging_form': sublodging_form,
        'formset': formset
    })

0 个答案:

没有答案