Fix issues
This commit is contained in:
0
services/data/tests/__init__.py
Normal file
0
services/data/tests/__init__.py
Normal file
82
services/data/tests/conftest.py
Normal file
82
services/data/tests/conftest.py
Normal file
@@ -0,0 +1,82 @@
|
||||
# ================================================================
|
||||
# services/data/tests/conftest.py
|
||||
# ================================================================
|
||||
"""Test configuration for data service"""
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
from fastapi.testclient import TestClient
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from app.main import app
|
||||
from app.core.database import Base, get_db
|
||||
from app.models.sales import SalesData
|
||||
from app.models.weather import WeatherData, WeatherForecast
|
||||
from app.models.traffic import TrafficData
|
||||
|
||||
# Test database URL
|
||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||
|
||||
# Create test engine
|
||||
test_engine = create_async_engine(
|
||||
TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
|
||||
TestingSessionLocal = async_sessionmaker(
|
||||
test_engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False
|
||||
)
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db():
|
||||
"""Create test database session"""
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
async with TestingSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Create test client"""
|
||||
async def override_get_db():
|
||||
async with TestingSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
@pytest.fixture
|
||||
def test_tenant_id():
|
||||
"""Test tenant ID"""
|
||||
return uuid.uuid4()
|
||||
|
||||
@pytest.fixture
|
||||
def test_sales_data():
|
||||
"""Sample sales data for testing"""
|
||||
return {
|
||||
"date": datetime.now(),
|
||||
"product_name": "Pan Integral",
|
||||
"quantity_sold": 25,
|
||||
"revenue": 37.50,
|
||||
"location_id": "madrid_centro",
|
||||
"source": "test"
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def mock_auth_token():
|
||||
"""Mock authentication token"""
|
||||
return "Bearer test-token-123"
|
||||
94
services/data/tests/test_data.py
Normal file
94
services/data/tests/test_data.py
Normal file
@@ -0,0 +1,94 @@
|
||||
# ================================================================
|
||||
# services/data/tests/test_data.py
|
||||
# ================================================================
|
||||
"""Integration tests for data service"""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from datetime import datetime, timedelta
|
||||
import uuid
|
||||
|
||||
from app.services.sales_service import SalesService
|
||||
from app.services.weather_service import WeatherService
|
||||
from app.services.traffic_service import TrafficService
|
||||
from app.schemas.sales import SalesDataCreate, SalesDataQuery
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_sales_record(db: AsyncSession, test_tenant_id, test_sales_data):
|
||||
"""Test creating a sales record"""
|
||||
sales_data = SalesDataCreate(
|
||||
tenant_id=test_tenant_id,
|
||||
**test_sales_data
|
||||
)
|
||||
|
||||
record = await SalesService.create_sales_record(sales_data, db)
|
||||
|
||||
assert record.id is not None
|
||||
assert record.tenant_id == test_tenant_id
|
||||
assert record.product_name == test_sales_data["product_name"]
|
||||
assert record.quantity_sold == test_sales_data["quantity_sold"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_query_sales_data(db: AsyncSession, test_tenant_id, test_sales_data):
|
||||
"""Test querying sales data"""
|
||||
# Create test record
|
||||
sales_data = SalesDataCreate(
|
||||
tenant_id=test_tenant_id,
|
||||
**test_sales_data
|
||||
)
|
||||
await SalesService.create_sales_record(sales_data, db)
|
||||
|
||||
# Query data
|
||||
query = SalesDataQuery(
|
||||
tenant_id=test_tenant_id,
|
||||
start_date=datetime.now() - timedelta(days=1),
|
||||
end_date=datetime.now() + timedelta(days=1)
|
||||
)
|
||||
|
||||
records = await SalesService.get_sales_data(query, db)
|
||||
|
||||
assert len(records) == 1
|
||||
assert records[0].product_name == test_sales_data["product_name"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_csv_import():
|
||||
"""Test CSV data import"""
|
||||
csv_content = """date,product,quantity,revenue
|
||||
2024-01-15,Pan Integral,25,37.50
|
||||
2024-01-15,Croissant,15,22.50"""
|
||||
|
||||
# This would normally use the database, but we're testing the parsing logic
|
||||
import io
|
||||
import pandas as pd
|
||||
|
||||
csv_file = io.StringIO(csv_content)
|
||||
df = pd.read_csv(csv_file)
|
||||
|
||||
assert len(df) == 2
|
||||
assert df.iloc[0]['product'] == 'Pan Integral'
|
||||
assert df.iloc[0]['quantity'] == 25
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_weather_service():
|
||||
"""Test weather service"""
|
||||
weather_service = WeatherService()
|
||||
|
||||
# Test current weather (should return synthetic data)
|
||||
current = await weather_service.get_current_weather(40.4168, -3.7038)
|
||||
|
||||
assert current is not None
|
||||
assert current.temperature is not None
|
||||
assert current.source in ['aemet', 'synthetic']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_traffic_service():
|
||||
"""Test traffic service"""
|
||||
traffic_service = TrafficService()
|
||||
|
||||
# Test current traffic (should return synthetic data)
|
||||
current = await traffic_service.get_current_traffic(40.4168, -3.7038)
|
||||
|
||||
assert current is not None
|
||||
assert current.traffic_volume is not None
|
||||
assert current.congestion_level in ['low', 'medium', 'high']
|
||||
87
services/data/tests/test_external.py
Normal file
87
services/data/tests/test_external.py
Normal file
@@ -0,0 +1,87 @@
|
||||
# ================================================================
|
||||
# services/data/tests/test_external.py
|
||||
# ================================================================
|
||||
"""Tests for external API clients"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.external.aemet import AEMETClient
|
||||
from app.external.madrid_opendata import MadridOpenDataClient
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_aemet_client_synthetic_weather():
|
||||
"""Test AEMET client synthetic weather generation"""
|
||||
client = AEMETClient()
|
||||
|
||||
weather = await client._generate_synthetic_weather()
|
||||
|
||||
assert weather is not None
|
||||
assert 'temperature' in weather
|
||||
assert 'precipitation' in weather
|
||||
assert 'humidity' in weather
|
||||
assert weather['source'] == 'synthetic'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_aemet_client_synthetic_forecast():
|
||||
"""Test AEMET client synthetic forecast"""
|
||||
client = AEMETClient()
|
||||
|
||||
forecast = await client._generate_synthetic_forecast(7)
|
||||
|
||||
assert len(forecast) == 7
|
||||
assert all('forecast_date' in item for item in forecast)
|
||||
assert all('temperature' in item for item in forecast)
|
||||
assert all(item['source'] == 'synthetic' for item in forecast)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_madrid_client_synthetic_traffic():
|
||||
"""Test Madrid Open Data client synthetic traffic"""
|
||||
client = MadridOpenDataClient()
|
||||
|
||||
traffic = await client._generate_synthetic_traffic(40.4168, -3.7038)
|
||||
|
||||
assert traffic is not None
|
||||
assert 'traffic_volume' in traffic
|
||||
assert 'pedestrian_count' in traffic
|
||||
assert 'congestion_level' in traffic
|
||||
assert traffic['congestion_level'] in ['low', 'medium', 'high']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_madrid_client_historical_traffic():
|
||||
"""Test Madrid Open Data client historical traffic"""
|
||||
client = MadridOpenDataClient()
|
||||
|
||||
start_date = datetime.now() - timedelta(days=7)
|
||||
end_date = datetime.now()
|
||||
|
||||
historical = await client._generate_historical_traffic(
|
||||
40.4168, -3.7038, start_date, end_date
|
||||
)
|
||||
|
||||
assert len(historical) > 0
|
||||
assert all('traffic_volume' in item for item in historical)
|
||||
assert all('date' in item for item in historical)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_distance_calculation():
|
||||
"""Test distance calculation in AEMET client"""
|
||||
client = AEMETClient()
|
||||
|
||||
# Distance between Madrid Centro and Madrid Norte (approximately)
|
||||
distance = client._calculate_distance(40.4168, -3.7038, 40.4518, -3.7246)
|
||||
|
||||
# Should be roughly 4-5 km
|
||||
assert 3 < distance < 6
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_madrid_area_detection():
|
||||
"""Test Madrid area detection"""
|
||||
client = AEMETClient()
|
||||
|
||||
# Madrid coordinates should be detected as Madrid area
|
||||
assert client._is_in_madrid_area(40.4168, -3.7038) == True
|
||||
|
||||
# Barcelona coordinates should not
|
||||
assert client._is_in_madrid_area(41.3851, 2.1734) == False
|
||||
Reference in New Issue
Block a user