Skip to content

Commit

Permalink
updating pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Dec 11, 2024
1 parent 44d5492 commit 9331c73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
27 changes: 20 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,14 +11,19 @@


@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(spec=OtherUses)])
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


Expand All @@ -29,6 +34,14 @@ def other_uses_repo(mock_db_session):
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 9331c73

Please sign in to comment.