From 50354170adadbf35ae8260964cb126858419098d Mon Sep 17 00:00:00 2001 From: Muhammad Faraz Maqsood Date: Thu, 13 Jun 2024 13:12:26 +0500 Subject: [PATCH] refactor: address comments --- ecommerce/core/tests/test_models.py | 5 +--- ecommerce/credit/tests/test_views.py | 28 ++++++++--------- ecommerce/enterprise/tests/test_utils.py | 5 +--- .../extensions/checkout/tests/test_views.py | 30 ++++--------------- .../fulfillment/tests/test_modules.py | 5 +--- .../tests/processors/test_cybersource.py | 5 +--- .../extensions/voucher/tests/test_utils.py | 5 +--- 7 files changed, 24 insertions(+), 59 deletions(-) diff --git a/ecommerce/core/tests/test_models.py b/ecommerce/core/tests/test_models.py index b4fcf286fcc..385bbe0d41e 100644 --- a/ecommerce/core/tests/test_models.py +++ b/ecommerce/core/tests/test_models.py @@ -178,10 +178,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. """ diff --git a/ecommerce/credit/tests/test_views.py b/ecommerce/credit/tests/test_views.py index 80b8595acc3..b291b8ba474 100644 --- a/ecommerce/credit/tests/test_views.py +++ b/ecommerce/credit/tests/test_views.py @@ -130,21 +130,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, diff --git a/ecommerce/enterprise/tests/test_utils.py b/ecommerce/enterprise/tests/test_utils.py index ccae48c4b30..a1b14a29ca0 100644 --- a/ecommerce/enterprise/tests/test_utils.py +++ b/ecommerce/enterprise/tests/test_utils.py @@ -123,10 +123,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): diff --git a/ecommerce/extensions/checkout/tests/test_views.py b/ecommerce/extensions/checkout/tests/test_views.py index 311c9dbfa9a..62d599443eb 100644 --- a/ecommerce/extensions/checkout/tests/test_views.py +++ b/ecommerce/extensions/checkout/tests/test_views.py @@ -339,10 +339,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 @@ -375,10 +372,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 @@ -394,10 +388,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 @@ -410,10 +401,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 @@ -469,10 +457,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 @@ -498,10 +483,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 diff --git a/ecommerce/extensions/fulfillment/tests/test_modules.py b/ecommerce/extensions/fulfillment/tests/test_modules.py index 880dcd077f3..43d333d8379 100644 --- a/ecommerce/extensions/fulfillment/tests/test_modules.py +++ b/ecommerce/extensions/fulfillment/tests/test_modules.py @@ -213,10 +213,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 diff --git a/ecommerce/extensions/payment/tests/processors/test_cybersource.py b/ecommerce/extensions/payment/tests/processors/test_cybersource.py index fbdac1c3d9f..c79269b938b 100644 --- a/ecommerce/extensions/payment/tests/processors/test_cybersource.py +++ b/ecommerce/extensions/payment/tests/processors/test_cybersource.py @@ -50,10 +50,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 diff --git a/ecommerce/extensions/voucher/tests/test_utils.py b/ecommerce/extensions/voucher/tests/test_utils.py index e59dfa87339..069363b5887 100644 --- a/ecommerce/extensions/voucher/tests/test_utils.py +++ b/ecommerce/extensions/voucher/tests/test_utils.py @@ -524,10 +524,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. """