-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: retry sending transaction to transaction service layer
- Removed the command functionality from the command layer - Added tests to the command logic
- Loading branch information
1 parent
5373e8d
commit 5172d5b
Showing
4 changed files
with
577 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 119 additions & 36 deletions
155
apps/billing/tests/test_command_retry_sage_transactions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,132 @@ | ||
from io import StringIO | ||
from unittest import mock | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.test import TestCase, override_settings | ||
|
||
from apps.billing.factories import SageX3TransactionInformationFactory, TransactionFactory, TransactionItemFactory | ||
from apps.billing.mocks import MockResponse | ||
from apps.billing.models import SageX3TransactionInformation | ||
from apps.billing.tests.test_utils import processor_success_response | ||
|
||
|
||
def create_transaction(status): | ||
transaction = TransactionFactory.build() | ||
transaction.save() | ||
item = TransactionItemFactory.build(transaction=transaction) | ||
item.save() | ||
sageX3TI = SageX3TransactionInformationFactory.build(transaction=transaction, status=status) | ||
sageX3TI.save() | ||
return transaction | ||
|
||
|
||
@mock.patch("apps.billing.services.transaction_service.TransactionService", autospec=True) | ||
class CommandRetrySageTransactionsTestCase(TestCase): | ||
""" | ||
Test the `retry_sage_transactions` Django command. | ||
""" | ||
|
||
def test_command_retry_sage_transactions(self, transaction_service_mock): | ||
""" | ||
Test the command `retry_sage_transactions`. | ||
Unfortunately all this calls needs to be on the same test. | ||
""" | ||
call_command("retry_sage_transactions") | ||
transaction_service_mock.assert_not_called() | ||
|
||
# test a pending transaction should be retried | ||
t = create_transaction(SageX3TransactionInformation.PENDING) | ||
for _ in range(5): | ||
create_transaction(SageX3TransactionInformation.SUCCESS) | ||
call_command("retry_sage_transactions") | ||
transaction_service_mock.assert_called_once_with(t) | ||
|
||
# test a failed transaction should be retried | ||
f = create_transaction(SageX3TransactionInformation.FAILED) | ||
call_command("retry_sage_transactions") | ||
transaction_service_mock.assert_called_with(f) | ||
|
||
# test the force retry of a success transaction | ||
t = create_transaction(SageX3TransactionInformation.SUCCESS) | ||
call_command("retry_sage_transactions", transaction_id=t.transaction_id) | ||
transaction_service_mock.assert_called_with(t) | ||
@override_settings(TRANSACTION_PROCESSOR_URL="http://fake-processor.com", DEFAULT_SERIES="AAA") | ||
@mock.patch("requests.post", side_effect=processor_success_response) | ||
def test_command_retry_sage_transactions_success_PENDIND_status(self, mocked_post): | ||
|
||
out = StringIO() | ||
transaction = TransactionFactory.create() | ||
TransactionItemFactory.create(transaction=transaction) | ||
|
||
SageX3TransactionInformationFactory.create( | ||
transaction=transaction, status=SageX3TransactionInformation.PENDING | ||
) | ||
|
||
call_command("retry_sage_transactions", stdout=out) | ||
|
||
message = "----- 1 Transactions were retried -----\n\nSUCCESSFULL RETRIES: 1 FAILED RETRIES: 0" | ||
|
||
self.assertTrue(message in out.getvalue()) | ||
|
||
@override_settings(TRANSACTION_PROCESSOR_URL="http://fake-processor.com", DEFAULT_SERIES="AAA") | ||
@mock.patch("requests.post", side_effect=processor_success_response) | ||
def test_command_retry_sage_transactions_success_FAILED_status(self, mocked_post): | ||
|
||
out = StringIO() | ||
transaction = TransactionFactory.create() | ||
TransactionItemFactory.create(transaction=transaction) | ||
|
||
SageX3TransactionInformationFactory.create(transaction=transaction, status=SageX3TransactionInformation.FAILED) | ||
|
||
call_command("retry_sage_transactions", stdout=out) | ||
|
||
message = "----- 1 Transactions were retried -----\n\nSUCCESSFULL RETRIES: 1 FAILED RETRIES: 0" | ||
|
||
self.assertTrue(message in out.getvalue()) | ||
|
||
@override_settings(TRANSACTION_PROCESSOR_URL="http://fake-processor.com", DEFAULT_SERIES="AAA") | ||
@mock.patch("requests.post", side_effect=processor_success_response) | ||
def test_command_retry_sage_transactions_success_without_transaction_id(self, mocked_post): | ||
|
||
out = StringIO() | ||
|
||
for transaction in TransactionFactory.create_batch(10): | ||
TransactionItemFactory.create(transaction=transaction) | ||
SageX3TransactionInformationFactory.create( | ||
transaction=transaction, status=SageX3TransactionInformation.FAILED | ||
) | ||
|
||
for transaction in TransactionFactory.create_batch(10): | ||
TransactionItemFactory.create(transaction=transaction) | ||
SageX3TransactionInformationFactory.create( | ||
transaction=transaction, status=SageX3TransactionInformation.PENDING | ||
) | ||
|
||
call_command("retry_sage_transactions", stdout=out) | ||
|
||
message = "----- 20 Transactions were retried -----\n\nSUCCESSFULL RETRIES: 20 FAILED RETRIES: 0" | ||
|
||
self.assertTrue(message in out.getvalue()) | ||
|
||
@override_settings(TRANSACTION_PROCESSOR_URL="http://fake-processor.com", DEFAULT_SERIES="AAA") | ||
@mock.patch("requests.post", return_value=MockResponse(status_code=500, data="Some not expected error")) | ||
def test_command_retry_sage_transactions_error_PENDIND_status(self, mocked_post): | ||
|
||
out = StringIO() | ||
transaction = TransactionFactory.create() | ||
TransactionItemFactory.create(transaction=transaction) | ||
|
||
SageX3TransactionInformationFactory.create( | ||
transaction=transaction, status=SageX3TransactionInformation.PENDING | ||
) | ||
|
||
call_command("retry_sage_transactions", stdout=out) | ||
|
||
message = "----- 1 Transactions were retried -----\n\nSUCCESSFULL RETRIES: 0 FAILED RETRIES: 1" | ||
|
||
self.assertTrue(message in out.getvalue()) | ||
|
||
@override_settings(TRANSACTION_PROCESSOR_URL="http://fake-processor.com", DEFAULT_SERIES="AAA") | ||
@mock.patch("requests.post", return_value=MockResponse(status_code=500, data="Some not expected error")) | ||
def test_command_retry_sage_transactions_error_FAILED_status(self, mocked_post): | ||
|
||
out = StringIO() | ||
transaction = TransactionFactory.create() | ||
TransactionItemFactory.create(transaction=transaction) | ||
|
||
SageX3TransactionInformationFactory.create(transaction=transaction, status=SageX3TransactionInformation.FAILED) | ||
|
||
call_command("retry_sage_transactions", stdout=out) | ||
|
||
message = "----- 1 Transactions were retried -----\n\nSUCCESSFULL RETRIES: 0 FAILED RETRIES: 1" | ||
|
||
self.assertTrue(message in out.getvalue()) | ||
|
||
@override_settings(TRANSACTION_PROCESSOR_URL="http://fake-processor.com", DEFAULT_SERIES="AAA") | ||
@mock.patch("requests.post", return_value=MockResponse(status_code=500, data="Some not expected error")) | ||
def test_command_retry_sage_transactions_error_without_transaction_id(self, mocked_post): | ||
|
||
out = StringIO() | ||
|
||
for transaction in TransactionFactory.create_batch(10): | ||
TransactionItemFactory.create(transaction=transaction) | ||
SageX3TransactionInformationFactory.create( | ||
transaction=transaction, status=SageX3TransactionInformation.FAILED | ||
) | ||
|
||
for transaction in TransactionFactory.create_batch(10): | ||
TransactionItemFactory.create(transaction=transaction) | ||
SageX3TransactionInformationFactory.create( | ||
transaction=transaction, status=SageX3TransactionInformation.PENDING | ||
) | ||
|
||
call_command("retry_sage_transactions", stdout=out) | ||
|
||
message = "----- 20 Transactions were retried -----\n\nSUCCESSFULL RETRIES: 0 FAILED RETRIES: 20" | ||
|
||
self.assertTrue(message in out.getvalue()) |
Oops, something went wrong.