Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
refactor: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Faraz Maqsood committed Jun 14, 2024
1 parent 146fe13 commit 9360ac0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 66 deletions.
6 changes: 1 addition & 5 deletions ecommerce/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


import json
import sys

import ddt
import mock
Expand Down Expand Up @@ -178,10 +177,7 @@ def test_user_details_uses_jwt(self):

# Verify the headers passed to the API were correct.
expected = {'Authorization': 'JWT {}'.format(token), }
if sys.version_info > (3, 9):
self.assertLessEqual(expected.items(), last_request.headers.items())
else:
self.assertDictContainsSubset(expected, last_request.headers)
self.assertLessEqual(expected.items(), last_request.headers.items())

def test_no_user_details(self):
""" Verify False is returned when there is a connection error. """
Expand Down
29 changes: 13 additions & 16 deletions ecommerce/credit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Tests for the checkout page.
"""

import sys
from datetime import timedelta

import ddt
Expand Down Expand Up @@ -130,21 +129,19 @@ def _assert_success_checkout_page(self, sku=None):

response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
if sys.version_info > (3, 9):
# assertDictContainsSubset is depreciated in python version>3.9
# context.response return ContextList object, belwo statements will convert it to dict
# assertLessEqual method is used instead of depreciated assertDictContainsSubset method
context = {}
for i, ctx in enumerate(response.context):
if isinstance(ctx, dict):
context.update(ctx)
elif hasattr(ctx, '__iter__') and not isinstance(ctx, str):
for item in ctx:
if isinstance(item, dict):
context.update(item)
self.assertLessEqual({'course': self.course}.items(), context.items())
else:
self.assertDictContainsSubset({'course': self.course}, response.context)

# assertDictContainsSubset is deprecated in Python version > 3.9
# response.context returns a ContextList object; the below statements will convert it to a dict
# assertLessEqual method is used instead of the deprecated assertDictContainsSubset method
context = {}
for i, ctx in enumerate(response.context):
if isinstance(ctx, dict):
context.update(ctx)
elif hasattr(ctx, '__iter__') and not isinstance(ctx, str):
for item in ctx:
if isinstance(item, dict):
context.update(item)
self.assertLessEqual({'course': self.course}.items(), context.items())

self.assertContains(
response,
Expand Down
6 changes: 1 addition & 5 deletions ecommerce/enterprise/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


import sys
import uuid

import ddt
Expand Down Expand Up @@ -123,10 +122,7 @@ def test_post_enterprise_customer_user(self, mock_helpers, expected_return):
self.learner.username
)

if sys.version_info > (3, 9):
self.assertLessEqual(expected_return.items(), response.items())
else:
self.assertDictContainsSubset(expected_return, response)
self.assertLessEqual(expected_return.items(), response.items())

@responses.activate
def test_ecu_needs_consent(self):
Expand Down
31 changes: 6 additions & 25 deletions ecommerce/extensions/checkout/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


import sys
from decimal import Decimal
from urllib import parse

Expand Down Expand Up @@ -339,10 +338,7 @@ def test_get_receipt_for_existing_order(self, mock_learner_data):
}

self.assertEqual(response.status_code, 200)
if sys.version_info > (3, 9):
self.assertEqual(response.context_data, response.context_data | context_data)
else:
self.assertDictContainsSubset(context_data, response.context_data)
self.assertLessEqual(context_data.items(), response.context_data.items())

@patch('ecommerce.extensions.checkout.views.fetch_enterprise_learner_data')
@responses.activate
Expand Down Expand Up @@ -375,10 +371,7 @@ def test_get_receipt_for_existing_entitlement_order(self, mock_learner_data):
}

self.assertEqual(response.status_code, 200)
if sys.version_info > (3, 9):
self.assertEqual(response.context_data, response.context_data | context_data)
else:
self.assertDictContainsSubset(context_data, response.context_data)
self.assertLessEqual(context_data.items(), response.context_data.items())

@patch('ecommerce.extensions.checkout.views.fetch_enterprise_learner_data')
@responses.activate
Expand All @@ -394,10 +387,7 @@ def test_get_receipt_for_existing_order_as_staff_user(self, mock_learner_data):
}

self.assertEqual(response.status_code, 200)
if sys.version_info > (3, 9):
self.assertEqual(response.context_data, response.context_data | context_data)
else:
self.assertDictContainsSubset(context_data, response.context_data)
self.assertLessEqual(context_data.items(), response.context_data.items())

@patch('ecommerce.extensions.checkout.views.fetch_enterprise_learner_data')
@responses.activate
Expand All @@ -410,10 +400,7 @@ def test_get_receipt_for_existing_order_user_not_owner(self, mock_learner_data):
context_data = {'order_history_url': self.site.siteconfiguration.build_lms_url('account/settings')}

self.assertEqual(response.status_code, 404)
if sys.version_info > (3, 9):
self.assertEqual(response.context_data, response.context_data | context_data)
else:
self.assertDictContainsSubset(context_data, response.context_data)
self.assertLessEqual(context_data.items(), response.context_data.items())

@patch('ecommerce.extensions.checkout.views.fetch_enterprise_learner_data')
@responses.activate
Expand Down Expand Up @@ -469,10 +456,7 @@ def test_dashboard_link_for_course_purchase(self, mock_learner_data):
}

self.assertEqual(response.status_code, 200)
if sys.version_info > (3, 9):
self.assertEqual(response.context_data, response.context_data | context_data)
else:
self.assertDictContainsSubset(context_data, response.context_data)
self.assertLessEqual(context_data.items(), response.context_data.items())

@patch('ecommerce.extensions.checkout.views.fetch_enterprise_learner_data')
@responses.activate
Expand All @@ -498,10 +482,7 @@ def test_dashboard_link_for_bundle_purchase(self, mock_learner_data):
}

self.assertEqual(response.status_code, 200)
if sys.version_info > (3, 9):
self.assertEqual(response.context_data, response.context_data | context_data)
else:
self.assertDictContainsSubset(context_data, response.context_data)
self.assertLessEqual(context_data.items(), response.context_data.items())

@patch('ecommerce.extensions.checkout.views.fetch_enterprise_learner_data')
@responses.activate
Expand Down
6 changes: 1 addition & 5 deletions ecommerce/extensions/fulfillment/tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import datetime
import json
import sys
import uuid
from decimal import Decimal
from urllib.parse import urlencode
Expand Down Expand Up @@ -213,10 +212,7 @@ def test_enrollment_module_fulfill(self):
'X-Forwarded-For': self.user.tracking_context['lms_ip'],
}

if sys.version_info > (3, 9):
self.assertLessEqual(expected_headers.items(), actual_headers.items())
else:
self.assertDictContainsSubset(expected_headers, actual_headers)
self.assertLessEqual(expected_headers.items(), actual_headers.items())
self.assertEqual(expected_body, actual_body)

@responses.activate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


import json
import sys
from decimal import Decimal
from unittest import SkipTest

Expand Down Expand Up @@ -50,10 +49,7 @@ def assert_processor_response_recorded(self, processor_name, transaction_id, res
expected = {
'requestID': transaction_id,
}
if sys.version_info > (3, 9):
self.assertLessEqual(expected.items(), ppr.response.items())
else:
self.assertDictContainsSubset(expected, ppr.response)
self.assertLessEqual(expected.items(), ppr.response.items())
self.assertEqual(ppr.basket, basket)

return ppr.id
Expand Down
6 changes: 1 addition & 5 deletions ecommerce/extensions/voucher/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-


import sys
import uuid

import ddt
Expand Down Expand Up @@ -524,10 +523,7 @@ def test_generate_coupon_report_with_deleted_product(self):

__, rows = generate_coupon_report([query_coupon.attr.coupon_vouchers])
self.assert_report_first_row(rows[0], query_coupon, first_voucher)
if sys.version_info > (3, 9):
self.assertLessEqual({'Redeemed For Course ID': 'Unknown'}.items(), rows[2].items())
else:
self.assertDictContainsSubset({'Redeemed For Course ID': 'Unknown'}, rows[2])
self.assertLessEqual({'Redeemed For Course ID': 'Unknown'}.items(), rows[2].items())

def test_report_for_inactive_coupons(self):
""" Verify the coupon report show correct status for inactive coupons. """
Expand Down

0 comments on commit 9360ac0

Please sign in to comment.