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.
32 lines
1.2 KiB
Python
32 lines
1.2 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():
|
|
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():
|
|
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
|