Skip to content

Commit

Permalink
updating pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Dec 9, 2024
1 parent 5244902 commit 9c06e6e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
48 changes: 41 additions & 7 deletions backend/lcfs/tests/other_uses/test_other_uses_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,58 @@


@pytest.fixture
def mock_db_session():
def mock_query_result():
# Setup mock for database query result chain
mock_result = AsyncMock()
mock_result.unique = MagicMock(return_value=mock_result)
mock_result.scalars = MagicMock(return_value=mock_result)
mock_result.all = MagicMock(
return_value=[
MagicMock(
fuel_type_id=1,
fuel_type="Gasoline",
default_carbon_intensity=12.34,
units="L",
unrecognized=False,
fuel_instances=[
MagicMock(
fuel_category=MagicMock(
fuel_category_id=1, category="Petroleum-based"
)
)
],
fuel_codes=[
MagicMock(fuel_code_id=1, fuel_code="FC123", carbon_intensity=10.5)
],
)
]
)
return mock_result


@pytest.fixture
def mock_db_session(mock_query_result):
session = MagicMock(spec=AsyncSession)
execute_result = AsyncMock()
execute_result.unique = MagicMock(return_value=execute_result)
execute_result.scalars = MagicMock(return_value=execute_result)
execute_result.all = MagicMock(return_value=[MagicMock(spec=OtherUses)])
execute_result.first = MagicMock(return_value=MagicMock(spec=OtherUses))
session.execute.return_value = execute_result
session.execute = AsyncMock(return_value=mock_query_result)
return session


@pytest.fixture
def other_uses_repo(mock_db_session):
repo = OtherUsesRepository(db=mock_db_session)

repo.fuel_code_repo = MagicMock()
repo.fuel_code_repo.get_fuel_categories = AsyncMock(return_value=[])
repo.fuel_code_repo.get_fuel_types = AsyncMock(return_value=[])
repo.fuel_code_repo.get_expected_use_types = AsyncMock(return_value=[])

# Mock for local get_formatted_fuel_types method
async def mock_get_formatted_fuel_types():
mock_result = await mock_db_session.execute(AsyncMock())
return mock_result.unique().scalars().all()

repo.get_formatted_fuel_types = AsyncMock(side_effect=mock_get_formatted_fuel_types)

return repo


Expand Down
6 changes: 3 additions & 3 deletions backend/lcfs/web/api/other_uses/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def get_latest_other_uses_by_group_uuid(
)

result = await self.db.execute(query)
return result.scalars().first()
return await result.unique().scalars().first()

@repo_handler
async def get_other_uses(self, compliance_report_id: int) -> List[OtherUsesSchema]:
Expand Down Expand Up @@ -302,7 +302,7 @@ async def get_other_use_version_by_user(
)

result = await self.db.execute(query)
return result.scalars().first()
return await result.scalars().first()

@repo_handler
async def get_formatted_fuel_types(self) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -333,7 +333,7 @@ async def get_formatted_fuel_types(self) -> List[Dict[str, Any]]:
)

result = await self.db.execute(query)
fuel_types = result.unique().scalars().all()
fuel_types = await result.unique().scalars().all()

# Prepare the data in the format matching your schema
formatted_fuel_types = []
Expand Down

0 comments on commit 9c06e6e

Please sign in to comment.