shop product refactoring
This commit is contained in:
165
tests/fixtures/vendor_fixtures.py
vendored
Normal file
165
tests/fixtures/vendor_fixtures.py
vendored
Normal file
@@ -0,0 +1,165 @@
|
||||
# tests/fixtures/vendor_fixtures.py
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
|
||||
from models.database.vendor import Vendor
|
||||
from models.database.product import Product
|
||||
from models.database.stock import Stock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_vendor(db, test_user):
|
||||
"""Create a test vendor with unique vendor code"""
|
||||
unique_id = str(uuid.uuid4())[:8].upper() # Make unique ID uppercase
|
||||
vendor = Vendor(
|
||||
vendor_code=f"TESTVENDOR_{unique_id}", # Will be all uppercase
|
||||
vendor_name=f"Test Vendor {unique_id.lower()}", # Keep display name readable
|
||||
owner_id=test_user.id,
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(vendor)
|
||||
db.commit()
|
||||
db.refresh(vendor)
|
||||
return vendor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def unique_vendor(db, test_user):
|
||||
"""Create a unique vendor for tests that need isolated vendor data"""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
vendor = Vendor(
|
||||
vendor_code=f"UNIQUEVENDOR_{unique_id}",
|
||||
vendor_name=f"Unique Test Vendor {unique_id}",
|
||||
description=f"A unique test vendor {unique_id}",
|
||||
owner_id=test_user.id,
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(vendor)
|
||||
db.commit()
|
||||
db.refresh(vendor)
|
||||
return vendor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inactive_vendor(db, other_user):
|
||||
"""Create an inactive vendor owned by other_user"""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
vendor = Vendor(
|
||||
vendor_code=f"INACTIVE_{unique_id}",
|
||||
vendor_name=f"Inactive Vendor {unique_id}",
|
||||
owner_id=other_user.id,
|
||||
is_active=False,
|
||||
is_verified=False,
|
||||
)
|
||||
db.add(vendor)
|
||||
db.commit()
|
||||
db.refresh(vendor)
|
||||
return vendor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def verified_vendor(db, other_user):
|
||||
"""Create a verified vendor owned by other_user"""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
vendor = Vendor(
|
||||
vendor_code=f"VERIFIED_{unique_id}",
|
||||
vendor_name=f"Verified Vendor {unique_id}",
|
||||
owner_id=other_user.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
|
||||
)
|
||||
# Add optional fields if they exist in your model
|
||||
if hasattr(Product, "price"):
|
||||
product.price = 24.99
|
||||
if hasattr(Product, "is_featured"):
|
||||
product.is_featured = False
|
||||
if hasattr(Product, "min_quantity"):
|
||||
product.min_quantity = 1
|
||||
|
||||
db.add(product)
|
||||
db.commit()
|
||||
db.refresh(product)
|
||||
return product
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_stock(db, test_marketplace_product, test_vendor):
|
||||
"""Create test stock entry"""
|
||||
unique_id = str(uuid.uuid4())[:8].upper() # Short unique identifier
|
||||
stock = Stock(
|
||||
gtin=test_marketplace_product.gtin, # Use gtin instead of marketplace_product_id
|
||||
location=f"WAREHOUSE_A_{unique_id}",
|
||||
quantity=10,
|
||||
reserved_quantity=0,
|
||||
vendor_id=test_vendor.id, # Add vendor_id reference
|
||||
)
|
||||
db.add(stock)
|
||||
db.commit()
|
||||
db.refresh(stock)
|
||||
return stock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def multiple_stocks(db, multiple_products, test_vendor):
|
||||
"""Create multiple stock entries for testing"""
|
||||
stocks = []
|
||||
|
||||
for i, product in enumerate(multiple_products):
|
||||
stock = Stock(
|
||||
gtin=product.gtin,
|
||||
location=f"LOC_{i}",
|
||||
quantity=10 + (i * 5), # Different quantities
|
||||
reserved_quantity=i,
|
||||
vendor_id=test_vendor.id,
|
||||
)
|
||||
stocks.append(stock)
|
||||
|
||||
db.add_all(stocks)
|
||||
db.commit()
|
||||
for stock in stocks:
|
||||
db.refresh(stock)
|
||||
return stocks
|
||||
|
||||
|
||||
def create_unique_vendor_factory():
|
||||
"""Factory function to create unique vendors in tests"""
|
||||
|
||||
def _create_vendor(db, owner_id, **kwargs):
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
defaults = {
|
||||
"vendor_code": f"FACTORY_{unique_id}",
|
||||
"vendor_name": f"Factory Vendor {unique_id}",
|
||||
"owner_id": owner_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()
|
||||
Reference in New Issue
Block a user