~homeworkprod/byceps

ref: 1181a35dd06d48c779208a6d2a4b3672dd38d812 byceps/tests/util/image/test_image.py -rw-r--r-- 1.2 KiB
1181a35d — Jochen Kupperschmidt Ported tests from nose2 to pytest 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from pathlib import Path

import pytest

from byceps.util.image import read_dimensions
from byceps.util.image.models import Dimensions, ImageType
from byceps.util.image.typeguess import guess_type


@pytest.mark.parametrize('filename_suffix, expected', [
    ('bmp',  None          ),
    ('gif',  ImageType.gif ),
    ('jpeg', ImageType.jpeg),
    ('png',  ImageType.png ),
])
def test_guess_type(filename_suffix, expected):
    with open_image_with_suffix(filename_suffix) as f:
        actual = guess_type(f)

    assert actual == expected


@pytest.mark.parametrize('filename_suffix, expected_width, expected_height', [
    ('bmp',   7, 11),
    ('gif',  17,  4),
    ('jpeg', 12,  7),
    ('png',   8, 25),
])
def test_read_dimensions(filename_suffix, expected_width, expected_height):
    expected = Dimensions(width=expected_width, height=expected_height)

    with open_image_with_suffix(filename_suffix) as f:
        actual = read_dimensions(f)

    assert actual == expected


def open_image_with_suffix(suffix):
    path = Path('testfixtures/images/image').with_suffix('.' + suffix)
    return path.open(mode='rb')