From 9360ac074b75a572f9addae47e062c171875ebbe Mon Sep 17 00:00:00 2001 From: Muhammad Faraz Maqsood Date: Fri, 14 Jun 2024 14:13:55 +0500 Subject: [PATCH] refactor: address comments --- ecommerce/core/tests/test_models.py | 6 +--- ecommerce/credit/tests/test_views.py | 29 ++++++++--------- ecommerce/enterprise/tests/test_utils.py | 6 +--- .../extensions/checkout/tests/test_views.py | 31 ++++--------------- .../fulfillment/tests/test_modules.py | 6 +--- .../tests/processors/test_cybersource.py | 6 +--- .../extensions/voucher/tests/test_utils.py | 6 +--- 7 files changed, 24 insertions(+), 66 deletions(-) diff --git a/ecommerce/core/tests/test_models.py b/ecommerce/core/tests/test_models.py index b4fcf286fcc..423b1be32eb 100644 --- a/ecommerce/core/tests/test_models.py +++ b/ecommerce/core/tests/test_models.py @@ -1,7 +1,6 @@ import json -import sys import ddt import mock @@ -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. """ diff --git a/ecommerce/credit/tests/test_views.py b/ecommerce/credit/tests/test_views.py index 80b8595acc3..d5b93272836 100644 --- a/ecommerce/credit/tests/test_views.py +++ b/ecommerce/credit/tests/test_views.py @@ -2,7 +2,6 @@ Tests for the checkout page. """ -import sys from datetime import timedelta import ddt @@ -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, diff --git a/ecommerce/enterprise/tests/test_utils.py b/ecommerce/enterprise/tests/test_utils.py index ccae48c4b30..bd83dcac305 100644 --- a/ecommerce/enterprise/tests/test_utils.py +++ b/ecommerce/enterprise/tests/test_utils.py @@ -1,6 +1,5 @@ -import sys import uuid import ddt @@ -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): diff --git a/ecommerce/extensions/checkout/tests/test_views.py b/ecommerce/extensions/checkout/tests/test_views.py index 311c9dbfa9a..8adfa8f9039 100644 --- a/ecommerce/extensions/checkout/tests/test_views.py +++ b/ecommerce/extensions/checkout/tests/test_views.py @@ -1,6 +1,5 @@ -import sys from decimal import Decimal from urllib import parse @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/ecommerce/extensions/fulfillment/tests/test_modules.py b/ecommerce/extensions/fulfillment/tests/test_modules.py index 880dcd077f3..ff8a5ff4e63 100644 --- a/ecommerce/extensions/fulfillment/tests/test_modules.py +++ b/ecommerce/extensions/fulfillment/tests/test_modules.py @@ -3,7 +3,6 @@ import datetime import json -import sys import uuid from decimal import Decimal from urllib.parse import urlencode @@ -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 diff --git a/ecommerce/extensions/payment/tests/processors/test_cybersource.py b/ecommerce/extensions/payment/tests/processors/test_cybersource.py index fbdac1c3d9f..aa59b4c3680 100644 --- a/ecommerce/extensions/payment/tests/processors/test_cybersource.py +++ b/ecommerce/extensions/payment/tests/processors/test_cybersource.py @@ -3,7 +3,6 @@ import json -import sys from decimal import Decimal from unittest import SkipTest @@ -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 diff --git a/ecommerce/extensions/voucher/tests/test_utils.py b/ecommerce/extensions/voucher/tests/test_utils.py index e59dfa87339..f8709e4d3b1 100644 --- a/ecommerce/extensions/voucher/tests/test_utils.py +++ b/ecommerce/extensions/voucher/tests/test_utils.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- -import sys import uuid import ddt @@ -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. """