Fix new services implementation 3

This commit is contained in:
Urtzi Alfaro
2025-08-14 16:47:34 +02:00
parent 0951547e92
commit 03737430ee
51 changed files with 657 additions and 982 deletions

View File

@@ -91,6 +91,7 @@ def sample_sales_data(sample_tenant_id: UUID) -> SalesDataCreate:
"""Sample sales data for testing"""
return SalesDataCreate(
date=datetime.now(timezone.utc),
inventory_product_id="550e8400-e29b-41d4-a716-446655440000",
product_name="Pan Integral",
product_category="Panadería",
product_sku="PAN001",
@@ -117,6 +118,7 @@ def sample_sales_records(sample_tenant_id: UUID) -> list[dict]:
{
"tenant_id": sample_tenant_id,
"date": base_date,
"inventory_product_id": "550e8400-e29b-41d4-a716-446655440001",
"product_name": "Croissant",
"quantity_sold": 3,
"revenue": Decimal("7.50"),
@@ -126,6 +128,7 @@ def sample_sales_records(sample_tenant_id: UUID) -> list[dict]:
{
"tenant_id": sample_tenant_id,
"date": base_date,
"inventory_product_id": "550e8400-e29b-41d4-a716-446655440002",
"product_name": "Café Americano",
"quantity_sold": 2,
"revenue": Decimal("5.00"),
@@ -135,6 +138,7 @@ def sample_sales_records(sample_tenant_id: UUID) -> list[dict]:
{
"tenant_id": sample_tenant_id,
"date": base_date,
"inventory_product_id": "550e8400-e29b-41d4-a716-446655440003",
"product_name": "Bocadillo Jamón",
"quantity_sold": 1,
"revenue": Decimal("4.50"),
@@ -229,6 +233,7 @@ def performance_test_data(sample_tenant_id: UUID) -> list[dict]:
records.append({
"tenant_id": sample_tenant_id,
"date": base_date,
"inventory_product_id": f"550e8400-e29b-41d4-a716-{i:012x}",
"product_name": f"Test Product {i % 20}",
"quantity_sold": (i % 10) + 1,
"revenue": Decimal(str(((i % 10) + 1) * 2.5)),

View File

@@ -26,7 +26,7 @@ class TestSalesRepository:
assert record is not None
assert record.id is not None
assert record.tenant_id == sample_tenant_id
assert record.product_name == sample_sales_data.product_name
assert record.inventory_product_id == sample_sales_data.inventory_product_id
assert record.quantity_sold == sample_sales_data.quantity_sold
assert record.revenue == sample_sales_data.revenue
@@ -42,7 +42,7 @@ class TestSalesRepository:
assert retrieved_record is not None
assert retrieved_record.id == created_record.id
assert retrieved_record.product_name == created_record.product_name
assert retrieved_record.inventory_product_id == created_record.inventory_product_id
async def test_get_by_tenant(self, populated_db, sample_tenant_id):
"""Test getting records by tenant"""
@@ -57,10 +57,12 @@ class TestSalesRepository:
"""Test getting records by product"""
repository = SalesRepository(populated_db)
records = await repository.get_by_product(sample_tenant_id, "Croissant")
# Get by inventory_product_id instead of product name
test_product_id = "550e8400-e29b-41d4-a716-446655440001"
records = await repository.get_by_inventory_product_id(sample_tenant_id, test_product_id)
assert len(records) == 1
assert records[0].product_name == "Croissant"
assert records[0].inventory_product_id == test_product_id
async def test_update_record(self, test_db_session, sample_tenant_id, sample_sales_data):
"""Test updating a sales record"""
@@ -71,6 +73,7 @@ class TestSalesRepository:
# Update record
update_data = SalesDataUpdate(
inventory_product_id="550e8400-e29b-41d4-a716-446655440999",
product_name="Updated Product",
quantity_sold=10,
revenue=Decimal("25.00")
@@ -78,7 +81,7 @@ class TestSalesRepository:
updated_record = await repository.update(created_record.id, update_data.model_dump(exclude_unset=True))
assert updated_record.product_name == "Updated Product"
assert updated_record.inventory_product_id == "550e8400-e29b-41d4-a716-446655440999"
assert updated_record.quantity_sold == 10
assert updated_record.revenue == Decimal("25.00")
@@ -137,7 +140,7 @@ class TestSalesRepository:
repository = SalesRepository(populated_db)
query = SalesDataQuery(
product_name="Croissant",
inventory_product_id="550e8400-e29b-41d4-a716-446655440001",
limit=10,
offset=0
)
@@ -145,7 +148,7 @@ class TestSalesRepository:
records = await repository.get_by_tenant(sample_tenant_id, query)
assert len(records) == 1
assert records[0].product_name == "Croissant"
assert records[0].inventory_product_id == "550e8400-e29b-41d4-a716-446655440001"
async def test_bulk_create(self, test_db_session, sample_tenant_id):
"""Test bulk creating records"""
@@ -155,6 +158,7 @@ class TestSalesRepository:
bulk_data = [
{
"date": datetime.now(timezone.utc),
"inventory_product_id": f"550e8400-e29b-41d4-a716-{i+100:012x}",
"product_name": f"Product {i}",
"quantity_sold": i + 1,
"revenue": Decimal(str((i + 1) * 2.5)),

View File

@@ -31,7 +31,7 @@ class TestSalesService:
mock_repository = AsyncMock()
mock_record = AsyncMock()
mock_record.id = uuid4()
mock_record.product_name = sample_sales_data.product_name
mock_record.inventory_product_id = sample_sales_data.inventory_product_id
mock_repository.create_sales_record.return_value = mock_record
with patch('app.services.sales_service.SalesRepository', return_value=mock_repository):
@@ -49,6 +49,7 @@ class TestSalesService:
# Create invalid sales data (future date)
invalid_data = SalesDataCreate(
date=datetime(2030, 1, 1, tzinfo=timezone.utc), # Future date
inventory_product_id="550e8400-e29b-41d4-a716-446655440000",
product_name="Test Product",
quantity_sold=1,
revenue=Decimal("5.00")
@@ -61,6 +62,7 @@ class TestSalesService:
"""Test updating a sales record"""
record_id = uuid4()
update_data = SalesDataUpdate(
inventory_product_id="550e8400-e29b-41d4-a716-446655440999",
product_name="Updated Product",
quantity_sold=10
)
@@ -78,7 +80,7 @@ class TestSalesService:
# Mock updated record
mock_updated = AsyncMock()
mock_updated.product_name = "Updated Product"
mock_updated.inventory_product_id = "550e8400-e29b-41d4-a716-446655440999"
mock_repository.update.return_value = mock_updated
with patch('app.services.sales_service.SalesRepository', return_value=mock_repository):
@@ -88,13 +90,13 @@ class TestSalesService:
sample_tenant_id
)
assert result.product_name == "Updated Product"
assert result.inventory_product_id == "550e8400-e29b-41d4-a716-446655440999"
mock_repository.update.assert_called_once()
async def test_update_nonexistent_record(self, sales_service, sample_tenant_id):
"""Test updating a non-existent record"""
record_id = uuid4()
update_data = SalesDataUpdate(product_name="Updated Product")
update_data = SalesDataUpdate(inventory_product_id="550e8400-e29b-41d4-a716-446655440999", product_name="Updated Product")
with patch('app.services.sales_service.get_db_transaction') as mock_get_db:
mock_db = AsyncMock()
@@ -195,7 +197,7 @@ class TestSalesService:
async def test_get_product_sales(self, sales_service, sample_tenant_id):
"""Test getting sales for specific product"""
product_name = "Test Product"
inventory_product_id = "550e8400-e29b-41d4-a716-446655440000"
with patch('app.services.sales_service.get_db_transaction') as mock_get_db:
mock_db = AsyncMock()
@@ -206,7 +208,7 @@ class TestSalesService:
mock_repository.get_by_product.return_value = mock_records
with patch('app.services.sales_service.SalesRepository', return_value=mock_repository):
result = await sales_service.get_product_sales(sample_tenant_id, product_name)
result = await sales_service.get_product_sales(sample_tenant_id, inventory_product_id)
assert len(result) == 2
mock_repository.get_by_product.assert_called_once()
@@ -268,6 +270,7 @@ class TestSalesService:
# Test revenue mismatch detection
sales_data = SalesDataCreate(
date=datetime.now(timezone.utc),
inventory_product_id="550e8400-e29b-41d4-a716-446655440000",
product_name="Test Product",
quantity_sold=5,
unit_price=Decimal("2.00"),