Skip to content

Commit

Permalink
feat: refactored fuel calculations for supply and export, added tests…
Browse files Browse the repository at this point in the history
… to cover compliance unit cases
  • Loading branch information
Alex Zorkin committed Nov 19, 2024
1 parent 58fd9b7 commit 04bcc88
Show file tree
Hide file tree
Showing 19 changed files with 1,837 additions and 692 deletions.
4 changes: 3 additions & 1 deletion backend/lcfs/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import structlog
import warnings

# Suppress the PendingDeprecationWarning for multipart
warnings.filterwarnings(
"ignore",
message="Please use `import python_multipart` instead.",
category=PendingDeprecationWarning
category=PendingDeprecationWarning,
)
import subprocess
import warnings
Expand Down Expand Up @@ -43,6 +44,7 @@

logger = structlog.get_logger(__name__)


@pytest.fixture(scope="session")
def anyio_backend() -> str:
"""
Expand Down
47 changes: 25 additions & 22 deletions backend/lcfs/tests/fuel_export/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from unittest.mock import AsyncMock, MagicMock
from lcfs.web.api.fuel_export.repo import FuelExportRepository
from lcfs.web.api.fuel_code.repo import FuelCodeRepository
from lcfs.web.api.fuel_export.services import FuelExportServices
from lcfs.web.api.fuel_export.actions_service import FuelExportActionService
from fastapi_cache import FastAPICache
Expand All @@ -11,18 +12,20 @@

@pytest.fixture(scope="function", autouse=True)
async def init_cache():
"""Initialize the cache for testing."""
FastAPICache.init(InMemoryBackend(), prefix="test-cache")


@pytest.fixture
def mock_db():
"""Mock the AsyncSession for database interactions."""
return AsyncMock(spec=AsyncSession)


@pytest.fixture
def mock_repo():
repo = AsyncMock(spec=FuelExportRepository())
# Add specific async mock methods that need to be available
def mock_repo(mock_db):
"""Mock FuelExportRepository."""
repo = FuelExportRepository(db=mock_db)
repo.get_fuel_export_table_options = AsyncMock()
repo.get_fuel_export_list = AsyncMock()
repo.get_fuel_exports_paginated = AsyncMock()
Expand All @@ -38,17 +41,17 @@ def mock_repo():

@pytest.fixture
def mock_compliance_report_repo():
return AsyncMock(spec=ComplianceReportRepository)
"""Mock ComplianceReportRepository."""
repo = AsyncMock(spec=ComplianceReportRepository)
return repo


@pytest.fixture
def fuel_export_service(mock_user_profile, mock_repo, mock_compliance_report_repo):
service = FuelExportServices()
service.repo = mock_repo
service.compliance_report_repo = mock_compliance_report_repo
service.request = MagicMock()
service.request.user = mock_user_profile
return service
def mock_fuel_code_repo():
"""Mock FuelCodeRepository."""
repo = AsyncMock(spec=FuelCodeRepository)
repo.get_standardized_fuel_data = AsyncMock()
return repo


@pytest.fixture
Expand All @@ -59,23 +62,23 @@ def fuel_export_repo(mock_db):


@pytest.fixture
def mock_fuel_export_services():
services = AsyncMock(spec=FuelExportServices)
# Add specific methods that need to be available
services.validate_and_calculate_compliance_units = AsyncMock()
services.get_fuel_export_options = AsyncMock()
return services
def fuel_export_service(mock_repo, mock_compliance_report_repo):
"""Mock FuelExportServices."""
service = FuelExportServices(
repo=mock_repo,
compliance_report_repo=mock_compliance_report_repo,
)
return service


@pytest.fixture
def fuel_export_action_service(mock_repo, mock_fuel_export_services):
service = FuelExportActionService(
repo=mock_repo, fuel_export_services=mock_fuel_export_services
)
def fuel_export_action_service(mock_repo, mock_fuel_code_repo):
"""Mock FuelExportActionService."""
service = FuelExportActionService(repo=mock_repo, fuel_repo=mock_fuel_code_repo)
return service


@pytest.fixture
def mock_user_profile():
"""Mock user profile with minimal required attributes"""
"""Mock user profile with minimal required attributes."""
return MagicMock(id=1, organization_id=1, user_type="SUPPLIER")
Loading

0 comments on commit 04bcc88

Please sign in to comment.