Skip to content

Commit

Permalink
fix generate_report_service 💯
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-mk committed Dec 16, 2024
1 parent b50b7e4 commit 581be38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from django.conf import settings
from django.core.files import File
from django.db import models
from django.db.models import (
Case,
Count,
Expand Down Expand Up @@ -273,7 +272,9 @@ def get_payment_verifications(report: Report) -> QuerySet:
"payment_verification_plan__completion_date__date__range": (report.date_from, report.date_to),
}
if report.program:
pp_program_ids = list(PaymentPlan.objects.filter(program=report.program).values_list("id", flat=True))
pp_program_ids = list(
PaymentPlan.objects.filter(program_cycle__program=report.program).values_list("id", flat=True)
)
filter_vars["payment_verification_plan__payment_plan_id__in"] = pp_program_ids
return PaymentVerification.objects.filter(**filter_vars)

Expand Down Expand Up @@ -330,21 +331,23 @@ def get_payments_for_individuals(report: Report) -> QuerySet:
if report.admin_area.all().exists():
filter_q &= Q(household__admin_area__in=report.admin_area.all())
if report.program:
filter_q &= Q(household__payment__parent__program=report.program)
filter_q &= Q(household__payment__parent__program_cycle__program=report.program)

return (
Individual.objects.filter(filter_q)
.annotate(first_delivery_date_payment=Min("household__payment__delivery_date"))
.annotate(
first_delivery_date=Least(
F("first_delivery_date_payment"),
Value("2099-12-31"),
output_field=DateTimeField(),
)
)
.annotate(last_delivery_date_payment=Max("household__payment__delivery_date"))
.annotate(
last_delivery_date=Greatest(
F("last_delivery_date_payment"),
Value("1990-01-01"),
output_field=DateTimeField(),
)
)
Expand All @@ -359,10 +362,7 @@ def get_payments_for_individuals(report: Report) -> QuerySet:
)
)
)
.annotate(
payment_currency="household__payment__currency",
output_field=models.CharField(),
)
.annotate(payment_currency=F("household__payment__currency"))
.annotate(
total_delivered_quantity_local=Coalesce(
Sum("household__payment__delivered_quantity"), Value(0), output_field=DecimalField()
Expand Down
29 changes: 14 additions & 15 deletions tests/unit/apps/reporting/test_report_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Any
from unittest.mock import patch

from django.conf import settings
from django.test import TestCase
Expand Down Expand Up @@ -172,13 +171,13 @@ def setUpTestData(self) -> None:
("payments_filter_admin_area", Report.PAYMENTS, True, False, 1),
("payment_verifications_no_filter", Report.PAYMENT_VERIFICATION, False, False, 2),
("payment_verifications_program", Report.PAYMENT_VERIFICATION, False, True, 1),
("cash_plans_no_filter", Report.CASH_PLAN, False, False, 2),
("cash_plans_program", Report.CASH_PLAN, False, True, 1),
("cash_plans_no_filter", Report.PAYMENT_PLAN, False, False, 2),
("cash_plans_program", Report.PAYMENT_PLAN, False, True, 1),
("individuals_payments_no_filter", Report.INDIVIDUALS_AND_PAYMENT, False, False, 4),
("individuals_payments_admin_area", Report.INDIVIDUALS_AND_PAYMENT, True, False, 2),
("individuals_payments_program", Report.INDIVIDUALS_AND_PAYMENT, False, True, 2),
("individuals_payments_admin_area_and_program", Report.INDIVIDUALS_AND_PAYMENT, True, True, 2),
("cash_plan_verification", Report.CASH_PLAN_VERIFICATION, False, True, 0),
("cash_plan_verification", Report.CASH_PLAN_VERIFICATION, True, True, 2),
]
)
def test_report_types(
Expand All @@ -202,17 +201,17 @@ def test_report_types(
report.save()

report_service = self.GenerateReportService(report)
with (
patch(
"hct_mis_api.apps.reporting.services.generate_report_service.GenerateReportService.save_wb_file_in_db"
) as mock_save_wb_file_in_db,
patch(
"hct_mis_api.apps.reporting.services.generate_report_service.GenerateReportService.generate_workbook"
) as mock_generate_workbook,
):
report_service.generate_report()
assert mock_generate_workbook.called
assert mock_save_wb_file_in_db.called
# with (
# # patch(
# # "hct_mis_api.apps.reporting.services.generate_report_service.GenerateReportService.save_wb_file_in_db"
# # ) as mock_save_wb_file_in_db,
# # patch(
# # "hct_mis_api.apps.reporting.services.generate_report_service.GenerateReportService.generate_workbook"
# # ) as mock_generate_workbook,
# ):
report_service.generate_report()
# assert mock_generate_workbook.called
# assert mock_save_wb_file_in_db.called
report.refresh_from_db()
self.assertEqual(report.status, Report.COMPLETED)
# self.assertEqual(report.number_of_records, number_of_records) # when mocking generating workbook, this is not set
Expand Down

0 comments on commit 581be38

Please sign in to comment.