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>
248 lines
8.0 KiB
Python
248 lines
8.0 KiB
Python
# tests/unit/models/database/test_customer.py
|
|
"""Unit tests for Customer and CustomerAddress database models."""
|
|
|
|
import pytest
|
|
|
|
from models.database.customer import Customer, CustomerAddress
|
|
|
|
|
|
@pytest.mark.unit
|
|
@pytest.mark.database
|
|
class TestCustomerModel:
|
|
"""Test Customer model."""
|
|
|
|
def test_customer_creation(self, db, test_vendor):
|
|
"""Test Customer model with vendor isolation."""
|
|
customer = Customer(
|
|
vendor_id=test_vendor.id,
|
|
email="customer@example.com",
|
|
hashed_password="hashed_password",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
customer_number="CUST001",
|
|
is_active=True,
|
|
)
|
|
|
|
db.add(customer)
|
|
db.commit()
|
|
db.refresh(customer)
|
|
|
|
assert customer.id is not None
|
|
assert customer.vendor_id == test_vendor.id
|
|
assert customer.email == "customer@example.com"
|
|
assert customer.customer_number == "CUST001"
|
|
assert customer.first_name == "John"
|
|
assert customer.last_name == "Doe"
|
|
assert customer.vendor.vendor_code == test_vendor.vendor_code
|
|
|
|
def test_customer_default_values(self, db, test_vendor):
|
|
"""Test Customer model default values."""
|
|
customer = Customer(
|
|
vendor_id=test_vendor.id,
|
|
email="defaults@example.com",
|
|
hashed_password="hash",
|
|
customer_number="CUST_DEFAULTS",
|
|
)
|
|
db.add(customer)
|
|
db.commit()
|
|
db.refresh(customer)
|
|
|
|
assert customer.is_active is True # Default
|
|
assert customer.marketing_consent is False # Default
|
|
assert customer.total_orders == 0 # Default
|
|
assert customer.total_spent == 0 # Default
|
|
|
|
def test_customer_full_name_property(self, db, test_vendor):
|
|
"""Test Customer full_name computed property."""
|
|
customer = Customer(
|
|
vendor_id=test_vendor.id,
|
|
email="fullname@example.com",
|
|
hashed_password="hash",
|
|
customer_number="CUST_FULLNAME",
|
|
first_name="Jane",
|
|
last_name="Smith",
|
|
)
|
|
db.add(customer)
|
|
db.commit()
|
|
db.refresh(customer)
|
|
|
|
assert customer.full_name == "Jane Smith"
|
|
|
|
def test_customer_full_name_fallback_to_email(self, db, test_vendor):
|
|
"""Test Customer full_name falls back to email when names not set."""
|
|
customer = Customer(
|
|
vendor_id=test_vendor.id,
|
|
email="noname@example.com",
|
|
hashed_password="hash",
|
|
customer_number="CUST_NONAME",
|
|
)
|
|
db.add(customer)
|
|
db.commit()
|
|
db.refresh(customer)
|
|
|
|
assert customer.full_name == "noname@example.com"
|
|
|
|
def test_customer_optional_fields(self, db, test_vendor):
|
|
"""Test Customer with optional fields."""
|
|
customer = Customer(
|
|
vendor_id=test_vendor.id,
|
|
email="optional@example.com",
|
|
hashed_password="hash",
|
|
customer_number="CUST_OPT",
|
|
phone="+352123456789",
|
|
preferences={"language": "en", "currency": "EUR"},
|
|
marketing_consent=True,
|
|
)
|
|
db.add(customer)
|
|
db.commit()
|
|
db.refresh(customer)
|
|
|
|
assert customer.phone == "+352123456789"
|
|
assert customer.preferences == {"language": "en", "currency": "EUR"}
|
|
assert customer.marketing_consent is True
|
|
|
|
def test_customer_vendor_relationship(self, db, test_vendor):
|
|
"""Test Customer-Vendor relationship."""
|
|
customer = Customer(
|
|
vendor_id=test_vendor.id,
|
|
email="relationship@example.com",
|
|
hashed_password="hash",
|
|
customer_number="CUST_REL",
|
|
)
|
|
db.add(customer)
|
|
db.commit()
|
|
db.refresh(customer)
|
|
|
|
assert customer.vendor is not None
|
|
assert customer.vendor.id == test_vendor.id
|
|
|
|
|
|
@pytest.mark.unit
|
|
@pytest.mark.database
|
|
class TestCustomerAddressModel:
|
|
"""Test CustomerAddress model."""
|
|
|
|
def test_customer_address_creation(self, db, test_vendor, test_customer):
|
|
"""Test CustomerAddress model."""
|
|
address = CustomerAddress(
|
|
vendor_id=test_vendor.id,
|
|
customer_id=test_customer.id,
|
|
address_type="shipping",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
address_line_1="123 Main St",
|
|
city="Luxembourg",
|
|
postal_code="L-1234",
|
|
country_name="Luxembourg",
|
|
country_iso="LU",
|
|
is_default=True,
|
|
)
|
|
|
|
db.add(address)
|
|
db.commit()
|
|
db.refresh(address)
|
|
|
|
assert address.id is not None
|
|
assert address.vendor_id == test_vendor.id
|
|
assert address.customer_id == test_customer.id
|
|
assert address.address_type == "shipping"
|
|
assert address.is_default is True
|
|
|
|
def test_customer_address_types(self, db, test_vendor, test_customer):
|
|
"""Test CustomerAddress with different address types."""
|
|
shipping_address = CustomerAddress(
|
|
vendor_id=test_vendor.id,
|
|
customer_id=test_customer.id,
|
|
address_type="shipping",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
address_line_1="123 Shipping St",
|
|
city="Luxembourg",
|
|
postal_code="L-1234",
|
|
country_name="Luxembourg",
|
|
country_iso="LU",
|
|
)
|
|
db.add(shipping_address)
|
|
|
|
billing_address = CustomerAddress(
|
|
vendor_id=test_vendor.id,
|
|
customer_id=test_customer.id,
|
|
address_type="billing",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
address_line_1="456 Billing Ave",
|
|
city="Luxembourg",
|
|
postal_code="L-5678",
|
|
country_name="Luxembourg",
|
|
country_iso="LU",
|
|
)
|
|
db.add(billing_address)
|
|
db.commit()
|
|
|
|
assert shipping_address.address_type == "shipping"
|
|
assert billing_address.address_type == "billing"
|
|
|
|
def test_customer_address_optional_fields(self, db, test_vendor, test_customer):
|
|
"""Test CustomerAddress with optional fields."""
|
|
address = CustomerAddress(
|
|
vendor_id=test_vendor.id,
|
|
customer_id=test_customer.id,
|
|
address_type="shipping",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
company="ACME Corp",
|
|
address_line_1="123 Main St",
|
|
address_line_2="Suite 100",
|
|
city="Luxembourg",
|
|
postal_code="L-1234",
|
|
country_name="Luxembourg",
|
|
country_iso="LU",
|
|
)
|
|
db.add(address)
|
|
db.commit()
|
|
db.refresh(address)
|
|
|
|
assert address.company == "ACME Corp"
|
|
assert address.address_line_2 == "Suite 100"
|
|
|
|
def test_customer_address_default_values(self, db, test_vendor, test_customer):
|
|
"""Test CustomerAddress default values."""
|
|
address = CustomerAddress(
|
|
vendor_id=test_vendor.id,
|
|
customer_id=test_customer.id,
|
|
address_type="shipping",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
address_line_1="123 Main St",
|
|
city="Luxembourg",
|
|
postal_code="L-1234",
|
|
country_name="Luxembourg",
|
|
country_iso="LU",
|
|
)
|
|
db.add(address)
|
|
db.commit()
|
|
db.refresh(address)
|
|
|
|
assert address.is_default is False # Default
|
|
|
|
def test_customer_address_relationships(self, db, test_vendor, test_customer):
|
|
"""Test CustomerAddress relationships."""
|
|
address = CustomerAddress(
|
|
vendor_id=test_vendor.id,
|
|
customer_id=test_customer.id,
|
|
address_type="shipping",
|
|
first_name="John",
|
|
last_name="Doe",
|
|
address_line_1="123 Main St",
|
|
city="Luxembourg",
|
|
postal_code="L-1234",
|
|
country_name="Luxembourg",
|
|
country_iso="LU",
|
|
)
|
|
db.add(address)
|
|
db.commit()
|
|
db.refresh(address)
|
|
|
|
assert address.customer is not None
|
|
assert address.customer.id == test_customer.id
|