Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyujacky committed Aug 9, 2024
1 parent a0aebb6 commit 8827781
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
9 changes: 7 additions & 2 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@

from commerce_coordinator.apps.commercetools.catalog_info.constants import DEFAULT_ORDER_EXPANSION, EdXFieldNames
from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes
from commerce_coordinator.apps.commercetools.utils import translate_stripe_refund_status_to_transaction_status, handle_commercetools_error
from commerce_coordinator.apps.commercetools.utils import (
handle_commercetools_error,
translate_stripe_refund_status_to_transaction_status
)
from commerce_coordinator.apps.core.constants import ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -441,7 +444,9 @@ def create_return_payment_transaction(

return returned_payment
except CommercetoolsError as err:
handle_commercetools_error(err, f"Unable to create refund payment transaction for payment {payment_id}")
context = f"Unable to create refund payment transaction for "\
f"payment {payment_id} and stripe refund {stripe_refund.id}"
handle_commercetools_error(err, context)
raise err

def update_line_item_transition_state_on_fulfillment(self, order_id: str, order_version: int,
Expand Down
29 changes: 15 additions & 14 deletions commerce_coordinator/apps/commercetools/tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ def test_create_return_for_order_exception(self):
"errors": [
{
"code": "ConcurrentModification",
"detailedErrorMessage": "Object [mock_order_id] has a "
"different version than expected. Expected: 2 - Actual: 1."
"message": "Object [mock_order_id] has a "
"different version than expected. Expected: 2 - Actual: 1.",
},
],
"response": {},
Expand All @@ -464,8 +464,8 @@ def test_create_return_for_order_exception(self):

expected_message = (
f"[CommercetoolsError] Unable to create return for "
f"order mock_order_id with error correlation id {exception.correlation_id} "
f"and error/s: {exception.errors}"
f"order mock_order_id "
f"- Correlation ID: {exception.correlation_id}, Details: {exception.errors}"
)

log_mock.assert_called_once_with(expected_message)
Expand Down Expand Up @@ -509,8 +509,8 @@ def test_update_return_payment_state_exception(self):
"errors": [
{
"code": "ConcurrentModification",
"detailedErrorMessage": "Object [mock_order_id] has a "
"different version than expected. Expected: 3 - Actual: 2."
"message": "Object [mock_order_id] has a "
"different version than expected. Expected: 3 - Actual: 2."
},
],
"response": {},
Expand Down Expand Up @@ -580,8 +580,8 @@ def test_create_refund_transaction_exception(self):
"errors": [
{
"code": "ConcurrentModification",
"detailedErrorMessage": "Object [mock_order_id] has a "
"different version than expected. Expected: 2 - Actual: 1."
"message": "Object [mock_order_id] has a "
"different version than expected. Expected: 2 - Actual: 1.",
},
],
"response": {},
Expand All @@ -607,8 +607,8 @@ def test_create_refund_transaction_exception(self):

expected_message = (
f"[CommercetoolsError] Unable to create refund payment transaction for "
f"payment mock_payment_id and stripe refund {mock_stripe_refund.id} with "
f"error correlation id {exception.correlation_id} and error/s: {exception.errors}"
f"payment mock_payment_id and stripe refund {mock_stripe_refund.id} "
f"- Correlation ID: {exception.correlation_id}, Details: {exception.errors}"
)

log_mock.assert_called_once_with(expected_message)
Expand Down Expand Up @@ -659,8 +659,8 @@ def test_update_line_item_state_exception(self, mock_state_by_id):
"errors": [
{
"code": "ConcurrentModification",
"detailedErrorMessage": "Object [mock_order_id] has a "
"different version than expected. Expected: 2 - Actual: 1."
"message": "Object [mock_order_id] has a "
"different version than expected. Expected: 2 - Actual: 1."
},
],
"response": {},
Expand All @@ -686,8 +686,9 @@ def test_update_line_item_state_exception(self, mock_state_by_id):

expected_message = (
f"[CommercetoolsError] Unable to update LineItemState "
f"of order mock_order_id with error correlation id {mock_error_response['correlation_id']} "
f"and error/s: {mock_error_response['errors']}"
f"of order mock_order_id "
f"- Correlation ID: {mock_error_response['correlation_id']}, "
f"Details: {mock_error_response['errors']}"
)

log_mock.assert_called_once_with(expected_message)
Expand Down
8 changes: 4 additions & 4 deletions commerce_coordinator/apps/commercetools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from braze.client import BrazeClient
from commercetools import CommercetoolsError
from commercetools.platform.models import Customer, LineItem, Order, Payment, TransactionState, TransactionType

from django.conf import settings
from django.urls import reverse

Expand All @@ -33,11 +32,12 @@ def get_braze_client():
app_id='',
)

def handle_commercetools_error(err: CommercetoolsError, context: str) -> None:
error_details = [f"Code: {e.code}, Message: {e.message}" for e in err.errors]
error_message = f"[CommercetoolsError] {context} - Correlation ID: {err.correlation_id}, Details: {error_details}"

def handle_commercetools_error(err: CommercetoolsError, context: str):
error_message = f"[CommercetoolsError] {context} - Correlation ID: {err.correlation_id}, Details: {err.errors}"
logger.error(error_message)


def send_order_confirmation_email(
lms_user_id, lms_user_email, canvas_entry_properties
):
Expand Down

0 comments on commit 8827781

Please sign in to comment.