Skip to content

Commit

Permalink
upd tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-mk committed Jan 24, 2025
1 parent e0ecd96 commit 78a6d70
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/hct_mis_api/apps/payment/migrations/0013_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='paymentplan',
name='targeting_criteria',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='payment_plan',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='payment_plan',
to='targeting.targetingcriteria'),
),
]
21 changes: 9 additions & 12 deletions src/hct_mis_api/apps/payment/models/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class Action(models.TextChoices):
built_at = models.DateTimeField(null=True, blank=True)
targeting_criteria = models.OneToOneField(
"targeting.TargetingCriteria",
on_delete=models.CASCADE,
on_delete=models.PROTECT,
related_name="payment_plan",
)
currency = models.CharField(max_length=4, choices=CURRENCY_CHOICES, blank=True, null=True)
Expand Down Expand Up @@ -750,17 +750,14 @@ def has_empty_criteria(self) -> bool:

@property
def has_empty_ids_criteria(self) -> bool:
if self.targeting_criteria is None:
return True
else:
has_hh_ids, has_ind_ids = False, False
for rule in self.targeting_criteria.rules.all():
if rule.household_ids:
has_hh_ids = True
if rule.individual_ids:
has_ind_ids = True

return not has_hh_ids and not has_ind_ids
has_hh_ids, has_ind_ids = False, False
for rule in self.targeting_criteria.rules.all():
if rule.household_ids:
has_hh_ids = True
if rule.individual_ids:
has_ind_ids = True

return not has_hh_ids and not has_ind_ids

@property
def excluded_beneficiaries_ids(self) -> List[str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ def update(self, input_data: Dict) -> PaymentPlan:
should_update_money_stats = False
should_rebuild_list = False
vulnerability_filter = False
old_targeting_criteria = None

name = input_data.get("name")
vulnerability_score_min = input_data.get("vulnerability_score_min")
Expand Down Expand Up @@ -541,7 +542,7 @@ def update(self, input_data: Dict) -> PaymentPlan:
targeting_criteria = from_input_to_targeting_criteria(targeting_criteria_input, program)
if self.payment_plan.status == PaymentPlan.Status.TP_OPEN:
if self.payment_plan.targeting_criteria:
self.payment_plan.targeting_criteria.delete()
old_targeting_criteria = self.payment_plan.targeting_criteria
self.payment_plan.targeting_criteria = targeting_criteria
if excluded_ids is not None:
should_rebuild_list = True
Expand All @@ -565,6 +566,9 @@ def update(self, input_data: Dict) -> PaymentPlan:
Payment.objects.filter(parent=self.payment_plan).update(currency=self.payment_plan.currency)

self.payment_plan.save()
# remove old targeting_criteria
if old_targeting_criteria:
old_targeting_criteria.delete()

# prevent race between commit transaction and using in task
transaction.on_commit(
Expand Down
11 changes: 3 additions & 8 deletions tests/unit/apps/payment/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ def test_payment_plan_exclude_hh_property(self) -> None:
self.assertEqual(pp.excluded_household_ids_targeting_level, [hh.unicef_id])

def test_payment_plan_has_empty_criteria_property(self) -> None:
pp: PaymentPlan = PaymentPlanFactory(targeting_criteria=None, created_by=self.user)

pp: PaymentPlan = PaymentPlanFactory(created_by=self.user)
self.assertTrue(pp.has_empty_criteria)

def test_payment_plan_has_empty_ids_criteria_property(self) -> None:
Expand Down Expand Up @@ -386,16 +385,12 @@ def test_remove_imported_file(self) -> None:
self.assertIsNone(pp.imported_file_date)

def test_has_empty_ids_criteria(self) -> None:
pp = PaymentPlanFactory(created_by=self.user, targeting_criteria=None)
self.assertTrue(pp.has_empty_ids_criteria)
targeting_criteria = TargetingCriteriaFactory()
pp = PaymentPlanFactory(created_by=self.user)
TargetingCriteriaRuleFactory(
targeting_criteria=targeting_criteria,
targeting_criteria=pp.targeting_criteria,
household_ids="HH-1, HH-2",
individual_ids="IND-01, IND-02",
)
pp.targeting_criteria = targeting_criteria
pp.save()
self.assertFalse(pp.has_empty_ids_criteria)


Expand Down

0 comments on commit 78a6d70

Please sign in to comment.