test: update tests for multi-language translation support

- Update marketplace_product_fixtures to create translations
- Update test_marketplace_product.py for translation-based titles
- Update test_product.py for effective property tests
- Update test_order.py to use get_title() method
- Add comprehensive CSV processor tests for translations
- Update stats service tests for new flat response structure
- Fix product schema tests with required marketplace_product_id field
- Add helper function create_marketplace_product_with_translation()

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-11 17:29:50 +01:00
parent 22c4937779
commit 2ecc2a9785
8 changed files with 790 additions and 201 deletions

View File

@@ -11,10 +11,31 @@ from app.services.stats_service import StatsService
from models.database.inventory import Inventory
from models.database.marketplace_import_job import MarketplaceImportJob
from models.database.marketplace_product import MarketplaceProduct
from models.database.marketplace_product_translation import MarketplaceProductTranslation
from models.database.product import Product
from models.database.vendor import Vendor
def create_marketplace_product_with_translation(
db, marketplace_product_id, title, language="en", **kwargs
):
"""Helper to create a MarketplaceProduct with its translation."""
product = MarketplaceProduct(
marketplace_product_id=marketplace_product_id,
**kwargs
)
db.add(product)
db.flush() # Get the product ID
translation = MarketplaceProductTranslation(
marketplace_product_id=product.id,
language=language,
title=title,
)
db.add(translation)
return product
@pytest.mark.unit
@pytest.mark.stats
class TestStatsService:
@@ -58,29 +79,28 @@ class TestStatsService:
):
"""Test comprehensive stats with multiple marketplaces."""
unique_id = str(uuid.uuid4())[:8]
additional_products = [
MarketplaceProduct(
marketplace_product_id=f"PROD002_{unique_id}",
title="MarketplaceProduct 2",
brand="DifferentBrand",
google_product_category="Different Category",
marketplace="Amazon",
vendor_name="AmazonVendor",
price="15.99",
currency="EUR",
),
MarketplaceProduct(
marketplace_product_id=f"PROD003_{unique_id}",
title="MarketplaceProduct 3",
brand="ThirdBrand",
google_product_category="Third Category",
marketplace="eBay",
vendor_name="eBayVendor",
price="25.99",
currency="USD",
),
]
db.add_all(additional_products)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"PROD002_{unique_id}",
title="MarketplaceProduct 2",
brand="DifferentBrand",
google_product_category="Different Category",
marketplace="Amazon",
vendor_name="AmazonVendor",
price="15.99",
currency="EUR",
)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"PROD003_{unique_id}",
title="MarketplaceProduct 3",
brand="ThirdBrand",
google_product_category="Third Category",
marketplace="eBay",
vendor_name="eBayVendor",
price="25.99",
currency="USD",
)
db.commit()
stats = self.service.get_comprehensive_stats(db)
@@ -91,29 +111,28 @@ class TestStatsService:
def test_get_comprehensive_stats_handles_nulls(self, db):
"""Test comprehensive stats handles null/empty values correctly."""
unique_id = str(uuid.uuid4())[:8]
products_with_nulls = [
MarketplaceProduct(
marketplace_product_id=f"NULL001_{unique_id}",
title="MarketplaceProduct with Nulls",
brand=None,
google_product_category=None,
marketplace=None,
vendor_name=None,
price="10.00",
currency="EUR",
),
MarketplaceProduct(
marketplace_product_id=f"EMPTY001_{unique_id}",
title="MarketplaceProduct with Empty Values",
brand="",
google_product_category="",
marketplace="",
vendor_name="",
price="15.00",
currency="EUR",
),
]
db.add_all(products_with_nulls)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"NULL001_{unique_id}",
title="MarketplaceProduct with Nulls",
brand=None,
google_product_category=None,
marketplace=None,
vendor_name=None,
price="10.00",
currency="EUR",
)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"EMPTY001_{unique_id}",
title="MarketplaceProduct with Empty Values",
brand="",
google_product_category="",
marketplace="",
vendor_name="",
price="15.00",
currency="EUR",
)
db.commit()
stats = self.service.get_comprehensive_stats(db)
@@ -170,36 +189,36 @@ class TestStatsService:
):
"""Test marketplace breakdown with multiple marketplaces."""
unique_id = str(uuid.uuid4())[:8]
marketplace_products = [
MarketplaceProduct(
marketplace_product_id=f"AMAZON001_{unique_id}",
title="Amazon MarketplaceProduct 1",
brand="AmazonBrand1",
marketplace="Amazon",
vendor_name="AmazonVendor1",
price="20.00",
currency="EUR",
),
MarketplaceProduct(
marketplace_product_id=f"AMAZON002_{unique_id}",
title="Amazon MarketplaceProduct 2",
brand="AmazonBrand2",
marketplace="Amazon",
vendor_name="AmazonVendor2",
price="25.00",
currency="EUR",
),
MarketplaceProduct(
marketplace_product_id=f"EBAY001_{unique_id}",
title="eBay MarketplaceProduct",
brand="eBayBrand",
marketplace="eBay",
vendor_name="eBayVendor",
price="30.00",
currency="USD",
),
]
db.add_all(marketplace_products)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"AMAZON001_{unique_id}",
title="Amazon MarketplaceProduct 1",
brand="AmazonBrand1",
marketplace="Amazon",
vendor_name="AmazonVendor1",
price="20.00",
currency="EUR",
)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"AMAZON002_{unique_id}",
title="Amazon MarketplaceProduct 2",
brand="AmazonBrand2",
marketplace="Amazon",
vendor_name="AmazonVendor2",
price="25.00",
currency="EUR",
)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"EBAY001_{unique_id}",
title="eBay MarketplaceProduct",
brand="eBayBrand",
marketplace="eBay",
vendor_name="eBayVendor",
price="30.00",
currency="USD",
)
db.commit()
stats = self.service.get_marketplace_breakdown_stats(db)
@@ -223,7 +242,8 @@ class TestStatsService:
def test_get_marketplace_breakdown_stats_excludes_nulls(self, db):
"""Test marketplace breakdown excludes products with null marketplaces."""
unique_id = str(uuid.uuid4())[:8]
null_marketplace_product = MarketplaceProduct(
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"NULLMARKET001_{unique_id}",
title="MarketplaceProduct without marketplace",
marketplace=None,
@@ -232,7 +252,6 @@ class TestStatsService:
price="10.00",
currency="EUR",
)
db.add(null_marketplace_product)
db.commit()
stats = self.service.get_marketplace_breakdown_stats(db)
@@ -264,15 +283,16 @@ class TestStatsService:
"""Test getting vendor statistics successfully."""
stats = self.service.get_vendor_stats(db, test_vendor.id)
assert "catalog" in stats
assert "staging" in stats
assert "inventory" in stats
assert "imports" in stats
assert "orders" in stats
assert "customers" in stats
# New flat structure compatible with VendorDashboardStatsResponse
assert "total_products" in stats
assert "active_products" in stats
assert "total_orders" in stats
assert "total_customers" in stats
assert "total_revenue" in stats
assert "total_inventory_quantity" in stats
assert stats["catalog"]["total_products"] >= 0
assert stats["inventory"]["total_quantity"] >= 0
assert stats["total_products"] >= 0
assert stats["total_inventory_quantity"] >= 0
def test_get_vendor_stats_vendor_not_found(self, db):
"""Test vendor stats with non-existent vendor."""
@@ -285,8 +305,8 @@ class TestStatsService:
"""Test vendor stats includes inventory data."""
stats = self.service.get_vendor_stats(db, test_vendor.id)
assert stats["inventory"]["total_quantity"] >= test_inventory.quantity
assert stats["inventory"]["reserved_quantity"] >= 0
assert stats["total_inventory_quantity"] >= test_inventory.quantity
assert stats["reserved_inventory_quantity"] >= 0
def test_get_vendor_stats_database_error(self, db, test_vendor):
"""Test vendor stats handles database errors after vendor check."""
@@ -453,27 +473,26 @@ class TestStatsService:
def test_get_unique_brands_count(self, db, test_marketplace_product):
"""Test getting unique brands count."""
unique_id = str(uuid.uuid4())[:8]
brand_products = [
MarketplaceProduct(
marketplace_product_id=f"BRAND001_{unique_id}",
title="Brand MarketplaceProduct 1",
brand="BrandA",
marketplace="Test",
vendor_name="TestVendor",
price="10.00",
currency="EUR",
),
MarketplaceProduct(
marketplace_product_id=f"BRAND002_{unique_id}",
title="Brand MarketplaceProduct 2",
brand="BrandB",
marketplace="Test",
vendor_name="TestVendor",
price="15.00",
currency="EUR",
),
]
db.add_all(brand_products)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"BRAND001_{unique_id}",
title="Brand MarketplaceProduct 1",
brand="BrandA",
marketplace="Test",
vendor_name="TestVendor",
price="10.00",
currency="EUR",
)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"BRAND002_{unique_id}",
title="Brand MarketplaceProduct 2",
brand="BrandB",
marketplace="Test",
vendor_name="TestVendor",
price="15.00",
currency="EUR",
)
db.commit()
count = self.service._get_unique_brands_count(db)
@@ -484,27 +503,26 @@ class TestStatsService:
def test_get_unique_categories_count(self, db, test_marketplace_product):
"""Test getting unique categories count."""
unique_id = str(uuid.uuid4())[:8]
category_products = [
MarketplaceProduct(
marketplace_product_id=f"CAT001_{unique_id}",
title="Category MarketplaceProduct 1",
google_product_category="Electronics",
marketplace="Test",
vendor_name="TestVendor",
price="10.00",
currency="EUR",
),
MarketplaceProduct(
marketplace_product_id=f"CAT002_{unique_id}",
title="Category MarketplaceProduct 2",
google_product_category="Books",
marketplace="Test",
vendor_name="TestVendor",
price="15.00",
currency="EUR",
),
]
db.add_all(category_products)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"CAT001_{unique_id}",
title="Category MarketplaceProduct 1",
google_product_category="Electronics",
marketplace="Test",
vendor_name="TestVendor",
price="10.00",
currency="EUR",
)
create_marketplace_product_with_translation(
db,
marketplace_product_id=f"CAT002_{unique_id}",
title="Category MarketplaceProduct 2",
google_product_category="Books",
marketplace="Test",
vendor_name="TestVendor",
price="15.00",
currency="EUR",
)
db.commit()
count = self.service._get_unique_categories_count(db)