Files
orion/tests/unit/models/database/test_customer.py
Samir Boulahtit ca9f17fc37 refactor: split database model tests into organized modules
Split the monolithic test_database_models.py into focused test modules:

Database model tests (tests/unit/models/database/):
- test_customer.py: Customer model and authentication tests
- test_inventory.py: Inventory model tests
- test_marketplace_import_job.py: Import job model tests
- test_marketplace_product.py: Marketplace product model tests
- test_order.py: Order and OrderItem model tests
- test_product.py: Product model tests
- test_team.py: Team invitation and membership tests
- test_user.py: User model tests
- test_vendor.py: Vendor model tests

Schema validation tests (tests/unit/models/schema/):
- test_auth.py: Auth schema validation tests
- test_customer.py: Customer schema validation tests
- test_inventory.py: Inventory schema validation tests
- test_marketplace_import_job.py: Import job schema tests
- test_order.py: Order schema validation tests
- test_product.py: Product schema validation tests

This improves test organization and makes it easier to find
and maintain tests for specific models.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 21:42:26 +01:00

241 lines
7.8 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="Luxembourg",
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="Luxembourg",
)
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="Luxembourg",
)
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="Luxembourg",
)
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="Luxembourg",
)
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="Luxembourg",
)
db.add(address)
db.commit()
db.refresh(address)
assert address.customer is not None
assert address.customer.id == test_customer.id