165 lines
4.2 KiB
Python
165 lines
4.2 KiB
Python
# tests/fixtures/shop_fixtures.py
|
|
import uuid
|
|
|
|
import pytest
|
|
|
|
from models.database.shop import Shop, ShopProduct
|
|
from models.database.stock import Stock
|
|
|
|
|
|
@pytest.fixture
|
|
def test_shop(db, test_user):
|
|
"""Create a test shop with unique shop code"""
|
|
unique_id = str(uuid.uuid4())[:8] # Short unique identifier
|
|
shop = Shop(
|
|
shop_code=f"TESTSHOP_{unique_id}",
|
|
shop_name=f"Test Shop {unique_id}",
|
|
owner_id=test_user.id,
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(shop)
|
|
db.commit()
|
|
db.refresh(shop)
|
|
return shop
|
|
|
|
|
|
@pytest.fixture
|
|
def unique_shop(db, test_user):
|
|
"""Create a unique shop for tests that need isolated shop data"""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
shop = Shop(
|
|
shop_code=f"UNIQUESHOP_{unique_id}",
|
|
shop_name=f"Unique Test Shop {unique_id}",
|
|
description=f"A unique test shop {unique_id}",
|
|
owner_id=test_user.id,
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(shop)
|
|
db.commit()
|
|
db.refresh(shop)
|
|
return shop
|
|
|
|
|
|
@pytest.fixture
|
|
def inactive_shop(db, other_user):
|
|
"""Create an inactive shop owned by other_user"""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
shop = Shop(
|
|
shop_code=f"INACTIVE_{unique_id}",
|
|
shop_name=f"Inactive Shop {unique_id}",
|
|
owner_id=other_user.id,
|
|
is_active=False,
|
|
is_verified=False,
|
|
)
|
|
db.add(shop)
|
|
db.commit()
|
|
db.refresh(shop)
|
|
return shop
|
|
|
|
|
|
@pytest.fixture
|
|
def verified_shop(db, other_user):
|
|
"""Create a verified shop owned by other_user"""
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
shop = Shop(
|
|
shop_code=f"VERIFIED_{unique_id}",
|
|
shop_name=f"Verified Shop {unique_id}",
|
|
owner_id=other_user.id,
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
db.add(shop)
|
|
db.commit()
|
|
db.refresh(shop)
|
|
return shop
|
|
|
|
|
|
@pytest.fixture
|
|
def shop_product(db, test_shop, unique_product):
|
|
"""Create a shop product relationship"""
|
|
shop_product = ShopProduct(
|
|
shop_id=test_shop.id, product_id=unique_product.id, is_active=True
|
|
)
|
|
# Add optional fields if they exist in your model
|
|
if hasattr(ShopProduct, "shop_price"):
|
|
shop_product.shop_price = 24.99
|
|
if hasattr(ShopProduct, "is_featured"):
|
|
shop_product.is_featured = False
|
|
if hasattr(ShopProduct, "min_quantity"):
|
|
shop_product.min_quantity = 1
|
|
|
|
db.add(shop_product)
|
|
db.commit()
|
|
db.refresh(shop_product)
|
|
return shop_product
|
|
|
|
|
|
@pytest.fixture
|
|
def test_stock(db, test_product, test_shop):
|
|
"""Create test stock entry"""
|
|
unique_id = str(uuid.uuid4())[:8].upper() # Short unique identifier
|
|
stock = Stock(
|
|
gtin=test_product.gtin, # Use gtin instead of product_id
|
|
location=f"WAREHOUSE_A_{unique_id}",
|
|
quantity=10,
|
|
reserved_quantity=0,
|
|
shop_id=test_shop.id, # Add shop_id reference
|
|
)
|
|
db.add(stock)
|
|
db.commit()
|
|
db.refresh(stock)
|
|
return stock
|
|
|
|
|
|
@pytest.fixture
|
|
def multiple_stocks(db, multiple_products, test_shop):
|
|
"""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,
|
|
shop_id=test_shop.id,
|
|
)
|
|
stocks.append(stock)
|
|
|
|
db.add_all(stocks)
|
|
db.commit()
|
|
for stock in stocks:
|
|
db.refresh(stock)
|
|
return stocks
|
|
|
|
|
|
def create_unique_shop_factory():
|
|
"""Factory function to create unique shops in tests"""
|
|
|
|
def _create_shop(db, owner_id, **kwargs):
|
|
unique_id = str(uuid.uuid4())[:8]
|
|
defaults = {
|
|
"shop_code": f"FACTORY_{unique_id}",
|
|
"shop_name": f"Factory Shop {unique_id}",
|
|
"owner_id": owner_id,
|
|
"is_active": True,
|
|
"is_verified": False,
|
|
}
|
|
defaults.update(kwargs)
|
|
|
|
shop = Shop(**defaults)
|
|
db.add(shop)
|
|
db.commit()
|
|
db.refresh(shop)
|
|
return shop
|
|
|
|
return _create_shop
|
|
|
|
|
|
@pytest.fixture
|
|
def shop_factory():
|
|
"""Fixture that provides a shop factory function"""
|
|
return create_unique_shop_factory()
|