This commit completes the migration to a fully module-driven architecture: ## Models Migration - Moved all domain models from models/database/ to their respective modules: - tenancy: User, Admin, Vendor, Company, Platform, VendorDomain, etc. - cms: MediaFile, VendorTheme - messaging: Email, VendorEmailSettings, VendorEmailTemplate - core: AdminMenuConfig - models/database/ now only contains Base and TimestampMixin (infrastructure) ## Schemas Migration - Moved all domain schemas from models/schema/ to their respective modules: - tenancy: company, vendor, admin, team, vendor_domain - cms: media, image, vendor_theme - messaging: email - models/schema/ now only contains base.py and auth.py (infrastructure) ## Routes Migration - Moved admin routes from app/api/v1/admin/ to modules: - menu_config.py -> core module - modules.py -> tenancy module - module_config.py -> tenancy module - app/api/v1/admin/ now only aggregates auto-discovered module routes ## Menu System - Implemented module-driven menu system with MenuDiscoveryService - Extended FrontendType enum: PLATFORM, ADMIN, VENDOR, STOREFRONT - Added MenuItemDefinition and MenuSectionDefinition dataclasses - Each module now defines its own menu items in definition.py - MenuService integrates with MenuDiscoveryService for template rendering ## Documentation - Updated docs/architecture/models-structure.md - Updated docs/architecture/menu-management.md - Updated architecture validation rules for new exceptions ## Architecture Validation - Updated MOD-019 rule to allow base.py in models/schema/ - Created core module exceptions.py and schemas/ directory - All validation errors resolved (only warnings remain) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
259 lines
6.9 KiB
Python
259 lines
6.9 KiB
Python
# tests/fixtures/vendor_fixtures.py
|
|
"""
|
|
Vendor-related test fixtures.
|
|
|
|
Note: Fixtures should NOT use db.expunge() as it breaks lazy loading.
|
|
See tests/conftest.py for details on fixture best practices.
|
|
"""
|
|
|
|
import uuid
|
|
|
|
import pytest
|
|
|
|
from app.modules.tenancy.models import Company
|
|
from app.modules.inventory.models import Inventory
|
|
from app.modules.catalog.models import Product
|
|
from app.modules.tenancy.models import Vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def test_company(db, test_user):
|
|
"""Create a test company owned by test_user."""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
company = Company(
|
|
name=f"Test Company {unique_id}",
|
|
owner_user_id=test_user.id,
|
|
contact_email=f"test{unique_id}@company.com",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(company)
|
|
db.commit()
|
|
db.refresh(company)
|
|
return company
|
|
|
|
|
|
@pytest.fixture
|
|
def other_company(db, other_user):
|
|
"""Create a test company owned by other_user."""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
company = Company(
|
|
name=f"Other Company {unique_id}",
|
|
owner_user_id=other_user.id,
|
|
contact_email=f"other{unique_id}@company.com",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(company)
|
|
db.commit()
|
|
db.refresh(company)
|
|
return company
|
|
|
|
|
|
@pytest.fixture
|
|
def test_vendor(db, test_company):
|
|
"""Create a test vendor with unique vendor code."""
|
|
unique_id = str(uuid.uuid4())[:8].upper()
|
|
vendor = Vendor(
|
|
company_id=test_company.id,
|
|
vendor_code=f"TESTVENDOR_{unique_id}",
|
|
subdomain=f"testvendor{unique_id.lower()}",
|
|
name=f"Test Vendor {unique_id.lower()}",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(vendor)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
return vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def test_vendor_with_vendor_user(db, test_vendor_user):
|
|
"""Create a vendor owned by a vendor user (for testing vendor API endpoints)."""
|
|
from app.modules.tenancy.models import VendorUser, VendorUserType
|
|
|
|
unique_id = str(uuid.uuid4())[:8].upper()
|
|
|
|
# Create company first
|
|
company = Company(
|
|
name=f"Vendor API Company {unique_id}",
|
|
owner_user_id=test_vendor_user.id,
|
|
contact_email=f"vendorapi{unique_id}@company.com",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(company)
|
|
db.commit()
|
|
db.refresh(company)
|
|
|
|
vendor = Vendor(
|
|
company_id=company.id,
|
|
vendor_code=f"VENDORAPI_{unique_id}",
|
|
subdomain=f"vendorapi{unique_id.lower()}",
|
|
name=f"Vendor API Test {unique_id}",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(vendor)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
|
|
# Create VendorUser association
|
|
vendor_user = VendorUser(
|
|
vendor_id=vendor.id,
|
|
user_id=test_vendor_user.id,
|
|
user_type=VendorUserType.OWNER.value,
|
|
is_active=True,
|
|
)
|
|
db.add(vendor_user)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
return vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def unique_vendor(db, test_company):
|
|
"""Create a unique vendor for tests that need isolated vendor data."""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
vendor = Vendor(
|
|
company_id=test_company.id,
|
|
vendor_code=f"UNIQUEVENDOR_{unique_id.upper()}",
|
|
subdomain=f"uniquevendor{unique_id.lower()}",
|
|
name=f"Unique Test Vendor {unique_id}",
|
|
description=f"A unique test vendor {unique_id}",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(vendor)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
return vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def inactive_vendor(db, other_company):
|
|
"""Create an inactive vendor owned by other_user."""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
vendor = Vendor(
|
|
company_id=other_company.id,
|
|
vendor_code=f"INACTIVE_{unique_id.upper()}",
|
|
subdomain=f"inactive{unique_id.lower()}",
|
|
name=f"Inactive Vendor {unique_id}",
|
|
is_active=False,
|
|
is_verified=False,
|
|
)
|
|
db.add(vendor)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
return vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def verified_vendor(db, other_company):
|
|
"""Create a verified vendor owned by other_user."""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
vendor = Vendor(
|
|
company_id=other_company.id,
|
|
vendor_code=f"VERIFIED_{unique_id.upper()}",
|
|
subdomain=f"verified{unique_id.lower()}",
|
|
name=f"Verified Vendor {unique_id}",
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(vendor)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
return vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def test_product(db, test_vendor, unique_product):
|
|
"""Create a vendor product relationship."""
|
|
product = Product(
|
|
vendor_id=test_vendor.id,
|
|
marketplace_product_id=unique_product.id,
|
|
is_active=True,
|
|
price=24.99,
|
|
is_featured=False,
|
|
min_quantity=1,
|
|
)
|
|
db.add(product)
|
|
db.commit()
|
|
db.refresh(product)
|
|
return product
|
|
|
|
|
|
@pytest.fixture
|
|
def test_inventory(db, test_product):
|
|
"""Create test inventory entry linked to product."""
|
|
unique_id = str(uuid.uuid4())[:8].upper()
|
|
inventory = Inventory(
|
|
product_id=test_product.id,
|
|
vendor_id=test_product.vendor_id,
|
|
warehouse="strassen",
|
|
bin_location=f"SA-10-{unique_id[:2]}",
|
|
location=f"WAREHOUSE_A_{unique_id}",
|
|
quantity=100,
|
|
reserved_quantity=10,
|
|
gtin=test_product.marketplace_product.gtin,
|
|
)
|
|
db.add(inventory)
|
|
db.commit()
|
|
db.refresh(inventory)
|
|
return inventory
|
|
|
|
|
|
@pytest.fixture
|
|
def multiple_inventory_entries(db, multiple_products, test_vendor):
|
|
"""Create multiple inventory entries for testing."""
|
|
inventory_entries = []
|
|
for i, product in enumerate(multiple_products):
|
|
inventory = Inventory(
|
|
product_id=product.id,
|
|
gtin=product.gtin,
|
|
warehouse="strassen",
|
|
bin_location=f"SA-{i:02d}-01",
|
|
location=f"LOC_{i}",
|
|
quantity=10 + (i * 5),
|
|
reserved_quantity=i,
|
|
vendor_id=test_vendor.id,
|
|
)
|
|
inventory_entries.append(inventory)
|
|
|
|
db.add_all(inventory_entries)
|
|
db.commit()
|
|
for inventory in inventory_entries:
|
|
db.refresh(inventory)
|
|
return inventory_entries
|
|
|
|
|
|
def create_unique_vendor_factory():
|
|
"""Factory function to create unique vendors in tests."""
|
|
|
|
def _create_vendor(db, company_id, **kwargs):
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
defaults = {
|
|
"company_id": company_id,
|
|
"vendor_code": f"FACTORY_{unique_id.upper()}",
|
|
"subdomain": f"factory{unique_id.lower()}",
|
|
"name": f"Factory Vendor {unique_id}",
|
|
"is_active": True,
|
|
"is_verified": False,
|
|
}
|
|
defaults.update(kwargs)
|
|
|
|
vendor = Vendor(**defaults)
|
|
db.add(vendor)
|
|
db.commit()
|
|
db.refresh(vendor)
|
|
return vendor
|
|
|
|
return _create_vendor
|
|
|
|
|
|
@pytest.fixture
|
|
def vendor_factory():
|
|
"""Fixture that provides a vendor factory function."""
|
|
return create_unique_vendor_factory()
|