chore: PostgreSQL migration compatibility and infrastructure improvements
Database & Migrations: - Update all Alembic migrations for PostgreSQL compatibility - Remove SQLite-specific syntax (AUTOINCREMENT, etc.) - Add database utility helpers for PostgreSQL operations - Fix services to use PostgreSQL-compatible queries Documentation: - Add comprehensive Docker deployment guide - Add production deployment documentation - Add infrastructure architecture documentation - Update database setup guide for PostgreSQL-only - Expand troubleshooting guide Architecture & Validation: - Add migration.yaml rules for SQL compatibility checking - Enhance validate_architecture.py with migration validation - Update architecture rules to validate Alembic migrations Development: - Fix duplicate install-all target in Makefile - Add Celery/Redis validation to install.py script - Add docker-compose.test.yml for CI testing - Add squash_migrations.py utility script - Update tests for PostgreSQL compatibility - Improve test fixtures in conftest.py Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
# tests/conftest.py - Updated main conftest with core fixtures only
|
||||
# tests/conftest.py - PostgreSQL test configuration
|
||||
"""
|
||||
Core pytest configuration and fixtures.
|
||||
|
||||
This project uses PostgreSQL for testing. Start the test database with:
|
||||
make test-db-up
|
||||
|
||||
IMPORTANT - Fixture Best Practices:
|
||||
===================================
|
||||
1. DO NOT use db.expunge() on fixtures - it detaches objects from the session
|
||||
and breaks lazy loading of relationships (e.g., product.marketplace_product).
|
||||
|
||||
2. Test isolation is achieved through the db fixture which drops and recreates
|
||||
all tables after each test - no need to manually detach objects.
|
||||
2. Test isolation is achieved through TRUNCATE CASCADE after each test,
|
||||
which is much faster than dropping/recreating tables.
|
||||
|
||||
3. If you need to ensure an object has fresh data, use db.refresh(obj) instead
|
||||
of expunge/re-query patterns.
|
||||
@@ -19,31 +22,53 @@ IMPORTANT - Fixture Best Practices:
|
||||
See docs/testing/testing-guide.md for comprehensive testing documentation.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.core.database import Base, get_db
|
||||
from main import app
|
||||
|
||||
# Import all models to ensure they're registered with Base metadata
|
||||
|
||||
# Use in-memory SQLite database for tests
|
||||
SQLALCHEMY_TEST_DATABASE_URL = "sqlite:///:memory:"
|
||||
# PostgreSQL test database URL
|
||||
# Use environment variable or default to local Docker test database
|
||||
TEST_DATABASE_URL = os.getenv(
|
||||
"TEST_DATABASE_URL",
|
||||
"postgresql://test_user:test_password@localhost:5433/wizamart_test"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def engine():
|
||||
"""Create test database engine."""
|
||||
return create_engine(
|
||||
SQLALCHEMY_TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
"""Create test database engine.
|
||||
|
||||
Verifies PostgreSQL connection on startup and provides helpful
|
||||
error message if the test database is not running.
|
||||
"""
|
||||
engine = create_engine(
|
||||
TEST_DATABASE_URL,
|
||||
pool_pre_ping=True,
|
||||
echo=False, # Set to True for SQL debugging
|
||||
)
|
||||
|
||||
# Verify connection on startup
|
||||
try:
|
||||
with engine.connect() as conn:
|
||||
conn.execute(text("SELECT 1"))
|
||||
except Exception as e:
|
||||
pytest.exit(
|
||||
f"\n\nCannot connect to test database at {TEST_DATABASE_URL}\n"
|
||||
f"Error: {e}\n\n"
|
||||
"Start the test database with:\n"
|
||||
" make test-db-up\n\n"
|
||||
"Or manually:\n"
|
||||
" docker-compose -f docker-compose.test.yml up -d\n"
|
||||
)
|
||||
|
||||
return engine
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def testing_session_local(engine):
|
||||
@@ -62,32 +87,42 @@ def testing_session_local(engine):
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def setup_database(engine):
|
||||
"""Create all tables once at the start of the test session."""
|
||||
Base.metadata.create_all(bind=engine)
|
||||
yield
|
||||
# Optionally drop tables after all tests (commented out for debugging)
|
||||
# Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db(engine, testing_session_local):
|
||||
"""
|
||||
Create a database session for each test function.
|
||||
|
||||
Provides test isolation by:
|
||||
- Creating fresh tables before each test
|
||||
- Dropping all tables after each test completes
|
||||
- Using a fresh session for each test
|
||||
- Truncating all tables after each test (fast cleanup)
|
||||
|
||||
Note: Fixtures should NOT use db.expunge() as this detaches objects
|
||||
from the session and breaks lazy loading. The table drop/create cycle
|
||||
provides sufficient isolation between tests.
|
||||
from the session and breaks lazy loading. The TRUNCATE provides
|
||||
sufficient isolation between tests.
|
||||
"""
|
||||
# Create all tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Create session
|
||||
db_session = testing_session_local()
|
||||
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
db_session.close()
|
||||
# Clean up all data after each test
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
# Fast cleanup with TRUNCATE CASCADE
|
||||
with engine.connect() as conn:
|
||||
# Disable FK checks temporarily for fast truncation
|
||||
conn.execute(text("SET session_replication_role = 'replica'"))
|
||||
for table in reversed(Base.metadata.sorted_tables):
|
||||
conn.execute(text(f'TRUNCATE TABLE "{table.name}" CASCADE'))
|
||||
conn.execute(text("SET session_replication_role = 'origin'"))
|
||||
conn.commit()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
|
||||
@@ -133,7 +133,8 @@ class TestCustomerAddressModel:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
is_default=True,
|
||||
)
|
||||
|
||||
@@ -158,7 +159,8 @@ class TestCustomerAddressModel:
|
||||
address_line_1="123 Shipping St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
db.add(shipping_address)
|
||||
|
||||
@@ -171,7 +173,8 @@ class TestCustomerAddressModel:
|
||||
address_line_1="456 Billing Ave",
|
||||
city="Luxembourg",
|
||||
postal_code="L-5678",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
db.add(billing_address)
|
||||
db.commit()
|
||||
@@ -192,7 +195,8 @@ class TestCustomerAddressModel:
|
||||
address_line_2="Suite 100",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
db.add(address)
|
||||
db.commit()
|
||||
@@ -212,7 +216,8 @@ class TestCustomerAddressModel:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
db.add(address)
|
||||
db.commit()
|
||||
@@ -231,7 +236,8 @@ class TestCustomerAddressModel:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
db.add(address)
|
||||
db.commit()
|
||||
|
||||
@@ -188,7 +188,8 @@ class TestCustomerAddressCreateSchema:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
assert address.address_type == "shipping"
|
||||
assert address.city == "Luxembourg"
|
||||
@@ -202,7 +203,8 @@ class TestCustomerAddressCreateSchema:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
assert address.address_type == "billing"
|
||||
|
||||
@@ -216,7 +218,8 @@ class TestCustomerAddressCreateSchema:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
assert "address_type" in str(exc_info.value).lower()
|
||||
|
||||
@@ -229,7 +232,8 @@ class TestCustomerAddressCreateSchema:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
assert address.is_default is False
|
||||
|
||||
@@ -243,7 +247,8 @@ class TestCustomerAddressCreateSchema:
|
||||
address_line_1="123 Main St",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
assert address.company == "Tech Corp"
|
||||
|
||||
@@ -257,7 +262,8 @@ class TestCustomerAddressCreateSchema:
|
||||
address_line_2="Apt 4B",
|
||||
city="Luxembourg",
|
||||
postal_code="L-1234",
|
||||
country="Luxembourg",
|
||||
country_name="Luxembourg",
|
||||
country_iso="LU",
|
||||
)
|
||||
assert address.address_line_2 == "Apt 4B"
|
||||
|
||||
@@ -321,7 +327,8 @@ class TestCustomerAddressResponseSchema:
|
||||
"address_line_2": None,
|
||||
"city": "Luxembourg",
|
||||
"postal_code": "L-1234",
|
||||
"country": "Luxembourg",
|
||||
"country_name": "Luxembourg",
|
||||
"country_iso": "LU",
|
||||
"is_default": True,
|
||||
"created_at": datetime.now(),
|
||||
"updated_at": datetime.now(),
|
||||
|
||||
@@ -211,20 +211,23 @@ class TestEmailService:
|
||||
class TestEmailSending:
|
||||
"""Test suite for email sending functionality."""
|
||||
|
||||
@patch("app.services.email_service.get_provider")
|
||||
@patch("app.services.email_service.settings")
|
||||
def test_send_raw_success(self, mock_settings, mock_get_provider, db):
|
||||
@patch("app.services.email_service.get_platform_provider")
|
||||
@patch("app.services.email_service.get_platform_email_config")
|
||||
def test_send_raw_success(self, mock_get_config, mock_get_platform_provider, db):
|
||||
"""Test successful raw email sending."""
|
||||
# Setup mocks
|
||||
mock_settings.email_enabled = True
|
||||
mock_settings.email_from_address = "noreply@test.com"
|
||||
mock_settings.email_from_name = "Test"
|
||||
mock_settings.email_reply_to = ""
|
||||
mock_settings.email_provider = "smtp"
|
||||
mock_get_config.return_value = {
|
||||
"enabled": True,
|
||||
"debug": False,
|
||||
"provider": "smtp",
|
||||
"from_email": "noreply@test.com",
|
||||
"from_name": "Test",
|
||||
"reply_to": "",
|
||||
}
|
||||
|
||||
mock_provider = MagicMock()
|
||||
mock_provider.send.return_value = (True, "msg-123", None)
|
||||
mock_get_provider.return_value = mock_provider
|
||||
mock_get_platform_provider.return_value = mock_provider
|
||||
|
||||
service = EmailService(db)
|
||||
|
||||
@@ -240,20 +243,23 @@ class TestEmailSending:
|
||||
assert log.subject == "Test Subject"
|
||||
assert log.provider_message_id == "msg-123"
|
||||
|
||||
@patch("app.services.email_service.get_provider")
|
||||
@patch("app.services.email_service.settings")
|
||||
def test_send_raw_failure(self, mock_settings, mock_get_provider, db):
|
||||
@patch("app.services.email_service.get_platform_provider")
|
||||
@patch("app.services.email_service.get_platform_email_config")
|
||||
def test_send_raw_failure(self, mock_get_config, mock_get_platform_provider, db):
|
||||
"""Test failed raw email sending."""
|
||||
# Setup mocks
|
||||
mock_settings.email_enabled = True
|
||||
mock_settings.email_from_address = "noreply@test.com"
|
||||
mock_settings.email_from_name = "Test"
|
||||
mock_settings.email_reply_to = ""
|
||||
mock_settings.email_provider = "smtp"
|
||||
mock_get_config.return_value = {
|
||||
"enabled": True,
|
||||
"debug": False,
|
||||
"provider": "smtp",
|
||||
"from_email": "noreply@test.com",
|
||||
"from_name": "Test",
|
||||
"reply_to": "",
|
||||
}
|
||||
|
||||
mock_provider = MagicMock()
|
||||
mock_provider.send.return_value = (False, None, "Connection refused")
|
||||
mock_get_provider.return_value = mock_provider
|
||||
mock_get_platform_provider.return_value = mock_provider
|
||||
|
||||
service = EmailService(db)
|
||||
|
||||
@@ -287,9 +293,9 @@ class TestEmailSending:
|
||||
assert log.status == EmailStatus.FAILED.value
|
||||
assert "disabled" in log.error_message.lower()
|
||||
|
||||
@patch("app.services.email_service.get_provider")
|
||||
@patch("app.services.email_service.settings")
|
||||
def test_send_template_success(self, mock_settings, mock_get_provider, db):
|
||||
@patch("app.services.email_service.get_platform_provider")
|
||||
@patch("app.services.email_service.get_platform_email_config")
|
||||
def test_send_template_success(self, mock_get_config, mock_get_platform_provider, db):
|
||||
"""Test successful template email sending."""
|
||||
# Create test template
|
||||
template = EmailTemplate(
|
||||
@@ -305,15 +311,18 @@ class TestEmailSending:
|
||||
db.commit()
|
||||
|
||||
# Setup mocks
|
||||
mock_settings.email_enabled = True
|
||||
mock_settings.email_from_address = "noreply@test.com"
|
||||
mock_settings.email_from_name = "Test"
|
||||
mock_settings.email_reply_to = ""
|
||||
mock_settings.email_provider = "smtp"
|
||||
mock_get_config.return_value = {
|
||||
"enabled": True,
|
||||
"debug": False,
|
||||
"provider": "smtp",
|
||||
"from_email": "noreply@test.com",
|
||||
"from_name": "Test",
|
||||
"reply_to": "",
|
||||
}
|
||||
|
||||
mock_provider = MagicMock()
|
||||
mock_provider.send.return_value = (True, "msg-456", None)
|
||||
mock_get_provider.return_value = mock_provider
|
||||
mock_get_platform_provider.return_value = mock_provider
|
||||
|
||||
service = EmailService(db)
|
||||
|
||||
@@ -331,7 +340,8 @@ class TestEmailSending:
|
||||
assert log.template_code == "test_send_template"
|
||||
assert log.subject == "Hello John"
|
||||
|
||||
# Cleanup
|
||||
# Cleanup - delete log first due to FK constraint
|
||||
db.delete(log)
|
||||
db.delete(template)
|
||||
db.commit()
|
||||
|
||||
@@ -531,7 +541,8 @@ class TestSignupWelcomeEmail:
|
||||
|
||||
yield template
|
||||
|
||||
# Cleanup
|
||||
# Cleanup - delete email logs referencing this template first
|
||||
db.query(EmailLog).filter(EmailLog.template_id == template.id).delete()
|
||||
db.delete(template)
|
||||
db.commit()
|
||||
|
||||
@@ -575,20 +586,23 @@ class TestSignupWelcomeEmail:
|
||||
for var in required_vars:
|
||||
assert var in template.variables_list, f"Missing variable: {var}"
|
||||
|
||||
@patch("app.services.email_service.get_provider")
|
||||
@patch("app.services.email_service.settings")
|
||||
def test_welcome_email_send(self, mock_settings, mock_get_provider, db, welcome_template):
|
||||
@patch("app.services.email_service.get_platform_provider")
|
||||
@patch("app.services.email_service.get_platform_email_config")
|
||||
def test_welcome_email_send(self, mock_get_config, mock_get_platform_provider, db, welcome_template, test_vendor, test_user):
|
||||
"""Test sending welcome email."""
|
||||
# Setup mocks
|
||||
mock_settings.email_enabled = True
|
||||
mock_settings.email_from_address = "noreply@test.com"
|
||||
mock_settings.email_from_name = "Test"
|
||||
mock_settings.email_reply_to = ""
|
||||
mock_settings.email_provider = "smtp"
|
||||
mock_get_config.return_value = {
|
||||
"enabled": True,
|
||||
"debug": False,
|
||||
"provider": "smtp",
|
||||
"from_email": "noreply@test.com",
|
||||
"from_name": "Test",
|
||||
"reply_to": "",
|
||||
}
|
||||
|
||||
mock_provider = MagicMock()
|
||||
mock_provider.send.return_value = (True, "welcome-msg-123", None)
|
||||
mock_get_provider.return_value = mock_provider
|
||||
mock_get_platform_provider.return_value = mock_provider
|
||||
|
||||
service = EmailService(db)
|
||||
|
||||
@@ -606,8 +620,8 @@ class TestSignupWelcomeEmail:
|
||||
"trial_days": 30,
|
||||
"tier_name": "Essential",
|
||||
},
|
||||
vendor_id=1,
|
||||
user_id=1,
|
||||
vendor_id=test_vendor.id,
|
||||
user_id=test_user.id,
|
||||
related_type="signup",
|
||||
)
|
||||
|
||||
|
||||
@@ -218,17 +218,13 @@ class TestOnboardingServiceStep1:
|
||||
|
||||
service = OnboardingService(db)
|
||||
|
||||
# First create the onboarding record
|
||||
onboarding = VendorOnboarding(
|
||||
vendor_id=99999,
|
||||
status=OnboardingStatus.NOT_STARTED.value,
|
||||
)
|
||||
db.add(onboarding)
|
||||
db.flush()
|
||||
# Use a vendor_id that doesn't exist
|
||||
# The service should check vendor exists before doing anything
|
||||
non_existent_vendor_id = 999999
|
||||
|
||||
with pytest.raises(VendorNotFoundException):
|
||||
service.complete_company_profile(
|
||||
vendor_id=99999,
|
||||
vendor_id=non_existent_vendor_id,
|
||||
default_language="en",
|
||||
dashboard_language="en",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user