major refactoring adding vendor and customer features

This commit is contained in:
2025-10-11 09:09:25 +02:00
parent f569995883
commit dd16198276
126 changed files with 15109 additions and 3747 deletions

42
tests/fixtures/customer_fixtures.py vendored Normal file
View File

@@ -0,0 +1,42 @@
# tests/fixtures/customer_fixtures.py
import pytest
from models.database.customer import Customer, CustomerAddress
@pytest.fixture
def test_customer(db, test_vendor):
"""Create a test customer"""
customer = Customer(
vendor_id=test_vendor.id,
email="testcustomer@example.com",
hashed_password="hashed_password",
first_name="John",
last_name="Doe",
customer_number="TEST001",
is_active=True,
)
db.add(customer)
db.commit()
db.refresh(customer)
return customer
@pytest.fixture
def test_customer_address(db, test_vendor, test_customer):
"""Create a test customer address"""
address = CustomerAddress(
vendor_id=test_vendor.id,
customer_id=test_customer.id,
address_type="shipping",
first_name="John",
last_name="Doe",
address_line_1="123 Main St",
city="Luxembourg",
postal_code="L-1234",
country="Luxembourg",
is_default=True,
)
db.add(address)
db.commit()
db.refresh(address)
return address

View File

@@ -1,6 +1,5 @@
# tests/fixtures/marketplace_import_job_fixtures.py
import pytest
from models.database.marketplace_import_job import MarketplaceImportJob
@@ -9,7 +8,7 @@ def test_marketplace_import_job(db, test_vendor, test_user):
"""Create a test marketplace import job"""
job = MarketplaceImportJob(
marketplace="amazon",
vendor_name="Test Import Vendor",
# REMOVED: vendor_name field doesn't exist
status="completed",
source_url="https://test-marketplace.example.com/import",
vendor_id=test_vendor.id,
@@ -30,7 +29,7 @@ def create_test_marketplace_import_job(db, vendor_id, user_id, **kwargs):
"""Helper function to create MarketplaceImportJob with defaults"""
defaults = {
"marketplace": "test",
"vendor_name": "Test Vendor",
# REMOVED: name field
"status": "pending",
"source_url": "https://test.example.com/import",
"vendor_id": vendor_id,

View File

@@ -17,7 +17,7 @@ def test_marketplace_product(db):
currency="EUR",
brand="TestBrand",
gtin="1234567890123",
availability="in stock",
availability="in inventory",
marketplace="Letzshop",
vendor_name="TestVendor",
)
@@ -39,7 +39,7 @@ def unique_product(db):
currency="EUR",
brand=f"UniqueBrand_{unique_id}",
gtin=f"123456789{unique_id[:4]}",
availability="in stock",
availability="in inventory",
marketplace="Letzshop",
vendor_name=f"UniqueVendor_{unique_id}",
google_product_category=f"UniqueCategory_{unique_id}",
@@ -89,7 +89,7 @@ def create_unique_marketplace_product_factory():
"price": "15.99",
"currency": "EUR",
"marketplace": "TestMarket",
"vendor_name": "TestVendor",
"name": "TestVendor",
}
defaults.update(kwargs)
@@ -109,15 +109,15 @@ def marketplace_product_factory():
@pytest.fixture
def test_marketplace_product_with_stock(db, test_marketplace_product, test_stock):
"""MarketplaceProduct with associated stock record."""
def test_marketplace_product_with_inventory(db, test_marketplace_product, test_inventory):
"""MarketplaceProduct with associated inventory record."""
# Ensure they're linked by GTIN
if test_marketplace_product.gtin != test_stock.gtin:
test_stock.gtin = test_marketplace_product.gtin
if test_marketplace_product.gtin != test_inventory.gtin:
test_inventory.gtin = test_marketplace_product.gtin
db.commit()
db.refresh(test_stock)
db.refresh(test_inventory)
return {
'marketplace_product': test_marketplace_product,
'stock': test_stock
'inventory': test_inventory
}

View File

@@ -23,7 +23,7 @@ def empty_db(db):
tables_to_clear = [
"marketplace_import_jobs", # Has foreign keys to vendors and users
"products", # Has foreign keys to vendors and products
"stock", # Fixed: singular not plural
"inventory", # Fixed: singular not plural
"products", # Referenced by products
"vendors", # Has foreign key to users
"users" # Base table

View File

@@ -1,28 +1,28 @@
# 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
from models.database.inventory import Inventory
@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
unique_id = str(uuid.uuid4())[:8].upper()
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,
vendor_code=f"TESTVENDOR_{unique_id}",
subdomain=f"testvendor{unique_id.lower()}", # ADDED
name=f"Test Vendor {unique_id.lower()}", # FIXED
owner_user_id=test_user.id,
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
return vendor
return vendor
@pytest.fixture
@@ -30,17 +30,18 @@ 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}",
vendor_code=f"UNIQUEVENDOR_{unique_id.upper()}",
subdomain=f"uniquevendor{unique_id.lower()}", # ADDED
name=f"Unique Test Vendor {unique_id}", # FIXED
description=f"A unique test vendor {unique_id}",
owner_id=test_user.id,
owner_user_id=test_user.id,
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
return vendor
return vendor
@pytest.fixture
@@ -48,16 +49,17 @@ 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,
vendor_code=f"INACTIVE_{unique_id.upper()}",
subdomain=f"inactive{unique_id.lower()}", # ADDED
name=f"Inactive Vendor {unique_id}", # FIXED
owner_user_id=other_user.id,
is_active=False,
is_verified=False,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
return vendor
return vendor
@pytest.fixture
@@ -65,32 +67,30 @@ 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,
vendor_code=f"VERIFIED_{unique_id.upper()}",
subdomain=f"verified{unique_id.lower()}", # ADDED
name=f"Verified Vendor {unique_id}", # FIXED
owner_user_id=other_user.id,
is_active=True,
is_verified=True,
)
db.add(vendor)
db.commit()
db.refresh(vendor)
return 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
vendor_id=test_vendor.id,
marketplace_product_id=unique_product.id,
is_active=True,
price=24.99,
is_featured=False,
min_quantity=1,
)
# 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)
@@ -98,53 +98,54 @@ def test_product(db, test_vendor, unique_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
@pytest.fixture
def test_inventory(db, test_product):
"""Create test inventory entry linked to product."""
unique_id = str(uuid.uuid4())[:8].upper()
inventory = Inventory(
product_id=test_product.id,
vendor_id=test_product.vendor_id,
location=f"WAREHOUSE_A_{unique_id}",
quantity=10,
reserved_quantity=0,
vendor_id=test_vendor.id, # Add vendor_id reference
quantity=100,
reserved_quantity=10,
gtin=test_product.marketplace_product.gtin, # Optional reference
)
db.add(stock)
db.add(inventory)
db.commit()
db.refresh(stock)
return stock
db.refresh(inventory)
return inventory
@pytest.fixture
def multiple_stocks(db, multiple_products, test_vendor):
"""Create multiple stock entries for testing"""
stocks = []
def multiple_inventory_entries(db, multiple_products, test_vendor):
"""Create multiple inventory entries for testing"""
inventory_entries = []
for i, product in enumerate(multiple_products):
stock = Stock(
inventory = Inventory(
gtin=product.gtin,
location=f"LOC_{i}",
quantity=10 + (i * 5), # Different quantities
quantity=10 + (i * 5),
reserved_quantity=i,
vendor_id=test_vendor.id,
)
stocks.append(stock)
inventory_entries.append(inventory)
db.add_all(stocks)
db.add_all(inventory_entries)
db.commit()
for stock in stocks:
db.refresh(stock)
return stocks
for inventory in inventory_entries:
db.refresh(inventory)
return inventory_entries
def create_unique_vendor_factory():
"""Factory function to create unique vendors in tests"""
def _create_vendor(db, owner_id, **kwargs):
def _create_vendor(db, owner_user_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,
"vendor_code": f"FACTORY_{unique_id.upper()}",
"subdomain": f"factory{unique_id.lower()}", # ADDED
"name": f"Factory Vendor {unique_id}", # FIXED
"owner_user_id": owner_user_id,
"is_active": True,
"is_verified": False,
}
@@ -154,7 +155,7 @@ def create_unique_vendor_factory():
db.add(vendor)
db.commit()
db.refresh(vendor)
return vendor
return vendor
return _create_vendor