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,9 +1,11 @@
# tests/test_stock_service.py
import pytest
import uuid
import pytest
from app.services.stock_service import StockService
from models.api_models import StockCreate, StockAdd, StockUpdate
from models.database_models import Stock, Product
from models.api_models import StockAdd, StockCreate, StockUpdate
from models.database_models import Product, Stock
class TestStockService:
@@ -39,7 +41,9 @@ class TestStockService:
assert self.service.normalize_gtin("1234567890123") == "1234567890123" # EAN-13
assert self.service.normalize_gtin("123456789012") == "123456789012" # UPC-A
assert self.service.normalize_gtin("12345678") == "12345678" # EAN-8
assert self.service.normalize_gtin("12345678901234") == "12345678901234" # GTIN-14
assert (
self.service.normalize_gtin("12345678901234") == "12345678901234"
) # GTIN-14
# Test with decimal points (should be removed)
assert self.service.normalize_gtin("1234567890123.0") == "1234567890123"
@@ -49,10 +53,14 @@ class TestStockService:
# Test short GTINs being padded
assert self.service.normalize_gtin("123") == "0000000000123" # Padded to EAN-13
assert self.service.normalize_gtin("12345") == "0000000012345" # Padded to EAN-13
assert (
self.service.normalize_gtin("12345") == "0000000012345"
) # Padded to EAN-13
# Test long GTINs being truncated
assert self.service.normalize_gtin("123456789012345") == "3456789012345" # Truncated to 13
assert (
self.service.normalize_gtin("123456789012345") == "3456789012345"
) # Truncated to 13
def test_normalize_gtin_edge_cases(self):
"""Test GTIN normalization edge cases"""
@@ -61,17 +69,21 @@ class TestStockService:
assert self.service.normalize_gtin(123) == "0000000000123"
# Test mixed valid/invalid characters
assert self.service.normalize_gtin("123-456-789-012") == "123456789012" # Dashes removed
assert self.service.normalize_gtin("123 456 789 012") == "123456789012" # Spaces removed
assert self.service.normalize_gtin("ABC123456789012DEF") == "123456789012" # Letters removed
assert (
self.service.normalize_gtin("123-456-789-012") == "123456789012"
) # Dashes removed
assert (
self.service.normalize_gtin("123 456 789 012") == "123456789012"
) # Spaces removed
assert (
self.service.normalize_gtin("ABC123456789012DEF") == "123456789012"
) # Letters removed
def test_set_stock_new_entry(self, db):
"""Test setting stock for a new GTIN/location combination"""
unique_id = str(uuid.uuid4())[:8]
stock_data = StockCreate(
gtin="1234567890123",
location=f"WAREHOUSE_A_{unique_id}",
quantity=100
gtin="1234567890123", location=f"WAREHOUSE_A_{unique_id}", quantity=100
)
result = self.service.set_stock(db, stock_data)
@@ -85,7 +97,7 @@ class TestStockService:
stock_data = StockCreate(
gtin=test_stock.gtin,
location=test_stock.location, # Use exact same location as test_stock
quantity=200
quantity=200,
)
result = self.service.set_stock(db, stock_data)
@@ -98,9 +110,7 @@ class TestStockService:
def test_set_stock_invalid_gtin(self, db):
"""Test setting stock with invalid GTIN"""
stock_data = StockCreate(
gtin="invalid_gtin",
location="WAREHOUSE_A",
quantity=100
gtin="invalid_gtin", location="WAREHOUSE_A", quantity=100
)
with pytest.raises(ValueError, match="Invalid GTIN format"):
@@ -110,9 +120,7 @@ class TestStockService:
"""Test adding stock for a new GTIN/location combination"""
unique_id = str(uuid.uuid4())[:8]
stock_data = StockAdd(
gtin="1234567890123",
location=f"WAREHOUSE_B_{unique_id}",
quantity=50
gtin="1234567890123", location=f"WAREHOUSE_B_{unique_id}", quantity=50
)
result = self.service.add_stock(db, stock_data)
@@ -127,7 +135,7 @@ class TestStockService:
stock_data = StockAdd(
gtin=test_stock.gtin,
location=test_stock.location, # Use exact same location as test_stock
quantity=25
quantity=25,
)
result = self.service.add_stock(db, stock_data)
@@ -138,11 +146,7 @@ class TestStockService:
def test_add_stock_invalid_gtin(self, db):
"""Test adding stock with invalid GTIN"""
stock_data = StockAdd(
gtin="invalid_gtin",
location="WAREHOUSE_A",
quantity=50
)
stock_data = StockAdd(gtin="invalid_gtin", location="WAREHOUSE_A", quantity=50)
with pytest.raises(ValueError, match="Invalid GTIN format"):
self.service.add_stock(db, stock_data)
@@ -150,12 +154,14 @@ class TestStockService:
def test_remove_stock_success(self, db, test_stock):
"""Test removing stock successfully"""
original_quantity = test_stock.quantity
remove_quantity = min(10, original_quantity) # Ensure we don't remove more than available
remove_quantity = min(
10, original_quantity
) # Ensure we don't remove more than available
stock_data = StockAdd(
gtin=test_stock.gtin,
location=test_stock.location, # Use exact same location as test_stock
quantity=remove_quantity
quantity=remove_quantity,
)
result = self.service.remove_stock(db, stock_data)
@@ -169,20 +175,20 @@ class TestStockService:
stock_data = StockAdd(
gtin=test_stock.gtin,
location=test_stock.location, # Use exact same location as test_stock
quantity=test_stock.quantity + 10 # More than available
quantity=test_stock.quantity + 10, # More than available
)
# Fix: Use more flexible regex pattern
with pytest.raises(ValueError, match="Insufficient stock|Not enough stock|Cannot remove"):
with pytest.raises(
ValueError, match="Insufficient stock|Not enough stock|Cannot remove"
):
self.service.remove_stock(db, stock_data)
def test_remove_stock_nonexistent_entry(self, db):
"""Test removing stock from non-existent GTIN/location"""
unique_id = str(uuid.uuid4())[:8]
stock_data = StockAdd(
gtin="9999999999999",
location=f"NONEXISTENT_{unique_id}",
quantity=10
gtin="9999999999999", location=f"NONEXISTENT_{unique_id}", quantity=10
)
with pytest.raises(ValueError, match="No stock found|Stock not found"):
@@ -190,11 +196,7 @@ class TestStockService:
def test_remove_stock_invalid_gtin(self, db):
"""Test removing stock with invalid GTIN"""
stock_data = StockAdd(
gtin="invalid_gtin",
location="WAREHOUSE_A",
quantity=10
)
stock_data = StockAdd(gtin="invalid_gtin", location="WAREHOUSE_A", quantity=10)
with pytest.raises(ValueError, match="Invalid GTIN format"):
self.service.remove_stock(db, stock_data)
@@ -218,14 +220,10 @@ class TestStockService:
# Create multiple stock entries for the same GTIN with unique locations
stock1 = Stock(
gtin=unique_gtin,
location=f"WAREHOUSE_A_{unique_id}",
quantity=50
gtin=unique_gtin, location=f"WAREHOUSE_A_{unique_id}", quantity=50
)
stock2 = Stock(
gtin=unique_gtin,
location=f"WAREHOUSE_B_{unique_id}",
quantity=30
gtin=unique_gtin, location=f"WAREHOUSE_B_{unique_id}", quantity=30
)
db.add(stock1)
@@ -275,7 +273,9 @@ class TestStockService:
assert len(result) >= 1
# Fix: Handle case sensitivity in comparison
assert all(stock.location.upper() == test_stock.location.upper() for stock in result)
assert all(
stock.location.upper() == test_stock.location.upper() for stock in result
)
def test_get_all_stock_with_gtin_filter(self, db, test_stock):
"""Test getting all stock with GTIN filter"""
@@ -293,14 +293,16 @@ class TestStockService:
stock = Stock(
gtin=f"1234567890{i:03d}", # Creates valid 13-digit GTINs: 1234567890000, 1234567890001, etc.
location=f"WAREHOUSE_{unique_prefix}_{i}",
quantity=10
quantity=10,
)
db.add(stock)
db.commit()
result = self.service.get_all_stock(db, skip=2, limit=2)
assert len(result) <= 2 # Should be at most 2, might be less if other records exist
assert (
len(result) <= 2
) # Should be at most 2, might be less if other records exist
def test_update_stock_success(self, db, test_stock):
"""Test updating stock quantity"""
@@ -359,7 +361,7 @@ def test_product_with_stock(db, test_stock):
gtin=test_stock.gtin,
price="29.99",
brand="TestBrand",
marketplace="Letzshop"
marketplace="Letzshop",
)
db.add(product)
db.commit()