"""Tests for model-related error handlers.""" from oto import response as oto_response from oto import status from sqlalchemy import exc from label_copy_export.models import error_handlers def test_integrity_error_handler(): """Assert decorator handles sqlalchemy.exc.IntegrityError.""" @error_handlers.integrity_error_handler(error_code='custom_error_code') def mock_function(): raise exc.IntegrityError(statement='test', params={}, orig='test') response = mock_function() assert isinstance(response, oto_response.Response) assert response.status == status.BAD_REQUEST assert response.errors['code'] == 'custom_error_code' def test_sqlalchemy_error_handler(mocker): """Assert decorator handles sqlalchemy.exc.SQLAlchemyError.""" mock_sentry_client = mocker.patch( 'label_copy_export.models.error_handlers.sentry.sentry_client') @error_handlers.sqlalchemy_error_handler def mock_function(): raise exc.SQLAlchemyError response = mock_function() assert isinstance(response, oto_response.Response) assert response.status == status.INTERNAL_ERROR assert mock_sentry_client.captureException.called is True