"""Unit tests for errors module.""" from src.enums import BatchErrorCode from src.errors import ( ERROR_CODE_MAP, ChecksumMismatchError, EmptyFileError, FileIntegrityError, FileParsingError, FileSizeExceededError, FileSystemError, InvalidFileTypeError, MissingHeadersError, PermanentError, RowCountExceededError, S3FileNotFoundError, TransientError, UpdateBatchError, get_error_code, ) class TestErrorInheritance: """Tests for error class inheritance.""" def test_permanent_error_is_exception(self): """Test PermanentError inherits from Exception.""" error = PermanentError('test error') assert isinstance(error, Exception) def test_transient_error_is_exception(self): """Test TransientError inherits from Exception.""" error = TransientError('test error') assert isinstance(error, Exception) def test_specific_errors_inherit_from_permanent_error(self): """Test specific error classes inherit from PermanentError.""" error_classes = [ EmptyFileError, RowCountExceededError, FileParsingError, FileSizeExceededError, InvalidFileTypeError, MissingHeadersError, S3FileNotFoundError, UpdateBatchError, ] for error_class in error_classes: error = error_class('test error') assert isinstance(error, PermanentError) assert isinstance(error, Exception) class TestGetErrorCode: """Tests for get_error_code function.""" def test_get_error_code_for_invalid_file_type(self): """Test get_error_code returns INVALID_FILE_TYPE for InvalidFileTypeError.""" error = InvalidFileTypeError('Invalid file type') result = get_error_code(error) assert result == BatchErrorCode.INVALID_FILE_TYPE def test_get_error_code_for_file_parsing_error(self): """Test get_error_code returns FILE_PARSING_ERROR for FileParsingError.""" error = FileParsingError('Failed to parse file') result = get_error_code(error) assert result == BatchErrorCode.FILE_PARSING_ERROR def test_get_error_code_for_missing_headers(self): """Test get_error_code returns MISSING_HEADERS for MissingHeadersError.""" error = MissingHeadersError('Required headers missing') result = get_error_code(error) assert result == BatchErrorCode.MISSING_HEADERS def test_get_error_code_for_empty_file(self): """Test get_error_code returns EMPTY_FILE for EmptyFileError.""" error = EmptyFileError('File is empty') result = get_error_code(error) assert result == BatchErrorCode.EMPTY_FILE def test_get_error_code_for_row_count_exceeded(self): """Test get_error_code returns ROW_COUNT_EXCEEDED for RowCountExceededError.""" error = RowCountExceededError('Too many rows') result = get_error_code(error) assert result == BatchErrorCode.ROW_COUNT_EXCEEDED def test_get_error_code_for_batch_state_error(self): """Test get_error_code returns BATCH_STATE_ERROR for UpdateBatchError.""" error = UpdateBatchError('Invalid batch state') result = get_error_code(error) assert result == BatchErrorCode.BATCH_STATE_ERROR def test_get_error_code_for_s3_object_not_found(self): """Test get_error_code returns S3_OBJECT_NOT_FOUND for S3ObjectNotFoundError.""" error = S3FileNotFoundError('S3 object not found') result = get_error_code(error) assert result == BatchErrorCode.FILE_NOT_FOUND def test_get_error_code_for_unknown_error(self): """Test get_error_code returns UNKNOWN_ERROR for unmapped exceptions.""" error = ValueError('Some other error') result = get_error_code(error) assert result == BatchErrorCode.UNKNOWN_ERROR def test_get_error_code_for_generic_exception(self): """Test get_error_code returns UNKNOWN_ERROR for generic Exception.""" error = Exception('Generic error') result = get_error_code(error) assert result == BatchErrorCode.UNKNOWN_ERROR def test_get_error_code_for_transient_error(self): """Test get_error_code returns UNKNOWN_ERROR for TransientError (not in map).""" error = TransientError('Network error') result = get_error_code(error) assert result == BatchErrorCode.UNKNOWN_ERROR def test_get_error_code_for_permanent_error(self): """Test get_error_code returns UNKNOWN_ERROR for base PermanentError (not in map).""" error = PermanentError('Generic permanent error') result = get_error_code(error) assert result == BatchErrorCode.UNKNOWN_ERROR class TestErrorCodeMap: """Tests for ERROR_CODE_MAP.""" def test_error_code_map_contains_all_permanent_errors(self): """Test ERROR_CODE_MAP contains mappings for all specific permanent errors.""" expected_mappings = { ChecksumMismatchError: BatchErrorCode.CHECKSUM_MISMATCH, EmptyFileError: BatchErrorCode.EMPTY_FILE, S3FileNotFoundError: BatchErrorCode.FILE_NOT_FOUND, FileParsingError: BatchErrorCode.FILE_PARSING_ERROR, FileSizeExceededError: BatchErrorCode.FILE_SIZE_EXCEEDED, FileIntegrityError: BatchErrorCode.FILE_INTEGRITY_ERROR, FileSystemError: BatchErrorCode.FILE_SYSTEM_ERROR, InvalidFileTypeError: BatchErrorCode.INVALID_FILE_TYPE, MissingHeadersError: BatchErrorCode.MISSING_HEADERS, RowCountExceededError: BatchErrorCode.ROW_COUNT_EXCEEDED, UpdateBatchError: BatchErrorCode.BATCH_STATE_ERROR, } assert ERROR_CODE_MAP == expected_mappings def test_error_code_map_has_correct_size(self): """Test ERROR_CODE_MAP has the expected number of entries.""" assert len(ERROR_CODE_MAP) == 11