From 1399ddf0e66c32ba28798cc11c6653c626e07c93 Mon Sep 17 00:00:00 2001 From: Hamed Valiollahi Date: Tue, 31 Dec 2024 07:28:17 -0800 Subject: [PATCH] fix: resolve 404 error. --- .../audit_log/test_audit_log_services.py | 12 +++++++--- .../test_compliance_report_services.py | 22 +++++++++++++++---- backend/lcfs/web/api/audit_log/services.py | 3 --- .../web/api/compliance_report/services.py | 5 +---- backend/lcfs/web/api/notification/repo.py | 9 +++----- backend/lcfs/web/api/organization/services.py | 5 +---- .../lcfs/web/api/organizations/services.py | 3 --- backend/lcfs/web/api/transaction/services.py | 3 --- 8 files changed, 32 insertions(+), 30 deletions(-) diff --git a/backend/lcfs/tests/audit_log/test_audit_log_services.py b/backend/lcfs/tests/audit_log/test_audit_log_services.py index 92dbee85a..a5e545bc4 100644 --- a/backend/lcfs/tests/audit_log/test_audit_log_services.py +++ b/backend/lcfs/tests/audit_log/test_audit_log_services.py @@ -72,9 +72,15 @@ async def test_get_audit_logs_paginated_no_data(audit_log_service, mock_repo): pagination = PaginationRequestSchema(page=1, size=10, filters=[], sort_orders=[]) mock_repo.get_audit_logs_paginated.return_value = ([], 0) - # Act & Assert - with pytest.raises(DataNotFoundException): - await audit_log_service.get_audit_logs_paginated(pagination) + # Act + result = await audit_log_service.get_audit_logs_paginated(pagination) + + # Assert + assert result.audit_logs == [], "Should return an empty list of audit logs" + assert result.pagination.total == 0, "Total should be zero if no records" + assert result.pagination.page == pagination.page + assert result.pagination.size == pagination.size + assert result.pagination.total_pages == 0 @pytest.mark.anyio diff --git a/backend/lcfs/tests/compliance_report/test_compliance_report_services.py b/backend/lcfs/tests/compliance_report/test_compliance_report_services.py index 9300d2918..2c39bbe2d 100644 --- a/backend/lcfs/tests/compliance_report/test_compliance_report_services.py +++ b/backend/lcfs/tests/compliance_report/test_compliance_report_services.py @@ -116,16 +116,30 @@ async def test_get_compliance_reports_paginated_success( async def test_get_compliance_reports_paginated_not_found( compliance_report_service, mock_repo ): + # Arrange pagination_mock = AsyncMock() pagination_mock.page = 1 pagination_mock.size = 10 + pagination_mock.filters = [] + pagination_mock.sort_orders = [] + # Mock the repository to return no records mock_repo.get_reports_paginated.return_value = ([], 0) - with pytest.raises(DataNotFoundException): - await compliance_report_service.get_compliance_reports_paginated( - pagination_mock - ) + # Act + result = await compliance_report_service.get_compliance_reports_paginated( + pagination_mock + ) + + # Assert: Verify the service returns an empty list and correct pagination metadata + assert result.reports == [], "Expected no compliance reports to be returned" + assert result.pagination.total == 0, "Expected total=0 when there are no records" + assert result.pagination.page == 1, "Page should match the requested page" + assert result.pagination.size == 10, "Size should match the requested size" + assert result.pagination.total_pages == 0, "0 records should yield 0 total_pages" + + # Also verify our repo was called exactly once + mock_repo.get_reports_paginated.assert_called_once() @pytest.mark.anyio diff --git a/backend/lcfs/web/api/audit_log/services.py b/backend/lcfs/web/api/audit_log/services.py index 8a1a81529..d3ff1347a 100644 --- a/backend/lcfs/web/api/audit_log/services.py +++ b/backend/lcfs/web/api/audit_log/services.py @@ -76,9 +76,6 @@ async def get_audit_logs_paginated( offset, limit, conditions, pagination.sort_orders ) - if not audit_logs: - raise DataNotFoundException("No audit logs found") - processed_audit_logs = [] for audit_log in audit_logs: # Extract the changed_fields as a comma-separated string diff --git a/backend/lcfs/web/api/compliance_report/services.py b/backend/lcfs/web/api/compliance_report/services.py index dac78edd9..fbf440d7b 100644 --- a/backend/lcfs/web/api/compliance_report/services.py +++ b/backend/lcfs/web/api/compliance_report/services.py @@ -166,10 +166,7 @@ async def get_compliance_reports_paginated( pagination, organization_id ) - if not reports: - raise DataNotFoundException("No compliance reports found.") - - if bceid_user: + if bceid_user and reports: reports = self._mask_report_status(reports) return ComplianceReportListSchema( diff --git a/backend/lcfs/web/api/notification/repo.py b/backend/lcfs/web/api/notification/repo.py index bd9d874fa..10b238604 100644 --- a/backend/lcfs/web/api/notification/repo.py +++ b/backend/lcfs/web/api/notification/repo.py @@ -124,7 +124,9 @@ def _apply_notification_filters( ) ) elif filter.field == "transaction_id": - field = get_field_for_filter(NotificationMessage, 'related_transaction_id') + field = get_field_for_filter( + NotificationMessage, "related_transaction_id" + ) conditions.append( apply_filter_conditions( field, filter_value, filter_option, filter_type @@ -353,11 +355,6 @@ async def get_notification_channel_subscriptions_by_user( result = await self.db.execute(query) subscriptions = result.scalars().all() - if not subscriptions: - raise DataNotFoundException( - f"Channel subscriptions not found for user id: '{user_profile_id}'" - ) - return subscriptions @repo_handler diff --git a/backend/lcfs/web/api/organization/services.py b/backend/lcfs/web/api/organization/services.py index 5ebf3573d..9371c6347 100644 --- a/backend/lcfs/web/api/organization/services.py +++ b/backend/lcfs/web/api/organization/services.py @@ -102,7 +102,7 @@ def apply_transaction_filters(self, pagination, conditions): # For non-date filters, use the standard filter value filter_value = filter.filter - if field.description == 'transaction_type': + if field.description == "transaction_type": filter_value = filter_value.replace(" ", "").lower() filter_option = filter.type @@ -169,9 +169,6 @@ async def get_transactions_paginated( ) ) - if not transactions: - raise DataNotFoundException("Transactions not found") - return { "transactions": [ TransactionViewSchema.model_validate(transaction) diff --git a/backend/lcfs/web/api/organizations/services.py b/backend/lcfs/web/api/organizations/services.py index 35c2155a3..5014760d5 100644 --- a/backend/lcfs/web/api/organizations/services.py +++ b/backend/lcfs/web/api/organizations/services.py @@ -258,9 +258,6 @@ async def get_organizations( offset, limit, conditions, pagination ) - if not organizations: - raise DataNotFoundException("Organizations not found") - return OrganizationListSchema( organizations=organizations, pagination=PaginationResponseSchema( diff --git a/backend/lcfs/web/api/transaction/services.py b/backend/lcfs/web/api/transaction/services.py index 9c2bf0011..1abdfc2f0 100644 --- a/backend/lcfs/web/api/transaction/services.py +++ b/backend/lcfs/web/api/transaction/services.py @@ -121,9 +121,6 @@ async def get_transactions_paginated( offset, limit, conditions, pagination.sort_orders, None ) - if not transactions: - raise DataNotFoundException("Transactions not found") - return { "transactions": [ TransactionViewSchema.model_validate(transaction)