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

Commit

Permalink
refactor: updated code to make voucher name unique
Browse files Browse the repository at this point in the history
  • Loading branch information
zubair-ce07 committed Nov 13, 2023
1 parent 04dc5d5 commit 520d31a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions ecommerce/extensions/api/v2/tests/views/test_coupons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion ecommerce/extensions/api/v2/tests/views/test_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions ecommerce/extensions/api/v2/views/coupons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions ecommerce/extensions/voucher/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': ''},
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion ecommerce/extensions/voucher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 520d31a

Please sign in to comment.