From 520d31a14d35360e18b5abccca4ef55fa2be79d2 Mon Sep 17 00:00:00 2001 From: zubair-ce07 Date: Mon, 13 Nov 2023 12:36:51 +0500 Subject: [PATCH] refactor: updated code to make voucher name unique --- ...test_migrate_enterprise_conditional_offers_command.py | 9 ++++++--- ecommerce/extensions/api/v2/tests/views/test_coupons.py | 5 +++-- ecommerce/extensions/api/v2/tests/views/test_products.py | 2 +- ecommerce/extensions/api/v2/views/coupons.py | 7 +++++++ ecommerce/extensions/voucher/tests/test_utils.py | 6 +++--- ecommerce/extensions/voucher/utils.py | 3 ++- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ecommerce/enterprise/tests/test_migrate_enterprise_conditional_offers_command.py b/ecommerce/enterprise/tests/test_migrate_enterprise_conditional_offers_command.py index 4a4cf23bd3c..0b9ccacdc24 100644 --- a/ecommerce/enterprise/tests/test_migrate_enterprise_conditional_offers_command.py +++ b/ecommerce/enterprise/tests/test_migrate_enterprise_conditional_offers_command.py @@ -54,7 +54,8 @@ def setUp(self): for i in range(2): code = '{}EntUserPercentBenefit'.format(i) - voucher = VoucherFactory(code=code) + name = 'Test_1 voucher{}'.format(i) + voucher = VoucherFactory(code=code, name=name) offer_name = "Coupon [{}]-{}-{}".format( voucher.pk, benefit_percent.type, @@ -69,7 +70,8 @@ def setUp(self): for i in range(2): code = '{}EntUserAbsoluteBenefit'.format(i) - voucher = VoucherFactory(code=code) + name = 'Test_2 voucher{}'.format(i) + voucher = VoucherFactory(code=code, name=name) offer_name = "Coupon [{}]-{}-{}".format( voucher.pk, benefit_absolute.type, @@ -93,7 +95,8 @@ def setUp(self): for i in range(3): code = '{}NoEntUserPercentBenefit'.format(i) - voucher = VoucherFactory(code=code) + name = 'Test_3 voucher{}'.format(i) + voucher = VoucherFactory(code=code, name=name) offer_name = "Coupon [{}]-{}-{}".format( voucher.pk, benefit.type, diff --git a/ecommerce/extensions/api/v2/tests/views/test_coupons.py b/ecommerce/extensions/api/v2/tests/views/test_coupons.py index c832abc30b3..3415ebe6166 100644 --- a/ecommerce/extensions/api/v2/tests/views/test_coupons.py +++ b/ecommerce/extensions/api/v2/tests/views/test_coupons.py @@ -168,7 +168,7 @@ def test_clean_voucher_request_data_notify_email_validation_msg(self): def test_creating_multi_offer_coupon(self): """Test the creation of a multi-offer coupon.""" - ordinary_coupon = self.create_coupon(quantity=2) + ordinary_coupon = self.create_coupon(quantity=2, title='Test offer coupon') ordinary_coupon_vouchers = ordinary_coupon.attr.coupon_vouchers.vouchers.all() self.assertEqual( ordinary_coupon_vouchers[0].offers.first(), @@ -607,7 +607,8 @@ def test_update_name(self): new_coupon = Product.objects.get(id=self.coupon.id) vouchers = new_coupon.attr.coupon_vouchers.vouchers.all() for voucher in vouchers: - self.assertEqual(voucher.name, 'New voucher name') + new_voucher_name = "%s - %d" % (data['name'], voucher.id + 1) + self.assertEqual(voucher.name, new_voucher_name) def test_update_datetimes(self): """Test that updating a coupons date updates all of it's voucher dates.""" diff --git a/ecommerce/extensions/api/v2/tests/views/test_products.py b/ecommerce/extensions/api/v2/tests/views/test_products.py index 5f622716a35..1de67d9790a 100644 --- a/ecommerce/extensions/api/v2/tests/views/test_products.py +++ b/ecommerce/extensions/api/v2/tests/views/test_products.py @@ -201,7 +201,7 @@ def test_coupon_voucher_serializer(self): response_data = response.json() voucher = response_data['attribute_values'][0]['value'][0] - self.assertEqual(voucher['name'], 'Test coupon') + self.assertEqual(voucher['name'], 'Test coupon' + voucher['code']) self.assertEqual(voucher['usage'], Voucher.SINGLE_USE) self.assertEqual(voucher['benefit']['type'], Benefit.PERCENTAGE) self.assertEqual(voucher['benefit']['value'], 100.0) diff --git a/ecommerce/extensions/api/v2/views/coupons.py b/ecommerce/extensions/api/v2/views/coupons.py index 49b23b9b4f1..5c064c3791b 100644 --- a/ecommerce/extensions/api/v2/views/coupons.py +++ b/ecommerce/extensions/api/v2/views/coupons.py @@ -374,6 +374,13 @@ def update(self, request, *args, **kwargs): def update_voucher_data(self, request_data, vouchers): data = self.create_update_data_dict(data=request_data, fields=CouponVouchers.UPDATEABLE_VOUCHER_FIELDS) if data: + if 'name' in data: + for voucher in vouchers: + voucher.name = "%s - %d" % (data['name'], voucher.id + 1) + voucher.save() + + data.pop('name') + vouchers.update(**data) def create_update_data_dict(self, data, fields): diff --git a/ecommerce/extensions/voucher/tests/test_utils.py b/ecommerce/extensions/voucher/tests/test_utils.py index dc843152787..a96539e675f 100644 --- a/ecommerce/extensions/voucher/tests/test_utils.py +++ b/ecommerce/extensions/voucher/tests/test_utils.py @@ -255,11 +255,11 @@ def test_create_voucher_with_long_name(self): }) trimmed = ( 'This Is A Really Really Really Really Really Really Long ' - 'Voucher Name That Needs To Be Trimmed To Fit Into The Name Column Of Th' + 'Voucher Name That Needs To Be Trimmed To Fit Into The N' ) vouchers = create_vouchers(**self.data) voucher = vouchers[0] - self.assertEqual(voucher.name, trimmed) + self.assertEqual(voucher.name, trimmed + voucher.code) @ddt.data( {'end_datetime': ''}, @@ -540,7 +540,7 @@ def test_report_for_inactive_coupons(self): # are only shown in row[0] # The data that is unique among vouchers like Code, Url, Status, etc. # starts from row[1] - self.assertEqual(rows[0]['Coupon Name'], self.coupon.title) + self.assertEqual(rows[0]['Coupon Name'], self.coupon.title + rows[1]['Code']) self.assertEqual(rows[2]['Status'], _('Inactive')) def test_generate_coupon_report_for_query_coupons(self): diff --git a/ecommerce/extensions/voucher/utils.py b/ecommerce/extensions/voucher/utils.py index 85556e894a3..704a73252ad 100644 --- a/ecommerce/extensions/voucher/utils.py +++ b/ecommerce/extensions/voucher/utils.py @@ -537,8 +537,9 @@ def create_new_voucher(code, end_datetime, name, start_datetime, voucher_type): if not isinstance(end_datetime, datetime.datetime): end_datetime = dateutil.parser.parse(end_datetime) + name = name[:128 - len(voucher_code)] + voucher_code voucher = Voucher.objects.create( - name=name[:128], + name=name, code=voucher_code, usage=voucher_type, start_datetime=start_datetime,