예제 #1
0
def test_svgfile():
    """
    SvgFile should have "name" attribute, "url" attribute correct values and
    "exists" method correct response.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(
        storage,
        format_name="SVG",
    )
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    svg = SvgFile(image)

    assert svg.name == image.name
    assert svg.exists() == True

    # Ensure url starts like a url and ends with the file name without to test
    # the full url matching
    assert len(svg.url) > 0
    assert svg.url.startswith("/")
    assert svg.url.endswith(image.name)
def test_format_incompatible_svg_to_bitmap(db):
    """
    When required format is a Bitmap for a SVG source, it should raise an
    exception since PIL is not able to read SVG.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(
        storage,
        format_name="SVG",
    )
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry format=format %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
        "format": "PNG",
    }

    # Render template with tag usage
    with pytest.raises(IncompatibleSvgToBitmap):
        result = t.render(Context(c))
def test_format_forced_jpeg(db):
    """
    When a specific Bitmap format is given, it should be respected no matter
    the source format is.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(
        storage,
        format_name="PNG",
    )
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry format=format %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
        "format": "JPEG",
    }

    # Render template with tag usage
    result = t.render(Context(c))
    thumb_filepath = storage.path(result.strip())

    assert os.path.exists(thumb_filepath)

    # Open created image file with PIL for validation
    with Image.open(thumb_filepath) as im:
        assert im.format == "JPEG"
def test_format_invalid_format_name(db):
    """
    When given format name is not supported, it should raise an exception.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(
        storage,
        format_name="PNG",
    )
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry format=format %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
        "format": "NOPE",
    }

    # Render template with tag usage
    with pytest.raises(InvalidFormatError):
        result = t.render(Context(c))
def test_basic(db):
    """
    Basic usage should create a thumb with default format and expected size
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(storage)
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
    }

    # Render template with tag usage
    result = t.render(Context(c))
    thumb_filepath = storage.path(result.strip())

    # Fail because the File object given to sorl have the initial required file
    # name which is not the final filename (which have additional unique hash)
    assert os.path.exists(thumb_filepath)

    # Open created image file with PIL for validation
    with Image.open(thumb_filepath) as im:
        assert im.format == "PNG"
        assert im.size == (50, 50)
def test_format_auto_bitmap(db, expected):
    """
    Default auto format for a Bitmap should use the same format than source.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(
        storage,
        format_name=expected,
    )
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
    }

    # Render template with tag usage
    result = t.render(Context(c))
    thumb_filepath = storage.path(result.strip())

    assert os.path.exists(thumb_filepath)

    # Open created image file with PIL for validation
    with Image.open(thumb_filepath) as im:
        assert im.format == expected
def test_bitmap_url(db):
    """
    Bitmap thumb url should be a valid attribute with correct url
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(storage)
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    # We render a basic JSON string to return some values
    t = Template(
        ("""{% load smart_format %}"""
         """{% media_thumb myfileobject geometry as mythumb %}"""
         """{"name": "{{ mythumb }}", "url": "{{ mythumb.url }}" }"""))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
    }

    # Render template with tag usage
    result = t.render(Context(c))

    # Decode JSON string
    datas = json.loads(result.strip())

    print("result:", result)
    print(datas)

    thumb_filepath = storage.path(datas["name"])
    thumb_url = storage.url(datas["name"])

    print("thumb_filepath:", thumb_filepath)
    print("thumb_url:", thumb_url)

    # Thumb file does exists and correspond to the thumb url
    assert os.path.exists(thumb_filepath)
    assert len(datas["url"]) > 0
    assert thumb_url == datas["url"]
def test_assignation(db):
    """
    Basic usage with assignation (with "as" keyword)
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(storage)
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(
        ("{% load smart_format %}"
         "{% media_thumb myfileobject geometry as mythumb %}[{{ mythumb }}]"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
    }

    # Render template with tag usage
    result = t.render(Context(c))

    # Ensure assignment is correctly done in variable which we surrounded with
    # dummy brackets for distinction purposes
    assert result.startswith("[")
    assert result.endswith("]")

    # Get thumb url from value inside brackets
    thumb_filepath = storage.path(result.strip()[1:-1])

    # Fail because the File object given to sorl have the initial required file
    # name which is not the final filename (which have additional unique hash)
    assert os.path.exists(thumb_filepath)

    # Open created image file with PIL for validation
    with Image.open(thumb_filepath) as im:
        assert im.format == "PNG"
        assert im.size == (50, 50)
def test_format_auto_svg(db):
    """
    Automatic format for SVG should be correctly detected and just return the
    source since we don't create thumb for this format.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(
        storage,
        format_name="SVG",
    )
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "50x50",
    }

    # Render template with tag usage
    result = t.render(Context(c))
    thumb_filepath = storage.path(result.strip())

    assert os.path.exists(thumb_filepath)

    # Thumb object is the source file object
    assert thumb_filepath == saved_source

    # Ensure this is a svg file
    with io.open(thumb_filepath, "r") as fp:
        content = fp.read()

    assert content.startswith("<svg")
def test_upscale_disabled(db):
    """
    Usage "upscale" option to False should not upscale a source smaller than
    required geometry.
    """
    storage = get_storage_class()()

    # Create a dummy source image file
    image = get_test_source(storage, size=(50, 50))
    saved_source = storage.path(image.name)

    assert os.path.exists(saved_source)

    # Template to use tag with dummy image
    t = Template(("{% load smart_format %}"
                  "{% media_thumb myfileobject geometry upscale=upscale %}"))
    # Template context to pass tag arguments
    c = {
        "myfileobject": image,
        "geometry": "100x100",
        "upscale": False,
    }

    # Render template with tag usage
    result = t.render(Context(c))
    thumb_filepath = storage.path(result.strip())

    # Fail because the File object given to sorl have the initial required file
    # name which is not the final filename (which have additional unique hash)
    assert os.path.exists(thumb_filepath)

    # Open created image file with PIL for validation
    with Image.open(thumb_filepath) as im:
        assert im.format == "PNG"
        # Original source size is respected
        assert im.size == (50, 50)