예제 #1
0
    def test_pngcrush_image_is_called(self, mock_move):
        expected_suffix = '.opti.png'
        tmp_src = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
        tmp_dest = os.path.splitext(tmp_src.name)[0] + expected_suffix
        open(tmp_dest, 'w').write(open(self.img_path).read())
        with mock.patch('tempfile.NamedTemporaryFile',
                        lambda *a, **k: tmp_src):
            rval = tasks.pngcrush_image(self.img_path)
            # pngcrush_image copies the stored file to a local tempfile.
            eq_(open(tmp_src.name).read(), open(self.img_path).read())

        expected_cmd = [
            'pngcrush', '-q', '-rem', 'alla', '-brute', '-reduce', '-e',
            expected_suffix, tmp_src.name
        ]
        # pngcrush_image incokes pngcrush with the tempfile name and writes to
        # another tempfile.
        self.mock_popen.assert_called_once_with(expected_cmd,
                                                stdin=subprocess.PIPE,
                                                stderr=subprocess.PIPE,
                                                stdout=subprocess.PIPE)
        # The output file is then copied back to storage.
        mock_move.assert_called_once_with(tmp_dest,
                                          self.img_path,
                                          dest_storage=public_storage,
                                          src_storage=local_storage)
        eq_(rval, {'image_hash': 'bb362450'})
예제 #2
0
    def test_pngcrush_image_is_called(self, mock_move):
        expected_suffix = '.opti.png'
        tmp_src = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
        tmp_dest = os.path.splitext(tmp_src.name)[0] + expected_suffix
        copy_stored_file(self.img_path, tmp_dest, src_storage=public_storage,
                         dst_storage=local_storage)
        with mock.patch('tempfile.NamedTemporaryFile',
                        lambda *a, **k: tmp_src):
            rval = tasks.pngcrush_image(self.img_path)
            # pngcrush_image copies the stored file to a local tempfile.
            eq_(open(tmp_src.name).read(),
                public_storage.open(self.img_path).read())

        expected_cmd = ['pngcrush', '-q', '-rem', 'alla', '-brute', '-reduce',
                        '-e', expected_suffix, tmp_src.name]
        # pngcrush_image incokes pngcrush with the tempfile name and writes to
        # another tempfile.
        self.mock_popen.assert_called_once_with(
            expected_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
            stdout=subprocess.PIPE)
        # The output file is then copied back to storage.
        mock_move.assert_called_once_with(tmp_dest, self.img_path,
                                          dst_storage=public_storage,
                                          src_storage=local_storage)
        eq_(rval, {'image_hash': 'bb362450'})
예제 #3
0
    def test_set_modified(self, mock_move, update_mock):
        """Test passed instance is updated with the hash."""
        name = self.src.name
        obj = app_factory()

        ret = tasks.pngcrush_image(name, 'some_hash', set_modified_on=[obj])
        ok_('some_hash' in ret)
        eq_(update_mock.call_args_list[-1][1]['some_hash'], ret['some_hash'])
        ok_('modified' in update_mock.call_args_list[-1][1])
예제 #4
0
    def test_pngcrush_image_is_called(self, mock_move):
        name = self.src.name
        expected_suffix = '.opti.png'
        expected_cmd = ['pngcrush', '-q', '-rem', 'alla', '-brute', '-reduce',
                        '-e', expected_suffix, name]

        rval = tasks.pngcrush_image(name)
        self.mock_popen.assert_called_once_with(
            expected_cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
            stdout=subprocess.PIPE)
        mock_move.assert_called_once_with(
            '%s%s' % (os.path.splitext(name)[0], expected_suffix), name)
        eq_(rval, {'image_hash': 'bb362450'})
예제 #5
0
    def test_set_modified(self, mock_move):
        """Test passed instance is updated with the hash."""
        name = self.src.name

        instance_mock = mock.Mock()
        update_mock = mock.Mock()
        instance_mock.update = update_mock

        ret = tasks.pngcrush_image(name, 'some_hash',
                                   set_modified_on=[instance_mock])
        ok_('some_hash' in ret)
        eq_(update_mock.call_args_list[0][1]['some_hash'], ret['some_hash'])
        ok_('modified' in update_mock.call_args_list[0][1])
예제 #6
0
 def test_set_modified(self, mock_move, update_mock):
     """Test passed instance is updated with the hash."""
     expected_suffix = '.opti.png'
     tmp_src = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
     tmp_dest = os.path.splitext(tmp_src.name)[0] + expected_suffix
     open(tmp_dest, 'w').write(open(self.img_path).read())
     with mock.patch('tempfile.NamedTemporaryFile',
                     lambda *a, **k: tmp_src):
         obj = app_factory()
         ret = tasks.pngcrush_image(self.img_path, 'some_hash',
                                    set_modified_on=[obj])
     ok_('some_hash' in ret)
     eq_(update_mock.call_args_list[-1][1]['some_hash'], ret['some_hash'])
     ok_('modified' in update_mock.call_args_list[-1][1])
예제 #7
0
 def test_set_modified(self, mock_move, update_mock):
     """Test passed instance is updated with the hash."""
     expected_suffix = '.opti.png'
     tmp_src = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
     tmp_dest = os.path.splitext(tmp_src.name)[0] + expected_suffix
     open(tmp_dest, 'w').write(open(self.img_path).read())
     with mock.patch('tempfile.NamedTemporaryFile',
                     lambda *a, **k: tmp_src):
         obj = app_factory()
         ret = tasks.pngcrush_image(self.img_path,
                                    'some_hash',
                                    set_modified_on=[obj])
     ok_('some_hash' in ret)
     eq_(update_mock.call_args_list[-1][1]['some_hash'], ret['some_hash'])
     ok_('modified' in update_mock.call_args_list[-1][1])
예제 #8
0
    def test_set_modified(self):
        """Test passed instance is updated with the hash."""
        src_crushed = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                                  delete=False)
        shutil.copyfile(self.src.name, src_crushed.name)

        instance_mock = mock.Mock()
        update_mock = mock.Mock()
        instance_mock.update = update_mock

        ret = tasks.pngcrush_image(src_crushed.name, 'some_hash',
                                   set_modified_on=[instance_mock])
        ok_('some_hash' in ret)
        eq_(update_mock.call_args_list[0][1]['some_hash'], ret['some_hash'])
        ok_('modified' in update_mock.call_args_list[0][1])
예제 #9
0
    def test_pngcrush_image_is_optimized(self):
        src_crushed = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                                  delete=False)
        shutil.copyfile(self.src.name, src_crushed.name)

        rval = tasks.pngcrush_image(src_crushed.name)
        ok_(rval)

        crushed_size = os.path.getsize(src_crushed.name)
        orig_size = os.path.getsize(self.src.name)
        assert crushed_size < orig_size

        orig_image = Image.open(self.src.name)
        crushed_image = Image.open(src_crushed.name)
        eq_(ImageChops.difference(crushed_image, orig_image).getbbox(), None)
        os.remove(src_crushed.name)