Skip to content

Instantly share code, notes, and snippets.

@tanishiking
Created May 4, 2015 08:53
Show Gist options
  • Save tanishiking/ff7336608fca2c889955 to your computer and use it in GitHub Desktop.
Save tanishiking/ff7336608fca2c889955 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.files.uploadedfile import SimpleUploadedFile
from StringIO import StringIO
from PIL import Image
def get_image_dict():
"""create inmemory dummy image for testing forms which have image field"""
img_file = StringIO()
img = Image.new('RGBA', size=(10,10), color=(255, 255, 255))
img.save(img_file, 'png')
img_file.name = 'test_img.png'
img_file.seek(0)
img_dict = {
'image': SimpleUploadedFile(
img_file.name,
img_file.read(),
content_type='image/png')}
return img_dict
@tanishiking
Copy link
Author

Usage

from inmemory_image_generator import get_image_dict

class HogeTest(TestCase):
    def test_hoge(self):
        img_dict = get_image_dict()
        ...
        param = {
            ...
            }
        form = HogeForm(param, img_dict)
        self.assertTrue(form.is_valid())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment