Skip to content

Commit

Permalink
unit tests ⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-mk committed Dec 21, 2024
1 parent 7ef29ed commit 02cc948
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
35 changes: 31 additions & 4 deletions src/hct_mis_api/apps/payment/models/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def household_list(self) -> "QuerySet":
2)
"""
all_households = Household.objects.filter(business_area=self.business_area, program=self.program_cycle.program)
households = all_households.filter(self.targeting_criteria.get_query())
households = all_households.filter(self.targeting_criteria.get_query()).order_by("unicef_id")
return households.distinct()

@property
Expand Down Expand Up @@ -960,7 +960,16 @@ def build_status_pending(self) -> None:
field=build_status,
source=[BuildStatus.BUILD_STATUS_PENDING, BuildStatus.BUILD_STATUS_FAILED, BuildStatus.BUILD_STATUS_OK],
target=BuildStatus.BUILD_STATUS_BUILDING,
conditions=[lambda obj: obj.status in [PaymentPlan.Status.TP_OPEN, PaymentPlan.Status.TP_LOCKED]],
conditions=[
lambda obj: obj.status
in [
PaymentPlan.Status.TP_OPEN,
PaymentPlan.Status.TP_LOCKED,
PaymentPlan.Status.TP_STEFICON_WAIT,
PaymentPlan.Status.TP_STEFICON_COMPLETED,
PaymentPlan.Status.TP_STEFICON_ERROR,
]
],
)
def build_status_building(self) -> None:
self.built_at = timezone.now()
Expand All @@ -969,7 +978,16 @@ def build_status_building(self) -> None:
field=build_status,
source=BuildStatus.BUILD_STATUS_BUILDING,
target=BuildStatus.BUILD_STATUS_FAILED,
conditions=[lambda obj: obj.status in [PaymentPlan.Status.TP_OPEN, PaymentPlan.Status.TP_LOCKED]],
conditions=[
lambda obj: obj.status
in [
PaymentPlan.Status.TP_OPEN,
PaymentPlan.Status.TP_LOCKED,
PaymentPlan.Status.TP_STEFICON_WAIT,
PaymentPlan.Status.TP_STEFICON_COMPLETED,
PaymentPlan.Status.TP_STEFICON_ERROR,
]
],
)
def build_status_failed(self) -> None:
self.built_at = timezone.now()
Expand All @@ -978,7 +996,16 @@ def build_status_failed(self) -> None:
field=build_status,
source=BuildStatus.BUILD_STATUS_BUILDING,
target=BuildStatus.BUILD_STATUS_OK,
conditions=[lambda obj: obj.status in [PaymentPlan.Status.TP_OPEN, PaymentPlan.Status.TP_LOCKED]],
conditions=[
lambda obj: obj.status
in [
PaymentPlan.Status.TP_OPEN,
PaymentPlan.Status.TP_LOCKED,
PaymentPlan.Status.TP_STEFICON_COMPLETED,
PaymentPlan.Status.TP_STEFICON_ERROR,
PaymentPlan.Status.TP_STEFICON_WAIT,
]
],
)
def build_status_ok(self) -> None:
self.built_at = timezone.now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def draft(self) -> PaymentPlan:
return self.payment_plan

def open(self, input_data: Dict) -> PaymentPlan:
self.payment_plan.status_open()
dispersion_end_date = input_data["dispersion_end_date"]
if not dispersion_end_date or dispersion_end_date <= timezone.now().date():
raise GraphQLError(f"Dispersion End Date [{dispersion_end_date}] cannot be a past date")
Expand All @@ -198,7 +199,6 @@ def open(self, input_data: Dict) -> PaymentPlan:
self.payment_plan.dispersion_start_date = input_data["dispersion_start_date"]
self.payment_plan.dispersion_end_date = dispersion_end_date

self.payment_plan.status_open()
self.payment_plan.save(
update_fields=("status_date", "status", "currency", "dispersion_start_date", "dispersion_end_date")
)
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/apps/payment/test_celery_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def test_payment_plan_rebuild_stats(self, get_exchange_rate_mock: Mock) -> None:
program_cycle=self.program.cycles.first(),
created_by=self.user,
status=PaymentPlan.Status.TP_STEFICON_WAIT,
build_status=PaymentPlan.BuildStatus.BUILD_STATUS_PENDING,
)
PaymentFactory(parent=payment_plan)
pp_id_str = str(payment_plan.pk)
Expand All @@ -181,6 +182,7 @@ def test_payment_plan_rebuild_stats_exception_handling(
program_cycle=self.program.cycles.first(),
created_by=self.user,
status=PaymentPlan.Status.TP_STEFICON_WAIT,
build_status=PaymentPlan.BuildStatus.BUILD_STATUS_PENDING,
)
PaymentFactory(parent=payment_plan)
mock_update_population_count_fields.side_effect = Exception("Simulated exception just for test")
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/apps/payment/test_payment_plan_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
)
from hct_mis_api.apps.payment.models import (
DeliveryMechanism,
FinancialServiceProvider,
FinancialServiceProviderXlsxTemplate,
Payment,
PaymentPlan,
Expand Down Expand Up @@ -454,6 +455,7 @@ def test_receiving_reconciliations_from_fsp(self, mock_get_exchange_rate: Any) -
santander_fsp = FinancialServiceProviderFactory(
name="Santander",
distribution_limit=None,
communication_channel=FinancialServiceProvider.COMMUNICATION_CHANNEL_XLSX,
)
santander_fsp.delivery_mechanisms.set([dm_cash, dm_transfer])
FspXlsxTemplatePerDeliveryMechanismFactory(financial_service_provider=santander_fsp, delivery_mechanism=dm_cash)
Expand Down Expand Up @@ -702,9 +704,11 @@ def test_receiving_reconciliations_from_fsp(self, mock_get_exchange_rate: Any) -

self.assertEqual(sheet.cell(row=1, column=1).value, "payment_id")
assert payment_plan.payment_items.count() == 4
payment = payment_plan.payment_items.order_by("unicef_id").first()
self.assertEqual(sheet.cell(row=2, column=1).value, payment.unicef_id) # unintuitive
payment = payment_plan.eligible_payments.filter(household=household_1).first()
# check if there is the same HH
self.assertEqual(payment.household.unicef_id, household_1.unicef_id)

self.assertEqual(sheet.cell(row=2, column=1).value, payment.unicef_id) # unintuitive
self.assertEqual(sheet.cell(row=1, column=2).value, "household_id")
self.assertEqual(sheet.cell(row=2, column=2).value, household_1.unicef_id)
self.assertEqual(sheet.cell(row=1, column=3).value, "household_size")
Expand Down
31 changes: 19 additions & 12 deletions tests/unit/apps/payment/test_payment_plan_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils import timezone

from aniso8601 import parse_date
from django_fsm import TransitionNotAllowed
from flaky import flaky
from freezegun import freeze_time
from graphql import GraphQLError
Expand Down Expand Up @@ -220,8 +221,12 @@ def test_create_validation_errors(self) -> None:
dispersion_end_date=parse_date("2020-09-11"),
currency="USD",
)
with self.assertRaisesMessage(GraphQLError, "Can only move from Draft status to Open"):
with self.assertRaises(TransitionNotAllowed) as e:
PaymentPlanService(payment_plan=pp).open(input_data=open_input_data)
self.assertEqual(
str(e.exception),
"Can't switch from state 'TP_OPEN' using method 'status_open'",
)

pp.status = PaymentPlan.Status.DRAFT
pp.save()
Expand All @@ -230,6 +235,8 @@ def test_create_validation_errors(self) -> None:
):
PaymentPlanService(payment_plan=pp).open(input_data=open_input_data)
open_input_data["dispersion_end_date"] = parse_date("2020-11-11")
pp.refresh_from_db()
self.assertEqual(pp.status, PaymentPlan.Status.DRAFT)
pp = PaymentPlanService(payment_plan=pp).open(input_data=open_input_data)
pp.refresh_from_db()
self.assertEqual(pp.status, PaymentPlan.Status.OPEN)
Expand Down Expand Up @@ -368,7 +375,7 @@ def test_create_follow_up_pp(self, get_exchange_rate_mock: Any) -> None:
pp_not_distributed = payments[1]
pp_force_failed = payments[2]

with self.assertNumQueries(8):
with self.assertNumQueries(7):
follow_up_pp = PaymentPlanService(pp).create_follow_up(
self.user, dispersion_start_date, dispersion_end_date
)
Expand Down Expand Up @@ -418,7 +425,7 @@ def test_create_follow_up_pp(self, get_exchange_rate_mock: Any) -> None:
follow_up_payment.excluded = True
follow_up_payment.save()

with self.assertNumQueries(8):
with self.assertNumQueries(7):
follow_up_pp_2 = PaymentPlanService(pp).create_follow_up(
self.user, dispersion_start_date, dispersion_end_date
)
Expand Down Expand Up @@ -776,22 +783,22 @@ def test_tp_lock_invalid_pp_status(self) -> None:
payment_plan = PaymentPlanFactory(
program_cycle=self.cycle, created_by=self.user, status=PaymentPlan.Status.DRAFT
)
with self.assertRaises(GraphQLError) as e:
with self.assertRaises(TransitionNotAllowed) as e:
PaymentPlanService(payment_plan).tp_lock()
self.assertEqual(
e.exception.message,
"Can only Lock Population for Open Population status",
str(e.exception),
"Can't switch from state 'DRAFT' using method 'status_tp_lock'",
)

def test_tp_unlock(self) -> None:
payment_plan = PaymentPlanFactory(
program_cycle=self.cycle, created_by=self.user, status=PaymentPlan.Status.DRAFT
)
with self.assertRaises(GraphQLError) as e:
with self.assertRaises(TransitionNotAllowed) as e:
PaymentPlanService(payment_plan).tp_unlock()
self.assertEqual(
e.exception.message,
"Can only Unlock Population for Locked Population status",
str(e.exception),
"Can't switch from state 'DRAFT' using method 'status_tp_open'",
)
payment_plan.status = PaymentPlan.Status.TP_LOCKED
payment_plan.save()
Expand Down Expand Up @@ -828,11 +835,11 @@ def test_draft_with_invalid_pp_status(self) -> None:
created_by=self.user,
status=PaymentPlan.Status.DRAFT,
)
with self.assertRaises(GraphQLError) as e:
with self.assertRaises(TransitionNotAllowed) as e:
PaymentPlanService(payment_plan).draft()
self.assertEqual(
e.exception.message,
"Only Locked Population status can be moved to next step",
str(e.exception),
"Can't switch from state 'DRAFT' using method 'status_draft'",
)

def test_lock_if_no_valid_payments(self) -> None:
Expand Down

0 comments on commit 02cc948

Please sign in to comment.