Exemplo n.º 1
0
def get_test_source(storage, *args, **kwargs):
    """
    Shortcut helper to create a dummy image, save it to the FS and get its
    final path from storage.

    Arguments:
        storage (django.core.files.storage.Storage): Storage class to use to
            save file to the file system.

    Keyword Arguments:
        storage (django.core.files.storage.Storage): Storage class to use to
            save file to the file system. This is a required argument.
        destination_dir (string): relative directory path where to save file
            in storage directory. Default to "pil".
        *args: Any other positionnal arguments are passed to
            ``create_image_file``.
        **kwargs: Any other Keyword arguments are passed to
            ``create_image_file``.

    Returns:
        django.core.files.File: File object with the right final file path.
    """
    destination_dir = kwargs.pop("destination_dir", "pil")

    image = create_image_file(*args, **kwargs)

    destination = os.path.join(destination_dir, image.name)
    source_filepath = storage.save(destination, image)

    # Trick to update name to final destination since File object is not
    # automatically updated to the final filepath during storage.
    image.name = source_filepath

    return image
Exemplo n.º 2
0
def test_create_image_file_args(caplog, testsettings):
    """
    Should succeed with a created file as expected from given arguments.
    """
    storage = get_storage_class()()

    image = create_image_file(filename="foo.red",
                              size=(200, 200),
                              color="red",
                              format_name="PNG")

    destination = os.path.join("pil", image.name)

    assert image.name == "foo.red"

    storage.save(destination, image)
    saved = storage.path(destination)

    assert os.path.exists(saved) == True

    try:
        with Image.open(saved) as im:
            # Since image are just single plain color we can pick any pixel to
            # verify expected color
            pixel_sample = im.getpixel((10, 10))

            assert im.format == "PNG"
            assert im.size == (200, 200)
            assert pixel_sample == (255, 0, 0, 255)
    except IOError:
        print("Cannot open image file with PIL:", saved)
        raise
Exemplo n.º 3
0
def test_create_image_file_svg(caplog, testsettings):
    """
    SVG format should create a dummy SVG file.
    """
    storage = get_storage_class()()

    image = create_image_file(
        format_name="SVG",
        color="green",
        size=(150, 80),
    )

    destination = os.path.join("pil", image.name)

    assert image.name == "green.svg"

    storage.save(destination, image)
    saved = storage.path(destination)

    assert os.path.exists(saved) == True

    with io.open(saved) as fp:
        content = fp.read()

    expected = (
        """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 80">"""
        """<path fill="green" d="M0 0h150v80H0z"/>"""
        """</svg>""")

    assert content == expected
Exemplo n.º 4
0
def test_create_image_file_basic(caplog, testsettings):
    """
    Basic usage without arguments should succeed with a created file as
    expected from default argument values.
    """
    storage = get_storage_class()()

    image = create_image_file()

    destination = os.path.join("pil", image.name)

    assert image.name == "blue.png"

    storage.save(destination, image)
    saved = storage.path(destination)

    assert os.path.exists(saved) == True

    try:
        with Image.open(saved) as im:
            # Since image are just single plain color we can pick any pixel to
            # verify expected color
            pixel_sample = im.getpixel((10, 10))

            assert im.format == "PNG"
            assert im.size == (100, 100)
            assert pixel_sample == (0, 0, 255, 255)
    except IOError:
        print("Cannot open image file with PIL:", saved)
        raise
Exemplo n.º 5
0
    def image(self):
        """
        Fill file field with generated image.

        Returns:
            django.core.files.File: File object.
        """

        return create_image_file()
Exemplo n.º 6
0
def test_create_image_file_jpg(caplog, testsettings):
    """
    Created image should be in the required format from argument.
    """
    storage = get_storage_class()()

    image = create_image_file(format_name="JPEG")

    destination = os.path.join("pil", image.name)

    assert image.name == "blue.jpg"

    storage.save(destination, image)
    saved = storage.path(destination)

    assert os.path.exists(saved) == True

    try:
        with Image.open(saved) as im:
            assert im.format == "JPEG"
    except IOError:
        print("Cannot open image file with PIL:", saved)
        raise