Pillow

Форк
0
/
test_file_msp.py 
91 строка · 2.3 Кб
1
from __future__ import annotations
2

3
import os
4
from pathlib import Path
5

6
import pytest
7

8
from PIL import Image, MspImagePlugin
9

10
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
11

12
TEST_FILE = "Tests/images/hopper.msp"
13
EXTRA_DIR = "Tests/images/picins"
14
YA_EXTRA_DIR = "Tests/images/msp"
15

16

17
def test_sanity(tmp_path: Path) -> None:
18
    test_file = str(tmp_path / "temp.msp")
19

20
    hopper("1").save(test_file)
21

22
    with Image.open(test_file) as im:
23
        im.load()
24
        assert im.mode == "1"
25
        assert im.size == (128, 128)
26
        assert im.format == "MSP"
27

28

29
def test_invalid_file() -> None:
30
    invalid_file = "Tests/images/flower.jpg"
31

32
    with pytest.raises(SyntaxError):
33
        MspImagePlugin.MspImageFile(invalid_file)
34

35

36
def test_bad_checksum() -> None:
37
    # Arrange
38
    # This was created by forcing Pillow to save with checksum=0
39
    bad_checksum = "Tests/images/hopper_bad_checksum.msp"
40

41
    # Act / Assert
42
    with pytest.raises(SyntaxError):
43
        MspImagePlugin.MspImageFile(bad_checksum)
44

45

46
def test_open_windows_v1() -> None:
47
    # Arrange
48
    # Act
49
    with Image.open(TEST_FILE) as im:
50
        # Assert
51
        assert_image_equal(im, hopper("1"))
52
        assert isinstance(im, MspImagePlugin.MspImageFile)
53

54

55
def _assert_file_image_equal(source_path: str, target_path: str) -> None:
56
    with Image.open(source_path) as im:
57
        assert_image_equal_tofile(im, target_path)
58

59

60
@pytest.mark.skipif(
61
    not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
62
)
63
def test_open_windows_v2() -> None:
64
    files = (
65
        os.path.join(EXTRA_DIR, f)
66
        for f in os.listdir(EXTRA_DIR)
67
        if os.path.splitext(f)[1] == ".msp"
68
    )
69
    for path in files:
70
        _assert_file_image_equal(path, path.replace(".msp", ".png"))
71

72

73
@pytest.mark.skipif(
74
    not os.path.exists(YA_EXTRA_DIR), reason="Even More Extra image files not installed"
75
)
76
def test_msp_v2() -> None:
77
    for f in os.listdir(YA_EXTRA_DIR):
78
        if ".MSP" not in f:
79
            continue
80
        path = os.path.join(YA_EXTRA_DIR, f)
81
        _assert_file_image_equal(path, path.replace(".MSP", ".png"))
82

83

84
def test_cannot_save_wrong_mode(tmp_path: Path) -> None:
85
    # Arrange
86
    im = hopper()
87
    filename = str(tmp_path / "temp.msp")
88

89
    # Act/Assert
90
    with pytest.raises(OSError):
91
        im.save(filename)
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.