File: test_fields.py

package info (click to toggle)
python-django-imagekit 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 696 kB
  • sloc: python: 2,000; makefile: 133
file content (49 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download | duplicates (2)
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
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 nose.tools import eq_
from . import imagegenerators  # noqa
from .models import (ProcessedImageFieldModel,
                     ProcessedImageFieldWithSpecModel,
                     ImageModel)
from .utils import get_image_file


def test_model_processedimagefield():
    instance = ProcessedImageFieldModel()
    file = File(get_image_file())
    instance.processed.save('whatever.jpeg', file)
    instance.save()

    eq_(instance.processed.width, 50)
    eq_(instance.processed.height, 50)


def test_model_processedimagefield_with_spec():
    instance = ProcessedImageFieldWithSpecModel()
    file = File(get_image_file())
    instance.processed.save('whatever.jpeg', file)
    instance.save()

    eq_(instance.processed.width, 100)
    eq_(instance.processed.height, 60)


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',

    upload_file = get_image_file()
    file_dict = {'image': SimpleUploadedFile('abc.jpg', upload_file.read())}
    form = TestForm({}, file_dict)
    instance = form.save()

    eq_(instance.image.width, 50)
    eq_(instance.image.height, 50)