From 9d6d16d5cc7bcf6070f5ad002de8642cbb2a17ab Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 6 Aug 2024 15:19:40 +0300 Subject: [PATCH 1/4] Parametrize some color_lut tests for DRYness --- Tests/test_color_lut.py | 131 +++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 77 deletions(-) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index 0d9c0b41974..152a7a1e60d 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -105,91 +105,68 @@ def test_wrong_args(self) -> None: with pytest.raises(TypeError): im.im.color_lut_3d("RGB", Image.Resampling.BILINEAR, 3, 2, 2, 2, 16) - def test_correct_args(self) -> None: + @pytest.mark.parametrize( + ("lut_mode", "table_size"), + [ + ("RGB", (3, 3)), + ("CMYK", (4, 3)), + ("RGB", (3, (2, 3, 3))), + ("RGB", (3, (65, 3, 3))), + ("RGB", (3, (3, 65, 3))), + ("RGB", (3, (3, 3, 65))), + ], + ) + def test_correct_args( + self, lut_mode: str, table_size: tuple[int, int | tuple[int, int, int]] + ) -> None: im = Image.new("RGB", (10, 10), 0) - - im.im.color_lut_3d( - "RGB", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - - im.im.color_lut_3d( - "CMYK", Image.Resampling.BILINEAR, *self.generate_identity_table(4, 3) - ) - + assert im.im is not None im.im.color_lut_3d( - "RGB", + lut_mode, Image.Resampling.BILINEAR, - *self.generate_identity_table(3, (2, 3, 3)), - ) - - im.im.color_lut_3d( - "RGB", - Image.Resampling.BILINEAR, - *self.generate_identity_table(3, (65, 3, 3)), - ) - - im.im.color_lut_3d( - "RGB", - Image.Resampling.BILINEAR, - *self.generate_identity_table(3, (3, 65, 3)), - ) - - im.im.color_lut_3d( - "RGB", - Image.Resampling.BILINEAR, - *self.generate_identity_table(3, (3, 3, 65)), - ) - - def test_wrong_mode(self) -> None: - with pytest.raises(ValueError, match="wrong mode"): - im = Image.new("L", (10, 10), 0) - im.im.color_lut_3d( - "RGB", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - - with pytest.raises(ValueError, match="wrong mode"): - im = Image.new("RGB", (10, 10), 0) - im.im.color_lut_3d( - "L", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - - with pytest.raises(ValueError, match="wrong mode"): - im = Image.new("L", (10, 10), 0) - im.im.color_lut_3d( - "L", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - - with pytest.raises(ValueError, match="wrong mode"): - im = Image.new("RGB", (10, 10), 0) - im.im.color_lut_3d( - "RGBA", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - + *self.generate_identity_table(*table_size), + ) + + @pytest.mark.parametrize( + ("image_mode", "lut_mode", "table_size"), + [ + ("L", "RGB", (3, 3)), + ("RGB", "L", (3, 3)), + ("L", "L", (3, 3)), + ("RGB", "RGBA", (3, 3)), + ("RGB", "RGB", (4, 3)), + ], + ) + def test_wrong_mode( + self, image_mode: str, lut_mode: str, table_size: tuple[int, int] + ) -> None: with pytest.raises(ValueError, match="wrong mode"): - im = Image.new("RGB", (10, 10), 0) + im = Image.new(image_mode, (10, 10), 0) + assert im.im is not None im.im.color_lut_3d( - "RGB", Image.Resampling.BILINEAR, *self.generate_identity_table(4, 3) + lut_mode, + Image.Resampling.BILINEAR, + *self.generate_identity_table(*table_size), ) - def test_correct_mode(self) -> None: - im = Image.new("RGBA", (10, 10), 0) - im.im.color_lut_3d( - "RGBA", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - - im = Image.new("RGBA", (10, 10), 0) + @pytest.mark.parametrize( + ("image_mode", "lut_mode", "table_size"), + [ + ("RGBA", "RGB", (3, 3)), + ("RGBA", "RGBA", (4, 3)), + ("RGB", "HSV", (3, 3)), + ("RGB", "RGBA", (4, 3)), + ], + ) + def test_correct_mode( + self, image_mode: str, lut_mode: str, table_size: tuple[int, int] + ) -> None: + im = Image.new(image_mode, (10, 10), 0) + assert im.im is not None im.im.color_lut_3d( - "RGBA", Image.Resampling.BILINEAR, *self.generate_identity_table(4, 3) - ) - - im = Image.new("RGB", (10, 10), 0) - im.im.color_lut_3d( - "HSV", Image.Resampling.BILINEAR, *self.generate_identity_table(3, 3) - ) - - im = Image.new("RGB", (10, 10), 0) - im.im.color_lut_3d( - "RGBA", Image.Resampling.BILINEAR, *self.generate_identity_table(4, 3) + lut_mode, + Image.Resampling.BILINEAR, + *self.generate_identity_table(*table_size), ) def test_identities(self) -> None: From 4fddc625f177d8b7cd2120fa2c7880f664e25e67 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 7 Aug 2024 18:26:24 +1000 Subject: [PATCH 2/4] Corrected lut mode --- Tests/test_color_lut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index 152a7a1e60d..1543faf01d2 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -152,7 +152,7 @@ def test_wrong_mode( @pytest.mark.parametrize( ("image_mode", "lut_mode", "table_size"), [ - ("RGBA", "RGB", (3, 3)), + ("RGBA", "RGBA", (3, 3)), ("RGBA", "RGBA", (4, 3)), ("RGB", "HSV", (3, 3)), ("RGB", "RGBA", (4, 3)), From 5c4aeaa3296c748facfafc265e29fbb1c7c8e518 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 7 Aug 2024 18:24:50 +1000 Subject: [PATCH 3/4] Concatenate parameters into single string --- Tests/test_color_lut.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index 1543faf01d2..346ea749a99 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -106,7 +106,7 @@ def test_wrong_args(self) -> None: im.im.color_lut_3d("RGB", Image.Resampling.BILINEAR, 3, 2, 2, 2, 16) @pytest.mark.parametrize( - ("lut_mode", "table_size"), + "lut_mode, table_size", [ ("RGB", (3, 3)), ("CMYK", (4, 3)), @@ -128,7 +128,7 @@ def test_correct_args( ) @pytest.mark.parametrize( - ("image_mode", "lut_mode", "table_size"), + "image_mode, lut_mode, table_size", [ ("L", "RGB", (3, 3)), ("RGB", "L", (3, 3)), @@ -150,7 +150,7 @@ def test_wrong_mode( ) @pytest.mark.parametrize( - ("image_mode", "lut_mode", "table_size"), + "image_mode, lut_mode, table_size", [ ("RGBA", "RGBA", (3, 3)), ("RGBA", "RGBA", (4, 3)), From a06529a3a89d3e57e584b85c25fa24cf075f4dcd Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 7 Aug 2024 18:28:51 +1000 Subject: [PATCH 4/4] Added channels parameter --- Tests/test_color_lut.py | 48 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index 346ea749a99..084cb19e137 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -106,39 +106,39 @@ def test_wrong_args(self) -> None: im.im.color_lut_3d("RGB", Image.Resampling.BILINEAR, 3, 2, 2, 2, 16) @pytest.mark.parametrize( - "lut_mode, table_size", + "lut_mode, table_channels, table_size", [ - ("RGB", (3, 3)), - ("CMYK", (4, 3)), - ("RGB", (3, (2, 3, 3))), - ("RGB", (3, (65, 3, 3))), - ("RGB", (3, (3, 65, 3))), - ("RGB", (3, (3, 3, 65))), + ("RGB", 3, 3), + ("CMYK", 4, 3), + ("RGB", 3, (2, 3, 3)), + ("RGB", 3, (65, 3, 3)), + ("RGB", 3, (3, 65, 3)), + ("RGB", 3, (2, 3, 65)), ], ) def test_correct_args( - self, lut_mode: str, table_size: tuple[int, int | tuple[int, int, int]] + self, lut_mode: str, table_channels: int, table_size: int | tuple[int, int, int] ) -> None: im = Image.new("RGB", (10, 10), 0) assert im.im is not None im.im.color_lut_3d( lut_mode, Image.Resampling.BILINEAR, - *self.generate_identity_table(*table_size), + *self.generate_identity_table(table_channels, table_size), ) @pytest.mark.parametrize( - "image_mode, lut_mode, table_size", + "image_mode, lut_mode, table_channels, table_size", [ - ("L", "RGB", (3, 3)), - ("RGB", "L", (3, 3)), - ("L", "L", (3, 3)), - ("RGB", "RGBA", (3, 3)), - ("RGB", "RGB", (4, 3)), + ("L", "RGB", 3, 3), + ("RGB", "L", 3, 3), + ("L", "L", 3, 3), + ("RGB", "RGBA", 3, 3), + ("RGB", "RGB", 4, 3), ], ) def test_wrong_mode( - self, image_mode: str, lut_mode: str, table_size: tuple[int, int] + self, image_mode: str, lut_mode: str, table_channels: int, table_size: int ) -> None: with pytest.raises(ValueError, match="wrong mode"): im = Image.new(image_mode, (10, 10), 0) @@ -146,27 +146,27 @@ def test_wrong_mode( im.im.color_lut_3d( lut_mode, Image.Resampling.BILINEAR, - *self.generate_identity_table(*table_size), + *self.generate_identity_table(table_channels, table_size), ) @pytest.mark.parametrize( - "image_mode, lut_mode, table_size", + "image_mode, lut_mode, table_channels, table_size", [ - ("RGBA", "RGBA", (3, 3)), - ("RGBA", "RGBA", (4, 3)), - ("RGB", "HSV", (3, 3)), - ("RGB", "RGBA", (4, 3)), + ("RGBA", "RGBA", 3, 3), + ("RGBA", "RGBA", 4, 3), + ("RGB", "HSV", 3, 3), + ("RGB", "RGBA", 4, 3), ], ) def test_correct_mode( - self, image_mode: str, lut_mode: str, table_size: tuple[int, int] + self, image_mode: str, lut_mode: str, table_channels: int, table_size: int ) -> None: im = Image.new(image_mode, (10, 10), 0) assert im.im is not None im.im.color_lut_3d( lut_mode, Image.Resampling.BILINEAR, - *self.generate_identity_table(*table_size), + *self.generate_identity_table(table_channels, table_size), ) def test_identities(self) -> None: