Beispiel #1
0
    def test_create_log(self):
        """
        Ensure create_log function is working properly.
        * create new log file and log a message, then confirm the file
        and message exist.
        """
        from garage.logger import create_log
        from garage.utils import get_file_contents, delete_file

        self._msg("test", "create_log", first=True)

        tmpfile_dir = tempfile.gettempdir()
        logname = "garage-test"
        filename = "{0}.log".format(logname)
        path = os.path.join(tmpfile_dir, filename)
        delete_file(path)

        self._msg("logfile", path)
        logger = create_log(logname, logfile=path)
        msg = "this is a test log message"
        logger.debug(msg)

        regexp = re.compile(r"^.*{0} - DEBUG - {1}$".format(logname, msg), re.I)
        data = get_file_contents(path)
        for lineno, line in enumerate(data.splitlines()):
            matched = regexp.match(line)
            if matched:
                self._msg("match: {0}".format(lineno), matched.group(0))
            self.assertTrue(matched)

        delete_file(path)
Beispiel #2
0
    def test_create_thumb(self):
        """
        create_thumb should generate a thumbnail of specified size and
        quality.
        * result should be, e.g., /path/to/thumb/thumb-245x136.jpg
        """
        from garage.image_utils import (
            create_thumb,
            get_image_size,
            get_img_ext,
        )
        from garage.utils import delete_file
        self._msg('test', 'create_thumb', first=True)

        tempdir = tempfile.gettempdir()
        path, fname, w, h, ext = self._get_test_image('rodents')
        w, h = 245, 138
        quality = 75
        dst = tempdir
        fbase = 'rodents'
        thumb = create_thumb(path, w, h, quality, dst, fbase, ext)
        width, height = get_image_size(thumb)
        fext = get_img_ext(thumb)
        self.assertTrue(os.path.isfile(thumb))
        self.assertEqual((width, height), (w, h))
        self.assertEqual(fext, ext)
        self._msg('original', fname)
        self._msg('thumb', thumb)
        self._msg('width', width)
        self._msg('height', height)
        self._msg('ext', fext)
        self.assertTrue(delete_file(thumb))
Beispiel #3
0
    def test_resize_image(self):
        """
        resize_image should downsample an image and resize according
        to input dimensions.
        """
        from garage.image_utils import (
            resize_image,
            get_img_ext,
            get_file_basename,
            get_image_size,
        )
        from garage.utils import delete_file
        self._msg('test', 'resize_image', first=True)

        tempdir = tempfile.gettempdir()
        path, fname, w, h, ext = self._get_test_image('rodents')
        fbase = 'rodents'
        w, h = 500, 280
        dst = '%s-%dx%d.%s' % (os.path.join(tempdir, fbase), w, h, ext)
        img = Image.open(path)
        result = resize_image(img, (w, h), True)
        result.save(dst, quality=75)
        self.assertTrue(os.path.isfile(dst))
        width, height = get_image_size(dst)
        fext = get_img_ext(dst)
        self.assertEqual((width, height), (w, h))
        self.assertEqual(fext, ext)
        self._msg('original', dst)
        self._msg('resized', result)
        self._msg('width', width)
        self._msg('height', height)
        self._msg('ext', fext)
        self.assertTrue(delete_file(dst))
Beispiel #4
0
    def test_generate_thumb(self):
        """
        generate_thumb is a wrapper function for create_thumb, so
        should bahave identically with the same parameters.
        * result should be, e.g., /path/to/thumb/thumb-245x136.jpg
        """
        from garage.image_utils import (
            generate_thumb,
            get_image_size,
            get_img_ext,
        )
        from garage.utils import delete_file
        self._msg('test', 'generate_thumb', first=True)

        tempdir = tempfile.gettempdir()
        path, fname, w, h, ext = self._get_test_image('rodents')
        w, h = 245, 138
        quality = 75
        dst = tempdir
        thumb = generate_thumb(path, w, h, quality, dst)
        width, height = get_image_size(thumb)
        fext = get_img_ext(thumb)
        self.assertTrue(os.path.isfile(thumb))
        self.assertEqual((width, height), (w, h))
        self.assertEqual(fext, ext)
        self._msg('original', fname)
        self._msg('thumb', thumb)
        self._msg('width', width)
        self._msg('height', height)
        self._msg('ext', fext)
        self.assertTrue(delete_file(thumb))
Beispiel #5
0
    def test_delete_nonexistent_file(self):
        """
        delete_file should bypass deletion and return True if file
        does not exist.
        """
        from garage.utils import write_file, get_file_contents, delete_file

        self._msg("test", "delete non-existent file", first=True)
        path = "garage-non-existent-testfile"
        result = delete_file(path)
        self.assertTrue(result)
        self.assertFalse(os.path.isfile(path))
Beispiel #6
0
    def test_delete_file(self):
        """
        Ensure delete_file function is working properly.
        """
        from garage.utils import write_file, get_file_contents, delete_file

        self._msg("test", "delete_file", first=True)
        filename = "garage-testfile"
        tmpfile_dir = tempfile.gettempdir()
        path = os.path.join(tmpfile_dir, filename)
        result = write_file(path, TestData)
        self.assertTrue(result)
        # chect contents
        data = get_file_contents(path)
        self._msg("path", path)
        self._msg("data", data, linebreak=True)
        self.assertEqual(data, TestData)
        result = delete_file(path)
        self.assertTrue(result)
        self.assertFalse(os.path.isfile(path))