marketplace refactoring

This commit is contained in:
2025-10-04 13:38:10 +02:00
parent 32be301d83
commit c971674ec2
68 changed files with 1102 additions and 1128 deletions

View File

@@ -2,7 +2,7 @@
import pytest
from app.services.stats_service import StatsService
from models.database.product import Product
from models.database.marketplace_product import MarketplaceProduct
from models.database.stock import Stock
@@ -15,7 +15,7 @@ class TestStatsService:
"""Setup method following the same pattern as other service tests"""
self.service = StatsService()
def test_get_comprehensive_stats_basic(self, db, test_product, test_stock):
def test_get_comprehensive_stats_basic(self, db, test_marketplace_product, test_stock):
"""Test getting comprehensive stats with basic data"""
stats = self.service.get_comprehensive_stats(db)
@@ -31,13 +31,13 @@ class TestStatsService:
assert stats["total_stock_entries"] >= 1
assert stats["total_inventory_quantity"] >= 10 # test_stock has quantity 10
def test_get_comprehensive_stats_multiple_products(self, db, test_product):
def test_get_comprehensive_stats_multiple_products(self, db, test_marketplace_product):
"""Test comprehensive stats with multiple products across different dimensions"""
# Create products with different brands, categories, marketplaces
additional_products = [
Product(
product_id="PROD002",
title="Product 2",
MarketplaceProduct(
marketplace_product_id="PROD002",
title="MarketplaceProduct 2",
brand="DifferentBrand",
google_product_category="Different Category",
marketplace="Amazon",
@@ -45,9 +45,9 @@ class TestStatsService:
price="15.99",
currency="EUR",
),
Product(
product_id="PROD003",
title="Product 3",
MarketplaceProduct(
marketplace_product_id="PROD003",
title="MarketplaceProduct 3",
brand="ThirdBrand",
google_product_category="Third Category",
marketplace="eBay",
@@ -55,12 +55,12 @@ class TestStatsService:
price="25.99",
currency="USD",
),
Product(
product_id="PROD004",
title="Product 4",
brand="TestBrand", # Same as test_product
MarketplaceProduct(
marketplace_product_id="PROD004",
title="MarketplaceProduct 4",
brand="TestBrand", # Same as test_marketplace_product
google_product_category="Different Category",
marketplace="Letzshop", # Same as test_product
marketplace="Letzshop", # Same as test_marketplace_product
shop_name="DifferentShop",
price="35.99",
currency="EUR",
@@ -71,7 +71,7 @@ class TestStatsService:
stats = self.service.get_comprehensive_stats(db)
assert stats["total_products"] >= 4 # test_product + 3 additional
assert stats["total_products"] >= 4 # test_marketplace_product + 3 additional
assert stats["unique_brands"] >= 3 # TestBrand, DifferentBrand, ThirdBrand
assert stats["unique_categories"] >= 2 # At least 2 different categories
assert stats["unique_marketplaces"] >= 3 # Letzshop, Amazon, eBay
@@ -81,9 +81,9 @@ class TestStatsService:
"""Test comprehensive stats handles null/empty values correctly"""
# Create products with null/empty values
products_with_nulls = [
Product(
product_id="NULL001",
title="Product with Nulls",
MarketplaceProduct(
marketplace_product_id="NULL001",
title="MarketplaceProduct with Nulls",
brand=None, # Null brand
google_product_category=None, # Null category
marketplace=None, # Null marketplace
@@ -91,9 +91,9 @@ class TestStatsService:
price="10.00",
currency="EUR",
),
Product(
product_id="EMPTY001",
title="Product with Empty Values",
MarketplaceProduct(
marketplace_product_id="EMPTY001",
title="MarketplaceProduct with Empty Values",
brand="", # Empty brand
google_product_category="", # Empty category
marketplace="", # Empty marketplace
@@ -115,7 +115,7 @@ class TestStatsService:
assert isinstance(stats["unique_marketplaces"], int)
assert isinstance(stats["unique_shops"], int)
def test_get_marketplace_breakdown_stats_basic(self, db, test_product):
def test_get_marketplace_breakdown_stats_basic(self, db, test_marketplace_product):
"""Test getting marketplace breakdown stats with basic data"""
stats = self.service.get_marketplace_breakdown_stats(db)
@@ -124,7 +124,7 @@ class TestStatsService:
# Find our test marketplace in the results
test_marketplace_stat = next(
(stat for stat in stats if stat["marketplace"] == test_product.marketplace),
(stat for stat in stats if stat["marketplace"] == test_marketplace_product.marketplace),
None,
)
assert test_marketplace_stat is not None
@@ -133,32 +133,32 @@ class TestStatsService:
assert test_marketplace_stat["unique_brands"] >= 1
def test_get_marketplace_breakdown_stats_multiple_marketplaces(
self, db, test_product
self, db, test_marketplace_product
):
"""Test marketplace breakdown with multiple marketplaces"""
# Create products for different marketplaces
marketplace_products = [
Product(
product_id="AMAZON001",
title="Amazon Product 1",
MarketplaceProduct(
marketplace_product_id="AMAZON001",
title="Amazon MarketplaceProduct 1",
brand="AmazonBrand1",
marketplace="Amazon",
shop_name="AmazonShop1",
price="20.00",
currency="EUR",
),
Product(
product_id="AMAZON002",
title="Amazon Product 2",
MarketplaceProduct(
marketplace_product_id="AMAZON002",
title="Amazon MarketplaceProduct 2",
brand="AmazonBrand2",
marketplace="Amazon",
shop_name="AmazonShop2",
price="25.00",
currency="EUR",
),
Product(
product_id="EBAY001",
title="eBay Product",
MarketplaceProduct(
marketplace_product_id="EBAY001",
title="eBay MarketplaceProduct",
brand="eBayBrand",
marketplace="eBay",
shop_name="eBayShop",
@@ -171,11 +171,11 @@ class TestStatsService:
stats = self.service.get_marketplace_breakdown_stats(db)
# Should have at least 3 marketplaces: test_product.marketplace, Amazon, eBay
# Should have at least 3 marketplaces: test_marketplace_product.marketplace, Amazon, eBay
marketplace_names = [stat["marketplace"] for stat in stats]
assert "Amazon" in marketplace_names
assert "eBay" in marketplace_names
assert test_product.marketplace in marketplace_names
assert test_marketplace_product.marketplace in marketplace_names
# Check Amazon stats specifically
amazon_stat = next(stat for stat in stats if stat["marketplace"] == "Amazon")
@@ -192,9 +192,9 @@ class TestStatsService:
def test_get_marketplace_breakdown_stats_excludes_nulls(self, db):
"""Test marketplace breakdown excludes products with null marketplaces"""
# Create product with null marketplace
null_marketplace_product = Product(
product_id="NULLMARKET001",
title="Product without marketplace",
null_marketplace_product = MarketplaceProduct(
marketplace_product_id="NULLMARKET001",
title="MarketplaceProduct without marketplace",
marketplace=None,
shop_name="SomeShop",
brand="SomeBrand",
@@ -212,29 +212,29 @@ class TestStatsService:
]
assert None not in marketplace_names
def test_get_product_count(self, db, test_product):
def test_get_product_count(self, db, test_marketplace_product):
"""Test getting total product count"""
count = self.service._get_product_count(db)
assert count >= 1
assert isinstance(count, int)
def test_get_unique_brands_count(self, db, test_product):
def test_get_unique_brands_count(self, db, test_marketplace_product):
"""Test getting unique brands count"""
# Add products with different brands
brand_products = [
Product(
product_id="BRAND001",
title="Brand Product 1",
MarketplaceProduct(
marketplace_product_id="BRAND001",
title="Brand MarketplaceProduct 1",
brand="BrandA",
marketplace="Test",
shop_name="TestShop",
price="10.00",
currency="EUR",
),
Product(
product_id="BRAND002",
title="Brand Product 2",
MarketplaceProduct(
marketplace_product_id="BRAND002",
title="Brand MarketplaceProduct 2",
brand="BrandB",
marketplace="Test",
shop_name="TestShop",
@@ -249,25 +249,25 @@ class TestStatsService:
assert (
count >= 2
) # At least BrandA and BrandB, plus possibly test_product brand
) # At least BrandA and BrandB, plus possibly test_marketplace_product brand
assert isinstance(count, int)
def test_get_unique_categories_count(self, db, test_product):
def test_get_unique_categories_count(self, db, test_marketplace_product):
"""Test getting unique categories count"""
# Add products with different categories
category_products = [
Product(
product_id="CAT001",
title="Category Product 1",
MarketplaceProduct(
marketplace_product_id="CAT001",
title="Category MarketplaceProduct 1",
google_product_category="Electronics",
marketplace="Test",
shop_name="TestShop",
price="10.00",
currency="EUR",
),
Product(
product_id="CAT002",
title="Category Product 2",
MarketplaceProduct(
marketplace_product_id="CAT002",
title="Category MarketplaceProduct 2",
google_product_category="Books",
marketplace="Test",
shop_name="TestShop",
@@ -283,21 +283,21 @@ class TestStatsService:
assert count >= 2 # At least Electronics and Books
assert isinstance(count, int)
def test_get_unique_marketplaces_count(self, db, test_product):
def test_get_unique_marketplaces_count(self, db, test_marketplace_product):
"""Test getting unique marketplaces count"""
# Add products with different marketplaces
marketplace_products = [
Product(
product_id="MARKET001",
title="Marketplace Product 1",
MarketplaceProduct(
marketplace_product_id="MARKET001",
title="Marketplace MarketplaceProduct 1",
marketplace="Amazon",
shop_name="AmazonShop",
price="10.00",
currency="EUR",
),
Product(
product_id="MARKET002",
title="Marketplace Product 2",
MarketplaceProduct(
marketplace_product_id="MARKET002",
title="Marketplace MarketplaceProduct 2",
marketplace="eBay",
shop_name="eBayShop",
price="15.00",
@@ -309,24 +309,24 @@ class TestStatsService:
count = self.service._get_unique_marketplaces_count(db)
assert count >= 2 # At least Amazon and eBay, plus test_product marketplace
assert count >= 2 # At least Amazon and eBay, plus test_marketplace_product marketplace
assert isinstance(count, int)
def test_get_unique_shops_count(self, db, test_product):
def test_get_unique_shops_count(self, db, test_marketplace_product):
"""Test getting unique shops count"""
# Add products with different shop names
shop_products = [
Product(
product_id="SHOP001",
title="Shop Product 1",
MarketplaceProduct(
marketplace_product_id="SHOP001",
title="Shop MarketplaceProduct 1",
marketplace="Test",
shop_name="ShopA",
price="10.00",
currency="EUR",
),
Product(
product_id="SHOP002",
title="Shop Product 2",
MarketplaceProduct(
marketplace_product_id="SHOP002",
title="Shop MarketplaceProduct 2",
marketplace="Test",
shop_name="ShopB",
price="15.00",
@@ -338,7 +338,7 @@ class TestStatsService:
count = self.service._get_unique_shops_count(db)
assert count >= 2 # At least ShopA and ShopB, plus test_product shop
assert count >= 2 # At least ShopA and ShopB, plus test_marketplace_product shop
assert isinstance(count, int)
def test_get_stock_statistics(self, db, test_stock):
@@ -374,27 +374,27 @@ class TestStatsService:
"""Test getting brands for a specific marketplace"""
# Create products for specific marketplace
marketplace_products = [
Product(
product_id="SPECIFIC001",
title="Specific Product 1",
MarketplaceProduct(
marketplace_product_id="SPECIFIC001",
title="Specific MarketplaceProduct 1",
brand="SpecificBrand1",
marketplace="SpecificMarket",
shop_name="SpecificShop1",
price="10.00",
currency="EUR",
),
Product(
product_id="SPECIFIC002",
title="Specific Product 2",
MarketplaceProduct(
marketplace_product_id="SPECIFIC002",
title="Specific MarketplaceProduct 2",
brand="SpecificBrand2",
marketplace="SpecificMarket",
shop_name="SpecificShop2",
price="15.00",
currency="EUR",
),
Product(
product_id="OTHER001",
title="Other Product",
MarketplaceProduct(
marketplace_product_id="OTHER001",
title="Other MarketplaceProduct",
brand="OtherBrand",
marketplace="OtherMarket",
shop_name="OtherShop",
@@ -416,18 +416,18 @@ class TestStatsService:
"""Test getting shops for a specific marketplace"""
# Create products for specific marketplace
marketplace_products = [
Product(
product_id="SHOPTEST001",
title="Shop Test Product 1",
MarketplaceProduct(
marketplace_product_id="SHOPTEST001",
title="Shop Test MarketplaceProduct 1",
brand="TestBrand",
marketplace="TestMarketplace",
shop_name="TestShop1",
price="10.00",
currency="EUR",
),
Product(
product_id="SHOPTEST002",
title="Shop Test Product 2",
MarketplaceProduct(
marketplace_product_id="SHOPTEST002",
title="Shop Test MarketplaceProduct 2",
brand="TestBrand",
marketplace="TestMarketplace",
shop_name="TestShop2",
@@ -448,25 +448,25 @@ class TestStatsService:
"""Test getting product count for a specific marketplace"""
# Create products for specific marketplace
marketplace_products = [
Product(
product_id="COUNT001",
title="Count Product 1",
MarketplaceProduct(
marketplace_product_id="COUNT001",
title="Count MarketplaceProduct 1",
marketplace="CountMarketplace",
shop_name="CountShop",
price="10.00",
currency="EUR",
),
Product(
product_id="COUNT002",
title="Count Product 2",
MarketplaceProduct(
marketplace_product_id="COUNT002",
title="Count MarketplaceProduct 2",
marketplace="CountMarketplace",
shop_name="CountShop",
price="15.00",
currency="EUR",
),
Product(
product_id="COUNT003",
title="Count Product 3",
MarketplaceProduct(
marketplace_product_id="COUNT003",
title="Count MarketplaceProduct 3",
marketplace="CountMarketplace",
shop_name="CountShop",
price="20.00",