使用ImageField测试Django视图

时间:2017-03-31 08:15:45

标签: python django unit-testing

我有以下模型和表格

class AdImage(models.Model):
    advertiser = models.ForeignKey(Advertiser)
    picture = models.ImageField('Ad picture',
                            upload_to='ad_pics/%Y-%m-%d/')

class AdImageForm(forms.ModelForm):

    class Meta:
        model = AdImage
        exclude = ['advertiser']

    helper = FormHelper()

    helper.layout = Layout(
        Div('picture'),
        FormActions(
            Submit('submit', "Sichern")
        )
    )

通过以下方式提供:

class CreateAdImage(LoginRequiredMixin, CreateView):

    form_class = AdImageForm
    template_name = 'advertis/adimage_form.html'

    def form_valid(self, form):

        slug = self.kwargs.get('slug', None)
        advertiser = Advertiser.objects.get(slug=slug)
        form.instance.advertiser=advertiser

        form.save()

        ret = render(self.request, 'modal_form_success.html',
                 {
                     'success_message': "Bild gespeicher fuer %s gespeichert" %
                                        (advertiser.user.get_full_name()),
                     'reload' : True,
                 })

        return ret

这非常有效。我可以用

测试表格
class AdImageFormTestCase(TestCase):

    def setUp(self):

        self.creator = ShrUser.objects.create_user(username="foo", 
                         email="hallo.ich@localhost.de", password="bla")
        self.advertiser = Advertiser.objects.create(user=self.creator, 
                             city='Chicago')

        self.uploaded_image = get_temporary_image()

        self.form_data = {}
        self.file_dict = {
            'picture': self.uploaded_image,
        }

    def test_AdImageCreate(self):

        form = AdImageForm(self.form_data, self.file_dict)
        self.assertTrue(form.is_valid())

self.uploaded_image在哪里

def get_temporary_image():
    io = BytesIO()
    size = (200,200)
    color = (255,0,0,0)
    image = Image.new("RGBA", size, color)
    image.save(io, format='JPEG')
    image_file = InMemoryUploadedFile(io, None, 'foo.jpg', 'jpeg', 
                     image.size, None)
    image_file.seek(0)
    return image_file

但我不能让视图测试运行。我总是遇到以下所需的字段:

    def test_create_adimage(self):

        advertiser = Advertiser.objects.create(user=self.user)

        response = self.client.get(reverse('create_adimage', kwargs={'slug' : advertiser.slug,}))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'advertis/adimage_form.html')

        self.uploaded_image = get_temporary_image()

        post_data = {
        }

        file_dict = {
            'picture': self.uploaded_image,
        }

        response = self.client.post(reverse('create_adimage', kwargs={'slug': advertiser.slug,}),
                                post_data=post_data, file_dict=file_dict)

        self.assertEqual(response.status_code, 200)

anybode知道如何设置post_data和file_dict以获得有效的表单吗?

亲切的问候

迈克尔

1 个答案:

答案 0 :(得分:0)

关于AKS评论,以下作品是完美的

getPagination