You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.5 KiB
Python

import pytest
from sqlalchemy.exc import IntegrityError, DataError, DBAPIError
from src.exceptions import handle_sqlalchemy_error
def test_handle_sqlalchemy_error_unique_constraint():
# Mocking IntegrityError is tricky, but we can check the string matching logic
# In a real test we might want to actually raise these, but for unit testing the logic:
err = IntegrityError("Unique constraint", params=None, orig=Exception("unique constraint violation"))
msg, status = handle_sqlalchemy_error(err)
assert status == 409
assert "already exists" in msg
def test_handle_sqlalchemy_error_foreign_key():
err = IntegrityError("Foreign key constraint", params=None, orig=Exception("foreign key constraint violation"))
msg, status = handle_sqlalchemy_error(err)
assert status == 400
assert "Related record not found" in msg
def test_handle_sqlalchemy_error_data_error():
err = DataError("Invalid data", params=None, orig=None)
msg, status = handle_sqlalchemy_error(err)
assert status == 400
assert "Invalid data" in msg
def test_handle_sqlalchemy_error_generic_dbapi():
# DBAPIError needs an 'orig' or it might fail some attribute checks depending on implementation
# But let's test a generic one
class MockError:
def __str__(self):
return "Some generic database error"
err = DBAPIError("Generic error", params=None, orig=MockError())
msg, status = handle_sqlalchemy_error(err)
assert status == 500
assert "Database error" in msg