Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #120 from marfire/pil_less
Browse files Browse the repository at this point in the history
Allowing tests to run without PIL
  • Loading branch information
vandersonmota committed Aug 31, 2013
2 parents 41f9f17 + 31616ca commit fa0e84f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
20 changes: 17 additions & 3 deletions test/generic/models.py
Expand Up @@ -15,6 +15,14 @@
from fields import *
from model_mommy.timezone import smart_datetime as datetime

# check whether or not PIL is installed
try:
from PIL import ImageFile as PilImageFile
except ImportError:
has_pil = False
else:
has_pil = True

GENDER_CH = [('M', 'male'), ('F', 'female')]


Expand Down Expand Up @@ -143,9 +151,15 @@ class DummyFileFieldModel(models.Model):
file_field = models.FileField(upload_to="%Y/%m/%d", storage=fs)


class DummyImageFieldModel(models.Model):
fs = FileSystemStorage(location=gettempdir())
image_field = models.ImageField(upload_to="%Y/%m/%d", storage=fs)
if has_pil:
class DummyImageFieldModel(models.Model):
fs = FileSystemStorage(location=gettempdir())
image_field = models.ImageField(upload_to="%Y/%m/%d", storage=fs)
else:
# doesn't matter, won't be using
class DummyImageFieldModel(models.Model):
pass


class DummyMultipleInheritanceModel(DummyDefaultFieldsModel, Person):
my_dummy_field = models.IntegerField()
Expand Down
34 changes: 19 additions & 15 deletions test/generic/tests/test_filling_fields.py
Expand Up @@ -26,6 +26,7 @@
from six import text_type

from model_mommy import mommy
from test.generic.models import has_pil
from test.generic.models import Person
from test.generic.models import DummyIntModel, DummyPositiveIntModel
from test.generic.models import DummyNumbersModel
Expand All @@ -39,8 +40,8 @@
'StringFieldsFilling', 'BooleanFieldsFilling', 'DateTimeFieldsFilling',
'DateFieldsFilling', 'FillingIntFields', 'FillingPositiveIntFields',
'FillingOthersNumericFields', 'FillingFromChoice', 'URLFieldsFilling',
'FillingEmailField', 'FillingGenericForeignKeyField','FillingFileField',
'TimeFieldsFilling', 'FillingCustomFields',
'FillingEmailField', 'FillingGenericForeignKeyField', 'FillingFileField',
'FillingImageFileField', 'TimeFieldsFilling', 'FillingCustomFields',
]


Expand Down Expand Up @@ -238,25 +239,28 @@ def test_filling_file_field(self):
def tearDown(self):
self.dummy.file_field.delete()


# skipUnless not available in Django 1.2
# @skipUnless(has_pil, "PIL is required to test ImageField")
class FillingImageFileField(TestCase):

def setUp(self):
path = mommy.mock_file_jpeg
self.fixture_img_file = ImageFile(open(path))

def test_filling_image_file_field(self):
self.dummy = mommy.make(DummyImageFieldModel)
field = DummyImageFieldModel._meta.get_field('image_field')
self.assertIsInstance(field, ImageField)
import time
path = "%s/%s/mock-img.jpeg" % (gettempdir(), time.strftime('%Y/%m/%d'))

from django import VERSION
if VERSION[1] >= 4:
self.assertEqual(abspath(self.dummy.image_field.path), abspath(path))
self.assertTrue(self.dummy.image_field.width)
self.assertTrue(self.dummy.image_field.height)
if has_pil:
def test_filling_image_file_field(self):
self.dummy = mommy.make(DummyImageFieldModel)
field = DummyImageFieldModel._meta.get_field('image_field')
self.assertIsInstance(field, ImageField)
import time
path = "%s/%s/mock-img.jpeg" % (gettempdir(), time.strftime('%Y/%m/%d'))

from django import VERSION
if VERSION[1] >= 4:
# These require the file to exist in earlier versions of Django
self.assertEqual(abspath(self.dummy.image_field.path), abspath(path))
self.assertTrue(self.dummy.image_field.width)
self.assertTrue(self.dummy.image_field.height)

def tearDown(self):
self.dummy.image_field.delete()
Expand Down

0 comments on commit fa0e84f

Please sign in to comment.