code quality run

This commit is contained in:
2025-09-13 21:58:54 +02:00
parent 0dfd885847
commit 3eb18ef91e
63 changed files with 1802 additions and 1289 deletions

View File

@@ -1,16 +1,18 @@
# tests/conftest.py
import uuid
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from app.core.database import Base, get_db
from main import app
from app.core.database import get_db, Base
# Import all models to ensure they're registered with Base metadata
from models.database_models import User, Product, Stock, Shop, MarketplaceImportJob, ShopProduct
from middleware.auth import AuthManager
import uuid
# Import all models to ensure they're registered with Base metadata
from models.database_models import (MarketplaceImportJob, Product, Shop,
ShopProduct, Stock, User)
# Use in-memory SQLite database for tests
SQLALCHEMY_TEST_DATABASE_URL = "sqlite:///:memory:"
@@ -23,7 +25,7 @@ def engine():
SQLALCHEMY_TEST_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
echo=False # Set to True for SQL debugging
echo=False, # Set to True for SQL debugging
)
@@ -89,7 +91,7 @@ def test_user(db, auth_manager):
username=f"testuser_{unique_id}",
hashed_password=hashed_password,
role="user",
is_active=True
is_active=True,
)
db.add(user)
db.commit()
@@ -107,7 +109,7 @@ def test_admin(db, auth_manager):
username=f"admin_{unique_id}",
hashed_password=hashed_password,
role="admin",
is_active=True
is_active=True,
)
db.add(admin)
db.commit()
@@ -118,10 +120,10 @@ def test_admin(db, auth_manager):
@pytest.fixture
def auth_headers(client, test_user):
"""Get authentication headers for test user"""
response = client.post("/api/v1/auth/login", json={
"username": test_user.username,
"password": "testpass123"
})
response = client.post(
"/api/v1/auth/login",
json={"username": test_user.username, "password": "testpass123"},
)
assert response.status_code == 200, f"Login failed: {response.text}"
token = response.json()["access_token"]
return {"Authorization": f"Bearer {token}"}
@@ -130,10 +132,10 @@ def auth_headers(client, test_user):
@pytest.fixture
def admin_headers(client, test_admin):
"""Get authentication headers for admin user"""
response = client.post("/api/v1/auth/login", json={
"username": test_admin.username,
"password": "adminpass123"
})
response = client.post(
"/api/v1/auth/login",
json={"username": test_admin.username, "password": "adminpass123"},
)
assert response.status_code == 200, f"Admin login failed: {response.text}"
token = response.json()["access_token"]
return {"Authorization": f"Bearer {token}"}
@@ -152,7 +154,7 @@ def test_product(db):
gtin="1234567890123",
availability="in stock",
marketplace="Letzshop",
shop_name="TestShop"
shop_name="TestShop",
)
db.add(product)
db.commit()
@@ -169,7 +171,7 @@ def test_shop(db, test_user):
shop_name=f"Test Shop {unique_id}",
owner_id=test_user.id,
is_active=True,
is_verified=True
is_verified=True,
)
db.add(shop)
db.commit()
@@ -186,7 +188,7 @@ def test_stock(db, test_product, test_shop):
location=f"WAREHOUSE_A_{unique_id}",
quantity=10,
reserved_quantity=0,
shop_id=test_shop.id # Add shop_id reference
shop_id=test_shop.id, # Add shop_id reference
)
db.add(stock)
db.commit()
@@ -208,7 +210,7 @@ def test_marketplace_job(db, test_shop, test_user): # Add test_shop dependency
updated_count=3,
total_processed=8,
error_count=0,
error_message=None
error_message=None,
)
db.add(job)
db.commit()
@@ -219,16 +221,16 @@ def test_marketplace_job(db, test_shop, test_user): # Add test_shop dependency
def create_test_import_job(db, shop_id, **kwargs): # Add shop_id parameter
"""Helper function to create MarketplaceImportJob with defaults"""
defaults = {
'marketplace': 'test',
'shop_name': 'Test Shop',
'status': 'pending',
'source_url': 'https://test.example.com/import',
'shop_id': shop_id, # Add required shop_id
'imported_count': 0,
'updated_count': 0,
'total_processed': 0,
'error_count': 0,
'error_message': None
"marketplace": "test",
"shop_name": "Test Shop",
"status": "pending",
"source_url": "https://test.example.com/import",
"shop_id": shop_id, # Add required shop_id
"imported_count": 0,
"updated_count": 0,
"total_processed": 0,
"error_count": 0,
"error_message": None,
}
defaults.update(kwargs)
@@ -250,6 +252,7 @@ def cleanup():
# Add these fixtures to your existing conftest.py
@pytest.fixture
def unique_product(db):
"""Create a unique product for tests that need isolated product data"""
@@ -265,7 +268,7 @@ def unique_product(db):
availability="in stock",
marketplace="Letzshop",
shop_name=f"UniqueShop_{unique_id}",
google_product_category=f"UniqueCategory_{unique_id}"
google_product_category=f"UniqueCategory_{unique_id}",
)
db.add(product)
db.commit()
@@ -283,7 +286,7 @@ def unique_shop(db, test_user):
description=f"A unique test shop {unique_id}",
owner_id=test_user.id,
is_active=True,
is_verified=True
is_verified=True,
)
db.add(shop)
db.commit()
@@ -301,7 +304,7 @@ def other_user(db, auth_manager):
username=f"otheruser_{unique_id}",
hashed_password=hashed_password,
role="user",
is_active=True
is_active=True,
)
db.add(user)
db.commit()
@@ -318,7 +321,7 @@ def inactive_shop(db, other_user):
shop_name=f"Inactive Shop {unique_id}",
owner_id=other_user.id,
is_active=False,
is_verified=False
is_verified=False,
)
db.add(shop)
db.commit()
@@ -335,7 +338,7 @@ def verified_shop(db, other_user):
shop_name=f"Verified Shop {unique_id}",
owner_id=other_user.id,
is_active=True,
is_verified=True
is_verified=True,
)
db.add(shop)
db.commit()
@@ -347,16 +350,14 @@ def verified_shop(db, other_user):
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
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, 'price'):
if hasattr(ShopProduct, "price"):
shop_product.price = "24.99"
if hasattr(ShopProduct, 'is_featured'):
if hasattr(ShopProduct, "is_featured"):
shop_product.is_featured = False
if hasattr(ShopProduct, 'stock_quantity'):
if hasattr(ShopProduct, "stock_quantity"):
shop_product.stock_quantity = 10
db.add(shop_product)
@@ -382,7 +383,7 @@ def multiple_products(db):
marketplace=f"MultiMarket_{i % 2}", # Create 2 different marketplaces
shop_name=f"MultiShop_{i}",
google_product_category=f"MultiCategory_{i % 2}", # Create 2 different categories
gtin=f"1234567890{i}{unique_id[:2]}"
gtin=f"1234567890{i}{unique_id[:2]}",
)
products.append(product)
@@ -404,7 +405,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
shop_id=test_shop.id,
)
stocks.append(stock)
@@ -422,12 +423,12 @@ def create_unique_product_factory():
def _create_product(db, **kwargs):
unique_id = str(uuid.uuid4())[:8]
defaults = {
'product_id': f"FACTORY_{unique_id}",
'title': f"Factory Product {unique_id}",
'price': "15.99",
'currency': "EUR",
'marketplace': "TestMarket",
'shop_name': "TestShop"
"product_id": f"FACTORY_{unique_id}",
"title": f"Factory Product {unique_id}",
"price": "15.99",
"currency": "EUR",
"marketplace": "TestMarket",
"shop_name": "TestShop",
}
defaults.update(kwargs)
@@ -452,11 +453,11 @@ def create_unique_shop_factory():
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
"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)
@@ -473,4 +474,3 @@ def create_unique_shop_factory():
def shop_factory():
"""Fixture that provides a shop factory function"""
return create_unique_shop_factory()