"""Tests for image_validator module.""" from unittest.mock import call from unittest.mock import MagicMock import numpy as np import pytest from PIL import Image from constants import errors from constants import image_standards from src import image_validator @pytest.mark.parametrize( ('width', 'height', 'expected_result'), [ (3500, 4500, errors.IMAGE_WRONG_ASPECT_RATIO_CODE), (1500, 1500, errors.IMAGE_TOO_SMALL_DIMENSIONS_CODE), (3500, 3500, image_standards.VALID_DIMENSIONS_CODE), (6500, 6500, errors.IMAGE_TOO_LARGE_DIMENSIONS_CODE), ] ) def test_validate_dimensions( width, height, expected_result ): """Test image dimensions validation.""" result = image_validator.validate_dimensions( width=width, height=height, ) assert result == expected_result @pytest.mark.parametrize(( 'description', 'image', 'expected_convert_calls', 'expected_result', ), [ ( 'RGB mode', MagicMock(mode='RGB'), [], True, ), ( 'RGBA mode with transparency used', Image.fromarray(np.full((100, 100, 4), [0, 0, 0, 254], dtype=np.uint8), mode='RGBA'), [], False, ), ( 'RGBA mode with transparency not used', Image.fromarray(np.full((100, 100, 4), [0, 0, 0, 255], dtype=np.uint8), mode='RGBA'), [], True, ), ( 'LA mode with transparency used', Image.fromarray(np.full((100, 100, 2), [0, 254], dtype=np.uint8), mode='LA'), [call('RGBA')], False, ), ( 'PA mode with transparency used', Image.fromarray(np.full((100, 100, 2), [0, 254], dtype=np.uint8), mode='PA'), [call('RGBA')], False, ), ]) def test_validate_is_opaque( description, image, expected_convert_calls, expected_result, ): """Test validate_is_opaque.""" image.convert = MagicMock(wraps=image.convert) result = image_validator.validate_is_opaque(image) assert image.convert.mock_calls == expected_convert_calls assert result == expected_result @pytest.mark.parametrize('mode,expected_result', [ ('RGB', True), ('RGBX', True), ('RGBA', True), ('L', True), ('LA', True), ('P', True), ('PA', True), ('1', True), ('CMYK', False), ('beast mode', False) ]) def test_validate_mode(mode, expected_result): """Test that valid modes pass validation and invalid modes fail.""" assert image_validator.validate_mode(mode) is expected_result