def test_delete_when_applied(self):
        _, prolongation = self.fixture_created()
        prolongation.status = 'applied'

        managed_prolongation = ProlongationManager(prolongation)
        with self.assertRaises(Exception) as _:  # noqa: F841
            managed_prolongation.delete()
    def test_apply_without_documents(self):
        _, prolongation = self.fixture_created()

        prolongation.documents = []
        managed_prolongation = ProlongationManager(prolongation)
        with self.assertRaises(ValidationError) as _:
            managed_prolongation.apply()
    def test_apply_when_need_to_apply_short(self):
        _, prolongation = self.fixture_created()

        managed_prolongation = ProlongationManager(prolongation)
        managed_prolongation.apply()

        self.assertEqual(prolongation.status, 'applied')
    def test_apply_long(self):
        contract, prolongation = self.fixture_created()
        pre_prolongation_contract_signingPeriod = contract.signingPeriod

        target_signingPeriod_endDate = calculate_business_date(
            contract.signingPeriod.startDate,
            PROLONGATION_LONG_PERIOD,
            contract.__parent__,
            working_days=True,
            specific_hour=CONTRACT_SIGNING_PERIOD_END_DATE_HOUR)

        previous_short_prolongation = self.fixture_created()[0]
        previous_short_prolongation.status = 'applied'
        contract.prolongations.append(previous_short_prolongation)

        managed_prolongation = ProlongationManager(prolongation)
        managed_prolongation.apply()
        self.assertEqual(previous_short_prolongation.status, 'applied')

        self.assertEqual(prolongation.status, 'applied')

        # check update of startDate
        self.assertEqual(contract.signingPeriod.startDate,
                         pre_prolongation_contract_signingPeriod.startDate,
                         'startDate of Contract is incorrect')
        # check update of endDate
        self.assertEqual(
            contract.signingPeriod.endDate,
            target_signingPeriod_endDate,
            'endDate of Contract is incorrect',
        )
    def patch(self):
        """Update of prolongation
            
            Fields, except on `status`, can be updated only when
            Prolongation has status `draft`.
        """
        new_status = self.request.validated['data'].get('status')

        if new_status == 'applied':
            # this method checks intention of long apply
            managed_prolongation = ProlongationManager(self.request.context)
            try:
                managed_prolongation.apply()
            except Exception as e:
                self.request.errors.add('body', 'data', e.message)
                self.request.errors.status = 403
                return
        if apply_patch(self.request):
            self.LOGGER.info(
                'Updated prolongation {}'.format(
                    self.request.context.id
                ),
                extra=context_unpack(
                    self.request,
                    {'MESSAGE_ID': 'contract_prolongation_patch'}
                    )
                )
            return {'data': self.request.context.serialize()}
    def test_apply_when_need_to_apply_long(self):
        contract, prolongation = self.fixture_created()
        previous_applied_prolongation = self.fixture_created()[0]
        contract.prolongations.append(previous_applied_prolongation)
        previous_applied_prolongation.status = 'applied'

        self.assertEqual(prolongation.status, 'draft')

        managed_prolongation = ProlongationManager(prolongation)
        managed_prolongation.apply()

        self.assertEqual(prolongation.status, 'applied')
        self.assertEqual(previous_applied_prolongation.status, 'applied')