Commit b00f82b1 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): cover some more lines

parent f1b6adbd
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ from PIL import Image
from hbutils.testing import tmatrix

from imgutils.preprocess.pillow import PillowResize, _get_pillow_resample, PillowCenterCrop, PillowToTensor, \
    PillowMaybeToTensor, PillowNormalize
    PillowMaybeToTensor, PillowNormalize, create_pillow_transforms
from imgutils.preprocess.torchvision import _get_interpolation_mode
from test.testings import get_testfile

@@ -408,6 +408,27 @@ class TestPreprocessPillow:
        ttotensor = ToTensor()
        np.testing.assert_array_almost_equal(ptotensor(image), ttotensor(image).numpy())

    @skipUnless(_TORCHVISION_AVAILABLE, 'Torchvision required')
    @pytest.mark.parametrize(*tmatrix({
        'src_image': [
            'png_640.png',
            'png_640_m90.png',
        ],
        'mode': [
            'I', 'I;16', 'F',
            '1', 'L', 'LA', 'P',
            'RGB', 'YCbCr', 'RGBA', 'CMYK',
        ]
    }))
    def test_to_tensor(self, src_image, mode):
        from torchvision.transforms import ToTensor
        image = Image.open(get_testfile(src_image))
        image = image.convert(mode)
        assert image.mode == mode
        ptotensor = create_pillow_transforms({'type': 'to_tensor'})
        ttotensor = ToTensor()
        np.testing.assert_array_almost_equal(ptotensor(image), ttotensor(image).numpy())

    def test_to_tensor_invalid(self):
        ptotensor = PillowToTensor()
        with pytest.raises(TypeError):
+10 −0
Original line number Diff line number Diff line
@@ -215,3 +215,13 @@ PillowCompose(
            ptrans(image),
            ttrans(image).numpy(),
        )


    def test_create_transform_invalid(self):
        with pytest.raises(TypeError):
            _ = create_pillow_transforms(None)
        with pytest.raises(TypeError):
            _ = create_pillow_transforms(1)
        with pytest.raises(TypeError):
            _ = create_pillow_transforms('str')
+15 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ class TestPreprocessPillow:
        with pytest.raises(ValueError):
            _ = _get_interpolation_mode('')

    @skipUnless(_TORCHVISION_AVAILABLE, 'Torchvision required.')
    def test_get_interpolation_mode_invalid(self):
        with pytest.raises(TypeError):
            _ = _get_interpolation_mode(None)
@@ -117,3 +118,17 @@ class TestPreprocessPillow:
        ttrans = create_torchvision_transforms({'type': 'to_tensor'})
        result = ttrans(image)
        assert tuple(result.shape) == (channels, image.height, image.width)

    @skipUnless(_TORCHVISION_AVAILABLE, 'Torchvision required.')
    def test_create_transform_invalid(self):
        with pytest.raises(TypeError):
            _ = create_torchvision_transforms(None)
        with pytest.raises(TypeError):
            _ = create_torchvision_transforms(1)
        with pytest.raises(TypeError):
            _ = create_torchvision_transforms('str')

    @skipUnless(not _TORCHVISION_AVAILABLE, 'Non-torchvision required.')
    def test_create_transform_non_torchvision(self):
        with pytest.raises(EnvironmentError):
            _ = create_torchvision_transforms([])