1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
import pytest
from django import forms
from django.core.files.base import File
from django.core.files.uploadedfile import SimpleUploadedFile
from imagekit import forms as ikforms
from imagekit.processors import SmartCrop
from . import imagegenerators # noqa
from .models import (ImageModel, ProcessedImageFieldModel,
ProcessedImageFieldWithSpecModel)
from .utils import get_image_file
@pytest.mark.django_db(transaction=True)
def test_model_processedimagefield():
instance = ProcessedImageFieldModel()
with File(get_image_file()) as file:
instance.processed.save('whatever.jpeg', file)
instance.save()
assert instance.processed.width == 50
assert instance.processed.height == 50
@pytest.mark.django_db(transaction=True)
def test_model_processedimagefield_with_spec():
instance = ProcessedImageFieldWithSpecModel()
with File(get_image_file()) as file:
instance.processed.save('whatever.jpeg', file)
instance.save()
assert instance.processed.width == 100
assert instance.processed.height == 60
@pytest.mark.django_db(transaction=True)
def test_form_processedimagefield():
class TestForm(forms.ModelForm):
image = ikforms.ProcessedImageField(spec_id='tests:testform_image',
processors=[SmartCrop(50, 50)],
format='JPEG')
class Meta:
model = ImageModel
fields = 'image',
with get_image_file() as upload_file:
files = {
'image': SimpleUploadedFile('abc.jpg', upload_file.read())
}
form = TestForm({}, files)
instance = form.save()
assert instance.image.width == 50
assert instance.image.height == 50
|