def test_provider_match_then_delete(self):
        """
        Does match between provider_payment and provider_invoice then delete
        """
        test_account = Account.objects.create(name='Test Account',
                                              currency=CURRENCY_CUC,
                                              balance=1000)

        test_provider = Provider.objects.create(name='Test Provider')

        test_date = timezone.now()
        test_amount = 100
        test_status = STATUS_READY

        provider_payment = ProviderPayment(date=test_date,
                                           account=test_account,
                                           amount=test_amount,
                                           provider=test_provider,
                                           status=test_status)

        provider_payment = FinanceService.save_provider_payment(
            user=self.test_user, provider_payment=provider_payment)

        provider_invoice = ProviderInvoice(date=test_date,
                                           amount=test_amount,
                                           provider=test_provider,
                                           status=test_status)

        provider_invoice = FinanceService.save_provider_invoice(
            user=self.test_user, provider_invoice=provider_invoice)

        provider_match = ProviderDocumentMatch(
            credit_document=provider_payment,
            debit_document=provider_invoice,
            matched_amount=test_amount)

        provider_match = FinanceService.save_provider_match(provider_match)

        # entity matched incremented
        self.assertProviderCurrencyMatchedAmount(provider=test_provider,
                                                 currency=CURRENCY_CUC,
                                                 amount=test_amount)

        provider_payment.refresh_from_db()
        provider_invoice.refresh_from_db()

        self.assertEqual(provider_payment.matched_amount, test_amount)
        self.assertEqual(provider_invoice.matched_amount, test_amount)

        # delete
        FinanceService.delete_provider_match(provider_match.pk)

        provider_payment.refresh_from_db()
        provider_invoice.refresh_from_db()

        self.assertEqual(provider_payment.matched_amount, 0)
        self.assertEqual(provider_invoice.matched_amount, 0)
    def test_provider_match_then_change_provider(self):
        """
        Does match between provider_payment and provider_invoice then change provider
        """
        test_account = Account.objects.create(name='Test Account',
                                              currency=CURRENCY_CUC,
                                              balance=1000)

        test_provider1 = Provider.objects.create(name='Test Provider1')

        test_date = timezone.now()
        test_amount = 100
        test_status = STATUS_READY

        provider_payment = ProviderPayment(date=test_date,
                                           account=test_account,
                                           amount=test_amount,
                                           provider=test_provider1,
                                           status=test_status)

        provider_payment = FinanceService.save_provider_payment(
            user=self.test_user, provider_payment=provider_payment)

        provider_invoice = ProviderInvoice(date=test_date,
                                           amount=test_amount,
                                           provider=test_provider1,
                                           status=test_status)

        provider_invoice = FinanceService.save_provider_invoice(
            user=self.test_user, provider_invoice=provider_invoice)

        provider_match = ProviderDocumentMatch(
            credit_document=provider_payment,
            debit_document=provider_invoice,
            matched_amount=test_amount)

        provider_match = FinanceService.save_provider_match(provider_match)

        provider_payment.refresh_from_db()
        provider_invoice.refresh_from_db()

        self.assertEqual(provider_payment.matched_amount, test_amount)
        self.assertEqual(provider_invoice.matched_amount, test_amount)

        test_provider2 = Provider.objects.create(name='Test Provider2')

        provider_invoice.provider = test_provider2

        with self.assertRaisesMessage(ValidationError, ERROR_MATCH_PROVIDER):
            provider_invoice = FinanceService.save_provider_invoice(
                user=self.test_user, provider_invoice=provider_invoice)