Esempio n. 1
0
    def test_validate_credit(self):
        credit_trade_status, created = CreditTradeStatus.objects.get_or_create(
            status='Approved')

        credit_trade_type, created = CreditTradeType.objects.get_or_create(
            the_type='Sell')

        credit_trade_zero_reason, created = CreditTradeZeroReason.objects \
            .get_or_create(reason='Other', display_order=2)

        CreditTrade.objects.create(
            status=credit_trade_status,
            initiator=self.user_2.organization,
            respondent=self.user_3.organization,
            type=credit_trade_type,
            number_of_credits=1000000000,
            fair_market_value_per_credit=0,
            zero_reason=credit_trade_zero_reason,
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        credit_trades = CreditTrade.objects.filter(
            status_id=credit_trade_status.id)

        with self.assertRaises(PositiveIntegerException):
            CreditTradeService.validate_credits(credit_trades)
Esempio n. 2
0
    def test_validate_credit_success(self):
        """
        As a government user, I should be able to validate approved credit
        transfers:
        It should raise an exception if it sees any fuel suppliers with
        insufficient funds
        This test is similar to the one above, but should succeed as we're
        going to allocate the right amount of credits this time
        """
        credit_trades = []

        # Award Test 1 with 1000 credits (new organizations start
        # with 0 credits)
        # (Please note in most cases we should use a different type
        # but to reduce the number of things to keep track, lets just
        # transfer from organization: 1 (BC Government))
        credit_trades.append(
            CreditTrade.objects.create(
                status=self.statuses['recorded'],
                initiator=self.users['gov_analyst'].organization,
                respondent=self.organizations['from'],
                type=self.credit_trade_types['sell'],
                number_of_credits=1000,
                fair_market_value_per_credit=0,
                zero_reason=self.zero_reason['other'],
                trade_effective_date=datetime.datetime.today().strftime(
                    '%Y-%m-%d')))

        # Transfer 500 from Test 1 to Test 2
        credit_trades.append(
            CreditTrade.objects.create(
                status=self.statuses['recorded'],
                initiator=self.organizations['from'],
                respondent=self.organizations['to'],
                type=self.credit_trade_types['sell'],
                number_of_credits=500,
                fair_market_value_per_credit=0,
                zero_reason=self.zero_reason['other'],
                trade_effective_date=datetime.datetime.today().strftime(
                    '%Y-%m-%d')))

        # Transfer 300 from Test 1 to Test 2
        credit_trades.append(
            CreditTrade.objects.create(
                status=self.statuses['recorded'],
                initiator=self.organizations['from'],
                respondent=self.organizations['to'],
                type=self.credit_trade_types['sell'],
                number_of_credits=300,
                fair_market_value_per_credit=0,
                zero_reason=self.zero_reason['other'],
                trade_effective_date=datetime.datetime.today().strftime(
                    '%Y-%m-%d')))

        # no exceptions should be raised
        CreditTradeService.validate_credits(credit_trades)
Esempio n. 3
0
    def batch_process(self, request):
        status_approved = CreditTradeStatus.objects \
                                           .get(status="Approved")

        credit_trades = CreditTrade.objects.filter(
            status_id=status_approved.id).order_by('id')

        CreditTradeService.validate_credits(credit_trades)

        for credit_trade in credit_trades:
            credit_trade.update_user_id = request.user.id
            CreditTradeService.approve(credit_trade)

        return Response(
            {"message": "Approved Credit Transactions have been processed."},
            status=status.HTTP_200_OK)
Esempio n. 4
0
    def test_validate_credit_complex(self):
        """
        As a government user, I should be able to validate recorded credit
        transfers:
        It should raise an exception if it sees any fuel suppliers with
        insufficient funds
        This is a slightly more complex test where we have multi credit
        trades with new organizations that bounces the number of credits
        up and down
        """

        initial_balance = OrganizationBalance.objects.get(
            organization_id=self.organizations['from'].id,
            expiration_date=None).validated_credits

        # Transfer initial balance from Test 1 to Test 2
        CreditTrade.objects.create(
            status=self.statuses['recorded'],
            initiator=self.organizations['from'],
            respondent=self.organizations['to'],
            type=self.credit_trade_types['sell'],
            number_of_credits=initial_balance,
            fair_market_value_per_credit=0,
            zero_reason=self.zero_reason['other'],
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        # Transfer 1 from Test 1 to Test 2
        CreditTrade.objects.create(
            status=self.statuses['recorded'],
            initiator=self.organizations['from'],
            respondent=self.organizations['to'],
            type=self.credit_trade_types['sell'],
            number_of_credits=1,
            fair_market_value_per_credit=0,
            zero_reason=self.zero_reason['other'],
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        credit_trades = CreditTrade.objects.filter(
            status_id=self.statuses['recorded'].id)

        # this should now raise an exception since we tried transferring
        # 1200 credits when only 1000 are available
        with self.assertRaises(PositiveIntegerException):
            CreditTradeService.validate_credits(credit_trades)
Esempio n. 5
0
    def batch_process(self, request):
        """
        Call the approve function on multiple Credit Trades
        """
        status_approved = CreditTradeStatus.objects \
                                           .get(status="Recorded")

        credit_trades = CreditTrade.objects.filter(
            status_id=status_approved.id).order_by('id')

        CreditTradeService.validate_credits(credit_trades)

        for credit_trade in credit_trades:
            credit_trade.update_user_id = request.user.id
            CreditTradeService.approve(credit_trade)
            CreditTradeService.dispatch_notifications(None, credit_trade)

        return Response(
            {"message": "Approved credit transactions have been processed."},
            status=status.HTTP_200_OK)
Esempio n. 6
0
    def test_validate_credit(self):
        """
        As a government user, I should be able to validate recorded credit
        transfers:
        It should raise an exception if it sees any fuel suppliers with
        insufficient funds
        """
        CreditTrade.objects.create(
            status=self.statuses['recorded'],
            initiator=self.users['fs_user_2'].organization,
            respondent=self.users['fs_user_3'].organization,
            type=self.credit_trade_types['sell'],
            number_of_credits=1000000000,
            fair_market_value_per_credit=0,
            zero_reason=self.zero_reason['other'],
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        credit_trades = CreditTrade.objects.filter(
            status_id=self.statuses['recorded'].id)

        with self.assertRaises(PositiveIntegerException):
            CreditTradeService.validate_credits(credit_trades)
Esempio n. 7
0
    def test_validate_credit_success(self):

        credit_trades = []

        credit_trade_status, created = CreditTradeStatus.objects.get_or_create(
            status='Approved')

        credit_trade_type, created = CreditTradeType.objects.get_or_create(
            the_type='Sell')

        credit_trade_zero_reason, created = CreditTradeZeroReason.objects \
            .get_or_create(reason='Other', display_order=2)

        from_organization = Organization.objects.create(name="Test 1",
                                                        actions_type_id=1,
                                                        status_id=1)
        to_organization = Organization.objects.create(name="Test 2",
                                                      actions_type_id=1,
                                                      status_id=1)

        # Award Test 1 with 1000 credits (new organizations start
        # with 0 credits)
        # (Please note in most cases we should use a different type
        # but to reduce the number of things to keep track, lets just
        # transfer from organization: 1 (BC Government))
        credit_trades.append(
            CreditTrade.objects.create(
                status=credit_trade_status,
                initiator=self.gov_user.organization,
                respondent=from_organization,
                type=credit_trade_type,
                number_of_credits=1000,
                fair_market_value_per_credit=0,
                zero_reason=credit_trade_zero_reason,
                trade_effective_date=datetime.datetime.today().strftime(
                    '%Y-%m-%d')))

        # Transfer 500 from Test 1 to Test 2
        credit_trades.append(
            CreditTrade.objects.create(
                status=credit_trade_status,
                initiator=from_organization,
                respondent=to_organization,
                type=credit_trade_type,
                number_of_credits=500,
                fair_market_value_per_credit=0,
                zero_reason=credit_trade_zero_reason,
                trade_effective_date=datetime.datetime.today().strftime(
                    '%Y-%m-%d')))

        # Transfer 300 from Test 1 to Test 2
        credit_trades.append(
            CreditTrade.objects.create(
                status=credit_trade_status,
                initiator=from_organization,
                respondent=to_organization,
                type=credit_trade_type,
                number_of_credits=300,
                fair_market_value_per_credit=0,
                zero_reason=credit_trade_zero_reason,
                trade_effective_date=datetime.datetime.today().strftime(
                    '%Y-%m-%d')))

        # no exceptions should be raised
        CreditTradeService.validate_credits(credit_trades)
Esempio n. 8
0
    def test_validate_credit_complex(self):
        credit_trade_status, created = CreditTradeStatus.objects.get_or_create(
            status='Approved')

        credit_trade_type, created = CreditTradeType.objects.get_or_create(
            the_type='Sell')

        credit_trade_zero_reason, created = CreditTradeZeroReason.objects \
            .get_or_create(reason='Other', display_order=2)

        from_organization = Organization.objects.create(name="Test 1",
                                                        actions_type_id=1,
                                                        status_id=1)
        to_organization = Organization.objects.create(name="Test 2",
                                                      actions_type_id=1,
                                                      status_id=1)

        # Award Test 1 with 1000 credits (new organizations start
        # with 0 credits)
        # (Please note in most cases we should use a different type
        # but to reduce the number of things to keep track, lets just
        # transfer from organization: 1 (BC Government))
        CreditTrade.objects.create(
            status=credit_trade_status,
            initiator=self.gov_user.organization,
            respondent=from_organization,
            type=credit_trade_type,
            number_of_credits=1000,
            fair_market_value_per_credit=0,
            zero_reason=credit_trade_zero_reason,
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        # Transfer 500 from Test 1 to Test 2
        CreditTrade.objects.create(
            status=credit_trade_status,
            initiator=from_organization,
            respondent=to_organization,
            type=credit_trade_type,
            number_of_credits=500,
            fair_market_value_per_credit=0,
            zero_reason=credit_trade_zero_reason,
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        # Transfer 700 from Test 1 to Test 2
        CreditTrade.objects.create(
            status=credit_trade_status,
            initiator=from_organization,
            respondent=to_organization,
            type=credit_trade_type,
            number_of_credits=700,
            fair_market_value_per_credit=0,
            zero_reason=credit_trade_zero_reason,
            trade_effective_date=datetime.datetime.today().strftime(
                '%Y-%m-%d'))

        credit_trades = CreditTrade.objects.filter(
            status_id=credit_trade_status.id)

        # this should now raise an exception since we tried transferring
        # 1200 credits when only 1000 are available
        with self.assertRaises(PositiveIntegerException):
            CreditTradeService.validate_credits(credit_trades)