Admin service tests update
This commit is contained in:
54
tests/fixtures/testing_fixtures.py
vendored
Normal file
54
tests/fixtures/testing_fixtures.py
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
# tests/fixtures/testing_fixtures.py
|
||||
"""
|
||||
Testing utility fixtures for edge cases and error handling.
|
||||
|
||||
This module provides fixtures for:
|
||||
- Empty database sessions for edge case testing
|
||||
- Mock database sessions for error simulation
|
||||
- Additional testing utilities
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def empty_db(db):
|
||||
"""Empty database session for edge case testing"""
|
||||
from sqlalchemy import text
|
||||
|
||||
# Clear only the tables that are relevant for admin service testing
|
||||
# In order to respect foreign key constraints
|
||||
tables_to_clear = [
|
||||
"marketplace_import_jobs", # Has foreign keys to shops and users
|
||||
"shop_products", # Has foreign keys to shops and products
|
||||
"stock", # Fixed: singular not plural
|
||||
"products", # Referenced by shop_products
|
||||
"shops", # Has foreign key to users
|
||||
"users" # Base table
|
||||
]
|
||||
|
||||
for table in tables_to_clear:
|
||||
try:
|
||||
db.execute(text(f"DELETE FROM {table}"))
|
||||
except Exception:
|
||||
# If table doesn't exist or delete fails, continue
|
||||
pass
|
||||
|
||||
db.commit()
|
||||
return db
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_with_error():
|
||||
"""Database session that raises errors for testing error handling"""
|
||||
mock_db = Mock()
|
||||
|
||||
# Configure the mock to raise SQLAlchemy errors on query operations
|
||||
mock_db.query.side_effect = SQLAlchemyError("Database connection failed")
|
||||
mock_db.add.side_effect = SQLAlchemyError("Database insert failed")
|
||||
mock_db.commit.side_effect = SQLAlchemyError("Database commit failed")
|
||||
mock_db.rollback.return_value = None
|
||||
|
||||
return mock_db
|
||||
Reference in New Issue
Block a user