def test_get_size(self):
        image = ImageModelFactory.create()
        thumbnail = Thumbnail(image.file.path, {'size': ['50', '50']})

        size = thumbnail.get_size()
        assert size.width == 25
        assert size.height == 50
    def test_media_source(self, settings):
        source = ImageModelFactory.create()

        template = Template((
            '{%% load ultimatethumb_tags %%}'
            '{%% ultimatethumb "img" "%s" sizes="100x0" %%}'
        ) % source.file.name)

        context = Context()
        template.render(context)

        assert context['img'][0].source == os.path.join(
            settings.MEDIA_ROOT, source.file.name)
    def test_gm_options_crop(self):
        image = ImageModelFactory.create()
        thumbnail = Thumbnail(image.file.path, {'size': ['100', '50'], 'crop': False})
        assert 'gravity' not in thumbnail.get_gm_options()
        assert 'crop' not in thumbnail.get_gm_options()

        thumbnail = Thumbnail(image.file.path, {'size': ['100', '50'], 'crop': True})
        assert list(thumbnail.get_gm_options().items()) == [
            ('+profile', '"*"'),
            ('resize', '50x25^'),
            ('gravity', 'Center'),
            ('crop', '50x25+0+0')
        ]
    def test_quality(self):
        source = ImageModelFactory.create(file__width=210, file__height=100)

        template = Template((
            '{%% load ultimatethumb_tags %%}'
            '{%% ultimatethumb "img" "%s" sizes="200x0" quality=10 %%}'
        ) % source.file.path)

        context = Context()
        template.render(context)

        assert 'img' in context
        assert len(context['img']) == 1
        assert context['img'][0].options['quality'] == 10
    def test_estimated_size(self, input_size, thumb_size, upscale, crop, expected):
        image = ImageModelFactory.create(
            file__width=input_size[0], file__height=input_size[1])

        instance = Thumbnail(image.file.path, {
            'size': (str(thumb_size[0]), str(thumb_size[1])),
            'upscale': upscale,
            'crop': crop
        })
        assert instance.get_estimated_size() == Size(*expected), (
            '{0} -> {1} (upscale: {3} crop: {2})'.format(
                input_size,
                thumb_size,
                crop,
                upscale
            ))
    def test_retina(self):
        source = ImageModelFactory.create(file__width=210, file__height=100)

        template = Template((
            '{%% load ultimatethumb_tags %%}'
            '{%% ultimatethumb "img" "%s" sizes="100x0,200x0,300x0,400x0" %%}'
        ) % source.file.path)

        context = Context()
        template.render(context)

        assert 'img' in context
        assert len(context['img']) == 2
        assert context['img'][0].requested_size.width == '100'
        assert context['img'][0].requested_size.height == '0'
        assert context['img'][1].requested_size.width == '105'
        assert context['img'][1].requested_size.height == '50'
    def test_gm_options_sizes(self, input_size, thumb_size, upscale, crop, expected):
        image = ImageModelFactory.create(
            file__width=input_size[0], file__height=input_size[1])

        instance = Thumbnail(image.file.path, {
            'size': (str(thumb_size[0]), str(thumb_size[1])),
            'upscale': upscale,
            'crop': crop
        })

        assert_error = '{0} -> {1} (upscale: {3} crop: {2})'.format(
            input_size,
            thumb_size,
            crop,
            upscale
        )

        options = instance.get_gm_options()
        assert options.get('resize', None) == expected[0], assert_error
        assert options.get('crop', None) == expected[1], assert_error
    def test_retina_disabled(self):
        source = ImageModelFactory.create(file__width=210, file__height=100)

        template = Template((
            '{%% load ultimatethumb_tags %%}'
            '{%% ultimatethumb "img" "%s" sizes="100x0,200x0,300x0,400x0" retina=False %%}'
        ) % source.file.path)

        context = Context()
        template.render(context)

        assert 'img' in context
        assert len(context['img']) == 3
        assert context['img'][0].requested_size.width == '100'
        assert context['img'][0].requested_size.height == '0'
        assert context['img'][0].url is not None
        assert context['img'][0].url_2x is None
        assert context['img'][1].requested_size.width == '200'
        assert context['img'][1].requested_size.height == '0'
        assert context['img'][1].url is not None
        assert context['img'][1].url_2x is None
        assert context['img'][2].requested_size.width == '210'
        assert context['img'][2].requested_size.height == '100'
        assert context['img'][2].url is not None
 def test_storage_path(self):
     image = ImageModelFactory.create()
     thumbnail = Thumbnail(image.file.path, {'size': ['50', '50']})
     assert thumbnail.get_storage_path() == os.path.join(
         thumbnail_storage.location, thumbnail.get_name())
 def test_exists(self):
     image = ImageModelFactory.create()
     thumbnail = Thumbnail(image.file.path, {'size': ['50', '50']})
     assert thumbnail.exists() is False
     thumbnail.generate()
     assert thumbnail.exists() is True
    def test_exists_dont_generate(self, generate_mock):
        image = ImageModelFactory.create()
        thumbnail = Thumbnail(image.file.path, {'size': ['50', '50']})

        assert thumbnail.exists() is False
        assert generate_mock.called is False
 def test_gm_options_quality(self):
     image = ImageModelFactory.create()
     thumbnail = Thumbnail(image.file.path, {'size': ['100', '50'], 'quality': 5})
     assert thumbnail.get_gm_options()['quality'] == 5
 def setup(self):
     self.image = ImageModelFactory.create()
     self.thumbnail = Thumbnail(self.image.file.path, {'size': [50, 50]})
    def test_generate_pngquant_no_png(self, pngquant_mock):
        image = ImageModelFactory.create()
        thumbnail = Thumbnail(image.file.path, {'size': ['50', '50'], 'pngquant': '50'})

        assert thumbnail.generate() is True
        assert pngquant_mock.called is False