|
11 | 11 | from django.utils import timezone
|
12 | 12 |
|
13 | 13 | from unicef_attachments.models import Attachment
|
| 14 | +from unicef_locations.tests.factories import LocationFactory |
14 | 15 |
|
15 | 16 | import etools.applications.partners.tasks
|
16 | 17 | from etools.applications.attachments.tests.factories import AttachmentFactory, AttachmentFileTypeFactory
|
17 | 18 | from etools.applications.core.tests.cases import BaseTenantTestCase
|
18 | 19 | from etools.applications.funds.tests.factories import FundsReservationHeaderFactory
|
19 |
| -from etools.applications.partners.models import Agreement, Intervention |
| 20 | +from etools.applications.partners.models import Agreement, Intervention, InterventionBudget |
| 21 | +from etools.applications.partners.tasks import _make_intervention_status_automatic_transitions |
20 | 22 | from etools.applications.partners.tests.factories import (
|
21 | 23 | AgreementFactory,
|
22 | 24 | CoreValuesAssessmentFactory,
|
23 | 25 | InterventionFactory,
|
| 26 | + InterventionResultLinkFactory, |
24 | 27 | PartnerFactory,
|
25 | 28 | )
|
26 |
| -from etools.applications.reports.tests.factories import CountryProgrammeFactory |
| 29 | +from etools.applications.reports.models import ResultType |
| 30 | +from etools.applications.reports.tests.factories import ( |
| 31 | + CountryProgrammeFactory, |
| 32 | + LowerResultFactory, |
| 33 | + OfficeFactory, |
| 34 | + ReportingRequirementFactory, |
| 35 | + SectionFactory, |
| 36 | +) |
27 | 37 | from etools.applications.users.tests.factories import CountryFactory, UserFactory
|
28 | 38 |
|
29 | 39 |
|
@@ -549,23 +559,11 @@ def test_make_intervention_status_automatic_transitions_with_valid_interventions
|
549 | 559 | interventions.append(intervention)
|
550 | 560 |
|
551 | 561 | # Create a few items that should be ignored. If they're not ignored, this test will fail.
|
552 |
| - # Ignored because of end date |
553 |
| - InterventionFactory(status=Intervention.ACTIVE, end=datetime.date.today() + datetime.timedelta(days=2)) |
554 | 562 | # Ignored because of status
|
555 |
| - InterventionFactory(status=Intervention.IMPLEMENTED, end=end_date) |
556 |
| - # Ignored because funds total outstanding != 0 |
557 |
| - intervention = InterventionFactory(status=Intervention.ENDED, end=end_date) |
558 |
| - for i in range(3): |
559 |
| - FundsReservationHeaderFactory(intervention=intervention, outstanding_amt=Decimal(i), |
560 |
| - intervention_amt=_make_decimal(i), |
561 |
| - actual_amt=_make_decimal(i), total_amt=_make_decimal(i)) |
562 |
| - |
563 |
| - # Ignored because funds totals don't match |
564 |
| - intervention = InterventionFactory(status=Intervention.ENDED, end=end_date) |
565 |
| - for i in range(3): |
566 |
| - FundsReservationHeaderFactory(intervention=intervention, outstanding_amt=Decimal(0.00), |
567 |
| - intervention_amt=_make_decimal(i), |
568 |
| - actual_amt=_make_decimal(i + 1), total_amt=_make_decimal(i)) |
| 563 | + InterventionFactory(status=Intervention.TERMINATED) |
| 564 | + InterventionFactory(status=Intervention.CLOSED) |
| 565 | + InterventionFactory(status=Intervention.SUSPENDED) |
| 566 | + InterventionFactory(status=Intervention.DRAFT) |
569 | 567 |
|
570 | 568 | # Mock InterventionValid() to always return True.
|
571 | 569 | mock_validator = mock.Mock(spec=['is_valid'])
|
@@ -619,22 +617,11 @@ def test_make_intervention_status_automatic_transitions_with_mixed_interventions
|
619 | 617 | interventions.append(intervention)
|
620 | 618 |
|
621 | 619 | # Create a few items that should be ignored. If they're not ignored, this test will fail.
|
622 |
| - # Ignored because of end date |
623 |
| - InterventionFactory(status=Intervention.ACTIVE, end=datetime.date.today() + datetime.timedelta(days=2)) |
624 | 620 | # Ignored because of status
|
625 |
| - InterventionFactory(status=Intervention.IMPLEMENTED, end=end_date) |
626 |
| - # Ignored because funds total outstanding != 0 |
627 |
| - intervention = InterventionFactory(status=Intervention.ENDED, end=end_date) |
628 |
| - for i in range(3): |
629 |
| - FundsReservationHeaderFactory(intervention=intervention, outstanding_amt=Decimal(i), |
630 |
| - intervention_amt=_make_decimal(i), |
631 |
| - actual_amt=_make_decimal(i), total_amt=_make_decimal(i)) |
632 |
| - # Ignored because funds totals don't match |
633 |
| - intervention = InterventionFactory(status=Intervention.ENDED, end=end_date) |
634 |
| - for i in range(3): |
635 |
| - FundsReservationHeaderFactory(intervention=intervention, outstanding_amt=Decimal(0.00), |
636 |
| - intervention_amt=_make_decimal(i), |
637 |
| - actual_amt=_make_decimal(i + 1), total_amt=_make_decimal(i)) |
| 621 | + InterventionFactory(status=Intervention.TERMINATED) |
| 622 | + InterventionFactory(status=Intervention.CLOSED) |
| 623 | + InterventionFactory(status=Intervention.SUSPENDED) |
| 624 | + InterventionFactory(status=Intervention.DRAFT) |
638 | 625 |
|
639 | 626 | def mock_intervention_valid_class_side_effect(*args, **kwargs):
|
640 | 627 | """Side effect for my mock InterventionValid() that gets called each time my mock InterventionValid() class
|
@@ -680,6 +667,69 @@ def mock_intervention_valid_class_side_effect(*args, **kwargs):
|
680 | 667 | ]
|
681 | 668 | self._assertCalls(mock_logger.error, expected_call_args)
|
682 | 669 |
|
| 670 | + def test_activate_intervention_with_task(self, _mock_db_connection, _mock_logger): |
| 671 | + today = datetime.date.today() |
| 672 | + unicef_staff = UserFactory(is_staff=True, groups__data=['UNICEF User']) |
| 673 | + |
| 674 | + partner = PartnerFactory(name='Partner 2') |
| 675 | + active_agreement = AgreementFactory( |
| 676 | + partner=partner, |
| 677 | + status=Agreement.SIGNED, |
| 678 | + signed_by_unicef_date=today - datetime.timedelta(days=2), |
| 679 | + signed_by_partner_date=today - datetime.timedelta(days=2), |
| 680 | + start=today - datetime.timedelta(days=2), |
| 681 | + ) |
| 682 | + |
| 683 | + active_intervention = InterventionFactory( |
| 684 | + agreement=active_agreement, |
| 685 | + title='Active Intervention', |
| 686 | + document_type=Intervention.PD, |
| 687 | + start=today - datetime.timedelta(days=1), |
| 688 | + end=today + datetime.timedelta(days=365), |
| 689 | + status=Intervention.SIGNED, |
| 690 | + country_programme=active_agreement.country_programme, |
| 691 | + # budget_owner=unicef_staff, |
| 692 | + # date_sent_to_partner=today - datetime.timedelta(days=1), |
| 693 | + signed_by_unicef_date=today - datetime.timedelta(days=1), |
| 694 | + signed_by_partner_date=today - datetime.timedelta(days=1), |
| 695 | + unicef_signatory=unicef_staff, |
| 696 | + partner_authorized_officer_signatory=partner.staff_members.all().first(), |
| 697 | + # cash_transfer_modalities=[Intervention.CASH_TRANSFER_DIRECT], |
| 698 | + ) |
| 699 | + InterventionBudget.objects.get_or_create( |
| 700 | + intervention=active_intervention, |
| 701 | + defaults={ |
| 702 | + 'unicef_cash': 100, |
| 703 | + 'unicef_cash_local': 10, |
| 704 | + 'partner_contribution': 200, |
| 705 | + 'partner_contribution_local': 20, |
| 706 | + 'in_kind_amount_local': 10, |
| 707 | + } |
| 708 | + ) |
| 709 | + active_intervention.flat_locations.add(LocationFactory()) |
| 710 | + active_intervention.partner_focal_points.add(partner.staff_members.all().first()) |
| 711 | + active_intervention.unicef_focal_points.add(unicef_staff) |
| 712 | + active_intervention.offices.add(OfficeFactory()) |
| 713 | + active_intervention.sections.add(SectionFactory()) |
| 714 | + ReportingRequirementFactory(intervention=active_intervention) |
| 715 | + AttachmentFactory( |
| 716 | + code='partners_intervention_signed_pd', |
| 717 | + content_object=active_intervention, |
| 718 | + ) |
| 719 | + FundsReservationHeaderFactory(intervention=active_intervention) |
| 720 | + |
| 721 | + result_link = InterventionResultLinkFactory( |
| 722 | + intervention=active_intervention, |
| 723 | + cp_output__result_type__name=ResultType.OUTPUT, |
| 724 | + ) |
| 725 | + LowerResultFactory(result_link=result_link) |
| 726 | + # activity = InterventionActivityFactory(result=pd_output) # epd related stuff |
| 727 | + # activity.time_frames.add(active_intervention.quarters.first()) |
| 728 | + |
| 729 | + _make_intervention_status_automatic_transitions(self.country_name) |
| 730 | + active_intervention.refresh_from_db() |
| 731 | + self.assertEqual(active_intervention.status, Intervention.ACTIVE) |
| 732 | + |
683 | 733 |
|
684 | 734 | @mock.patch('etools.applications.partners.tasks.logger', spec=['info'])
|
685 | 735 | @mock.patch('etools.applications.partners.tasks.connection', spec=['set_tenant'])
|
|
0 commit comments