Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 def test_gm_options_crop_gravities(self, crop, expected):
     image = ImageModelFactory.create()
     thumbnail = Thumbnail(image.file.path, {
         'size': ['100', '50'],
         'crop': crop
     })
     assert thumbnail.get_gm_options().get('gravity', None) == expected
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    def test_generate_pngquant_called(self, pngquant_mock):
        image = ImageModelFactory.create(file__filename='test.png')
        thumbnail = Thumbnail(image.file.path, {
            'size': ['50', '50'],
            'pngquant': '50'
        })

        assert thumbnail.generate() is True
        assert pngquant_mock.called is True
Ejemplo n.º 5
0
 def test_base64_content(self):
     image = ImageModelFactory.create()
     thumbnail = Thumbnail(image.file.path, {'size': ['2', '2']})
     assert thumbnail.get_base64_content() == (
         '/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAg'
         'MDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8'
         'XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQU'
         'FBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAACAAEDAREAAhEBAxEB/'
         '8QAFAABAAAAAAAAAAAAAAAAAAAAB//EABQQAQAAAAAAAAAAAAAAAAAAAAD/xAAVAQ'
         'EBAAAAAAAAAAAAAAAAAAAICf/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhE'
         'DEQA/AHhPom3/2Q==')
    def test_media_source(self, settings):
        source = ImageModelFactory.create()

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

        context = Context()
        assert template.render(context) == ''

        assert context['img'][0].source == os.path.join(
            settings.MEDIA_ROOT, source.file.name)
    def test_crop(self):
        source = ImageModelFactory.create(file__width=210, file__height=100)

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

        context = Context()
        assert template.render(context) == ''

        assert 'img' in context
        assert len(context['img']) == 1
        assert context['img'][0].options['crop'] is True
    def test_viewport(self):
        source = ImageModelFactory.create(file__width=400, file__height=100)

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

        context = Context()
        assert template.render(context) == ''

        assert 'img' in context
        assert len(context['img']) == 1
        assert context['img'][0].options['size'] == ['200', '0']
        assert context['img'][0].options['viewport'] == ['600', '0']
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    def test_oversize_exact_crop_cross_aspect(self):
        source = ImageModelFactory.create(file__width=210, file__height=420)

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

        context = Context()
        assert 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 == '20'
        assert context['img'][1].requested_size.width == '200'
        assert context['img'][1].requested_size.height == '20'
        assert context['img'][2].requested_size.width == '210'
        assert context['img'][2].requested_size.height == '20'
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    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'),
            ('quality', 90),
        ]
Ejemplo n.º 13
0
    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()
        assert 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'][0].url is not None
        assert context['img'][0].url_2x is not None
        assert context['img'][1].requested_size.width == '105'
        assert context['img'][1].requested_size.height == '0'
        assert context['img'][1].url is not None
        assert context['img'][1].url_2x is not None
Ejemplo n.º 14
0
    def test_oversize_upscale(self):
        source = ImageModelFactory.create(file__width=210, file__height=100)

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

        context = Context()
        assert template.render(context) == ''

        assert 'img' in context
        assert len(context['img']) == 4
        assert context['img'][0].requested_size.width == '100'
        assert context['img'][0].requested_size.height == '0'
        assert context['img'][1].requested_size.width == '200'
        assert context['img'][1].requested_size.height == '0'
        assert context['img'][2].requested_size.width == '300'
        assert context['img'][2].requested_size.height == '0'
        assert context['img'][3].requested_size.width == '400'
        assert context['img'][3].requested_size.height == '0'
Ejemplo n.º 15
0
    def test_viewport(self, input_size, thumb_size, viewport, 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])),
                'viewport':
                (str(viewport[0]), str(viewport[1])) if viewport else None,
            },
        )

        assert_error = '{0} -> {1} / {2}'.format(
            input_size,
            thumb_size,
            viewport,
        )

        viewport_size = instance.viewport
        assert viewport_size.width == expected[0], assert_error
        assert viewport_size.height == expected[1], assert_error
Ejemplo n.º 16
0
 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
Ejemplo n.º 17
0
 def test_base64_path(self):
     image = ImageModelFactory.create()
     thumbnail = Thumbnail(image.file.path, {'size': ['50', '50']})
     assert thumbnail.get_base64_path().endswith('.base64')
Ejemplo n.º 18
0
 def setup(self):
     self.image = ImageModelFactory.create()
     self.thumbnail = Thumbnail(self.image.file.path, {'size': [50, 50]})
Ejemplo n.º 19
0
 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())
Ejemplo n.º 20
0
    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
Ejemplo n.º 21
0
    def test_generate_pngquant_disabled(self, pngquant_mock):
        image = ImageModelFactory.create()
        thumbnail = Thumbnail(image.file.path, {'size': ['50', '50']})

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