shop product refactoring

This commit is contained in:
2025-10-04 23:38:53 +02:00
parent 4d2866af5e
commit 0114b6c46e
68 changed files with 2234 additions and 2236 deletions

View File

@@ -5,14 +5,14 @@ from models.database.marketplace_import_job import MarketplaceImportJob
@pytest.fixture
def test_marketplace_import_job(db, test_shop, test_user):
def test_marketplace_import_job(db, test_vendor, test_user):
"""Create a test marketplace import job"""
job = MarketplaceImportJob(
marketplace="amazon",
shop_name="Test Import Shop",
vendor_name="Test Import Shop",
status="completed",
source_url="https://test-marketplace.example.com/import",
shop_id=test_shop.id,
vendor_id=test_vendor.id,
user_id=test_user.id,
imported_count=5,
updated_count=3,
@@ -26,14 +26,14 @@ def test_marketplace_import_job(db, test_shop, test_user):
return job
def create_test_marketplace_import_job(db, shop_id, user_id, **kwargs):
def create_test_marketplace_import_job(db, vendor_id, user_id, **kwargs):
"""Helper function to create MarketplaceImportJob with defaults"""
defaults = {
"marketplace": "test",
"shop_name": "Test Shop",
"vendor_name": "Test Shop",
"status": "pending",
"source_url": "https://test.example.com/import",
"shop_id": shop_id,
"vendor_id": vendor_id,
"user_id": user_id,
"imported_count": 0,
"updated_count": 0,

View File

@@ -19,7 +19,7 @@ def test_marketplace_product(db):
gtin="1234567890123",
availability="in stock",
marketplace="Letzshop",
shop_name="TestShop",
vendor_name="TestVendor",
)
db.add(marketplace_product)
db.commit()
@@ -41,7 +41,7 @@ def unique_product(db):
gtin=f"123456789{unique_id[:4]}",
availability="in stock",
marketplace="Letzshop",
shop_name=f"UniqueShop_{unique_id}",
vendor_name=f"UniqueShop_{unique_id}",
google_product_category=f"UniqueCategory_{unique_id}",
)
db.add(marketplace_product)
@@ -65,7 +65,7 @@ def multiple_products(db):
currency="EUR",
brand=f"MultiBrand_{i % 3}", # Create 3 different brands
marketplace=f"MultiMarket_{i % 2}", # Create 2 different marketplaces
shop_name=f"MultiShop_{i}",
vendor_name=f"MultiShop_{i}",
google_product_category=f"MultiCategory_{i % 2}", # Create 2 different categories
gtin=f"1234567890{i}{unique_id[:2]}",
)
@@ -89,7 +89,7 @@ def create_unique_marketplace_product_factory():
"price": "15.99",
"currency": "EUR",
"marketplace": "TestMarket",
"shop_name": "TestShop",
"vendor_name": "TestVendor",
}
defaults.update(kwargs)

View File

@@ -21,11 +21,11 @@ def empty_db(db):
# Clear only the tables that are relevant for admin service testing
# In order to respect foreign key constraints
tables_to_clear = [
"marketplace_import_jobs", # Has foreign keys to shops and users
"products", # Has foreign keys to shops and products
"marketplace_import_jobs", # Has foreign keys to vendors and users
"products", # Has foreign keys to vendors and products
"stock", # Fixed: singular not plural
"products", # Referenced by products
"shops", # Has foreign key to users
"vendors", # Has foreign key to users
"users" # Base table
]

View File

@@ -1,90 +1,90 @@
# tests/fixtures/shop_fixtures.py
# tests/fixtures/vendor_fixtures.py
import uuid
import pytest
from models.database.shop import Shop
from models.database.vendor import Vendor
from models.database.product import Product
from models.database.stock import Stock
@pytest.fixture
def test_shop(db, test_user):
"""Create a test shop with unique shop code"""
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
shop = Shop(
shop_code=f"TESTSHOP_{unique_id}", # Will be all uppercase
shop_name=f"Test Shop {unique_id.lower()}", # Keep display name readable
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(shop)
db.add(vendor)
db.commit()
db.refresh(shop)
return shop
db.refresh(vendor)
return vendor
@pytest.fixture
def unique_shop(db, test_user):
"""Create a unique shop for tests that need isolated shop data"""
def unique_vendor(db, test_user):
"""Create a unique vendor for tests that need isolated vendor 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}",
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(shop)
db.add(vendor)
db.commit()
db.refresh(shop)
return shop
db.refresh(vendor)
return vendor
@pytest.fixture
def inactive_shop(db, other_user):
"""Create an inactive shop owned by other_user"""
def inactive_vendor(db, other_user):
"""Create an inactive vendor 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}",
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(shop)
db.add(vendor)
db.commit()
db.refresh(shop)
return shop
db.refresh(vendor)
return vendor
@pytest.fixture
def verified_shop(db, other_user):
"""Create a verified shop owned by other_user"""
def verified_vendor(db, other_user):
"""Create a verified vendor 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}",
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(shop)
db.add(vendor)
db.commit()
db.refresh(shop)
return shop
db.refresh(vendor)
return vendor
@pytest.fixture
def test_product(db, test_shop, unique_product):
"""Create a shop product relationship"""
def test_product(db, test_vendor, unique_product):
"""Create a vendor product relationship"""
product = Product(
shop_id=test_shop.id, marketplace_product_id=unique_product.id, is_active=True
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, "shop_price"):
if hasattr(Product, "price"):
product.price = 24.99
if hasattr(Product, "is_featured"):
product.is_featured = False
@@ -98,7 +98,7 @@ def test_product(db, test_shop, unique_product):
@pytest.fixture
def test_stock(db, test_marketplace_product, test_shop):
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(
@@ -106,7 +106,7 @@ def test_stock(db, test_marketplace_product, test_shop):
location=f"WAREHOUSE_A_{unique_id}",
quantity=10,
reserved_quantity=0,
shop_id=test_shop.id, # Add shop_id reference
vendor_id=test_vendor.id, # Add vendor_id reference
)
db.add(stock)
db.commit()
@@ -115,7 +115,7 @@ def test_stock(db, test_marketplace_product, test_shop):
@pytest.fixture
def multiple_stocks(db, multiple_products, test_shop):
def multiple_stocks(db, multiple_products, test_vendor):
"""Create multiple stock entries for testing"""
stocks = []
@@ -125,7 +125,7 @@ def multiple_stocks(db, multiple_products, test_shop):
location=f"LOC_{i}",
quantity=10 + (i * 5), # Different quantities
reserved_quantity=i,
shop_id=test_shop.id,
vendor_id=test_vendor.id,
)
stocks.append(stock)
@@ -136,30 +136,30 @@ def multiple_stocks(db, multiple_products, test_shop):
return stocks
def create_unique_shop_factory():
"""Factory function to create unique shops in tests"""
def create_unique_vendor_factory():
"""Factory function to create unique vendors in tests"""
def _create_shop(db, owner_id, **kwargs):
def _create_vendor(db, owner_id, **kwargs):
unique_id = str(uuid.uuid4())[:8]
defaults = {
"shop_code": f"FACTORY_{unique_id}",
"shop_name": f"Factory Shop {unique_id}",
"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)
shop = Shop(**defaults)
db.add(shop)
vendor = Vendor(**defaults)
db.add(vendor)
db.commit()
db.refresh(shop)
return shop
db.refresh(vendor)
return vendor
return _create_shop
return _create_vendor
@pytest.fixture
def shop_factory():
"""Fixture that provides a shop factory function"""
return create_unique_shop_factory()
def vendor_factory():
"""Fixture that provides a vendor factory function"""
return create_unique_vendor_factory()