Skip to content

Commit

Permalink
fix: Fixed program subscription price update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shafqatfarhan authored and uzairr committed Jun 15, 2023
1 parent ccab626 commit 52735ad
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3115,20 +3115,31 @@
'title': 'Test Program',
'subscription price': '$79.89',
'subscription_eligible': 'TRUE',
'currency': 'USD',
}

INVALID_PROGRAM_SUBSCRIPTION_DICT = {
'uuid': '0668eef0-5dcf-41dc-80cd-6222d2de4d22',
'title': 'Test Program 2',
'subscription price': '$79.99',
'subscription_eligible': 'FALSE',
'currency': 'USD',
}

PROGRAM_WITH_INVALID_SUBSCRIPTION_ELIGIBILITY_DICT = {
'uuid': '0668eef0-5dcf-41dc-80cd-6222d2de4d22',
'title': 'Test Program 2',
'subscription price': '$79.99',
'subscription_eligible': 'test-flag',
'currency': 'USD',
}

PROGRAM_WITH_INVALID_CURRENCY_DICT = {
'uuid': '0668eef0-5dcf-41dc-80cd-6222d2de4d22',
'title': 'Test Program 2',
'subscription price': '$79.99',
'subscription_eligible': 'TRUE',
'currency': 'invalid-currency',
}

VALID_MINIMAL_DEGREE_CSV_DICT = VALID_DEGREE_CSV_DICT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.db.models.signals import post_delete, post_save

from course_discovery.apps.api.cache import api_change_receiver
from course_discovery.apps.core.models import Currency
from course_discovery.apps.course_metadata.gspread_client import GspreadClient
from course_discovery.apps.course_metadata.models import (
Program, ProgramSubscription, ProgramSubscriptionConfiguration, ProgramSubscriptionPrice
Expand Down Expand Up @@ -58,10 +59,10 @@ def handle(self, *args, **options):

except Exception:
raise CommandError( # pylint: disable=raise-missing-from
"Error reading the input data source"
'Error reading the input data source'
)

logger.info("Initiating Program CSV data loader flow.")
logger.info('Initiating Program CSV data loader flow.')
for row in reader:
row = self.transform_dict_keys(row)
program_uuid = row.get('uuid', None)
Expand All @@ -70,7 +71,15 @@ def handle(self, *args, **options):
subscription_eligible = row.get('subscription_eligible', None)

if subscription_eligible not in ['TRUE', 'FALSE']:
logger.info(f"Skipped record: {program_uuid} because of invalid subscription eligibility value")
logger.warning(f'Skipped record: {program_uuid} because of '
f'invalid subscription eligibility value')
continue

currency_code = row.get('currency', None)
try:
currency = Currency.objects.get(code=currency_code)
except Currency.DoesNotExist:
logger.warning(f'Could not find currency {currency_code} for program {program_uuid}')
continue

try:
Expand All @@ -83,13 +92,15 @@ def handle(self, *args, **options):
program=program, defaults=default_params
)
subscription_price, created = ProgramSubscriptionPrice.objects.update_or_create(
program_subscription=subscription, price=price
program_subscription=subscription, currency=currency, defaults={'price': price}
)
created_or_updated = 'created' if created else 'updated'
logger.info(f"Program located with slug: {program.marketing_slug}."
f"Its subscription with price: {subscription_price.price} USD is {created_or_updated}")
logger.info(f'Program ({program_uuid}) located with slug: {program.marketing_slug} '
f'is marked {"eligible" if subscription_eligible else "ineligible"} '
f'for subscription and its price: {subscription_price.price} '
f'{currency_code} is {created_or_updated}')
except Program.DoesNotExist:
logger.exception(f"Unable to locate Program instance with code {program_uuid}")
logger.exception(f'Unable to locate Program instance with code {program_uuid}')

def transform_dict_keys(self, data):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ def test_subscription_created_and_updated(self):
with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
subscription = ProgramSubscription.objects.get(program=self.program)
subscription_price = ProgramSubscriptionPrice.objects.get(program_subscription=subscription)
expected_log = f"Program located with slug: {self.program.marketing_slug}." \
f"Its subscription with price: {subscription_price.price} USD is created"
subscription_price = ProgramSubscriptionPrice.objects.get(
program_subscription=subscription,
currency='USD'
)
expected_log = f'Program ({self.program.uuid}) located with slug: ' \
f'{self.program.marketing_slug} is marked eligible for subscription ' \
f'and its price: {subscription_price.price} USD is created'
log_capture.check_present(
(LOGGER_PATH, 'INFO', expected_log)
)

with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
subscription = ProgramSubscription.objects.get(program=self.program)
subscription_price = ProgramSubscriptionPrice.objects.get(program_subscription=subscription)
expected_log = f"Program located with slug: {self.program.marketing_slug}." \
f"Its subscription with price: {subscription_price.price} USD is updated"
subscription_price = ProgramSubscriptionPrice.objects.get(
program_subscription=subscription,
currency='USD'
)
expected_log = f'Program ({self.program.uuid}) located with slug: ' \
f'{self.program.marketing_slug} is marked eligible for subscription ' \
f'and its price: {subscription_price.price} USD is updated'
log_capture.check_present(
(LOGGER_PATH, 'INFO', expected_log)
)
Expand All @@ -76,8 +84,8 @@ def test_subscription_updated_if_exists(self):

with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
expected_log = f"Unable to locate Program instance with code " \
f"{mock_data.INVALID_PROGRAM_SUBSCRIPTION_DICT['uuid']}"
expected_log = f'Unable to locate Program instance with code ' \
f'{mock_data.INVALID_PROGRAM_SUBSCRIPTION_DICT["uuid"]}'
log_capture.check_present(
(LOGGER_PATH, 'ERROR', expected_log)
)
Expand All @@ -98,10 +106,10 @@ def test_record_skipped_if_subscription_eligibility_is_invalid(self):

with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
expected_log = f"Skipped record: {mock_data.PROGRAM_WITH_INVALID_SUBSCRIPTION_ELIGIBILITY_DICT['uuid']} " \
f"because of invalid subscription eligibility value"
expected_log = f'Skipped record: {mock_data.PROGRAM_WITH_INVALID_SUBSCRIPTION_ELIGIBILITY_DICT["uuid"]} ' \
f'because of invalid subscription eligibility value'
log_capture.check_present(
(LOGGER_PATH, 'INFO', expected_log)
(LOGGER_PATH, 'WARNING', expected_log)
)

def test_subscription_object_should_be_updated_if_created_earlier(self):
Expand All @@ -121,10 +129,14 @@ def test_subscription_object_should_be_updated_if_created_earlier(self):
with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
subscription = ProgramSubscription.objects.get(program=self.program)
subscription_price = ProgramSubscriptionPrice.objects.get(program_subscription=subscription)
subscription_price = ProgramSubscriptionPrice.objects.get(
program_subscription=subscription,
currency='USD'
)
assert len(ProgramSubscription.objects.all()) == 1
expected_log = f"Program located with slug: {self.program.marketing_slug}." \
f"Its subscription with price: {subscription_price.price} USD is created"
expected_log = f'Program ({self.program.uuid}) located with slug: ' \
f'{self.program.marketing_slug} is marked eligible for subscription ' \
f'and its price: {subscription_price.price} USD is created'
log_capture.check_present(
(LOGGER_PATH, 'INFO', expected_log)
)
Expand All @@ -143,10 +155,36 @@ def test_subscription_object_should_be_updated_if_created_earlier(self):
with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
subscription = ProgramSubscription.objects.get(program=self.program)
subscription_price = ProgramSubscriptionPrice.objects.get(program_subscription=subscription)
subscription_price = ProgramSubscriptionPrice.objects.get(
program_subscription=subscription,
currency='USD'
)
assert len(ProgramSubscription.objects.all()) == 1
expected_log = f"Program located with slug: {self.program.marketing_slug}." \
f"Its subscription with price: {subscription_price.price} USD is updated"
expected_log = f'Program ({self.program.uuid}) located with slug: ' \
f'{self.program.marketing_slug} is marked ineligible for subscription ' \
f'and its price: {subscription_price.price} USD is updated'
log_capture.check_present(
(LOGGER_PATH, 'INFO', expected_log)
)

def test_record_skipped_if_currency_is_invalid(self):
"""
Test that the log would be captured in case the record is skipped.
"""
csv_file_content = ','.join(list(mock_data.PROGRAM_WITH_INVALID_CURRENCY_DICT)) + '\n'
csv_file_content += ','.join(f'"{key}"' for key in list(
mock_data.PROGRAM_WITH_INVALID_CURRENCY_DICT.values()))
self.csv_file = SimpleUploadedFile(
name='test.csv',
content=csv_file_content.encode('utf-8'),
content_type='text/csv'
)
_ = ProgramSubscriptionConfigurationFactory.create(enabled=True, csv_file=self.csv_file)

with LogCapture(LOGGER_PATH) as log_capture:
call_command('manage_program_subscription')
expected_log = f'Could not find currency {mock_data.PROGRAM_WITH_INVALID_CURRENCY_DICT["currency"]} ' \
f'for program {mock_data.PROGRAM_WITH_INVALID_CURRENCY_DICT["uuid"]}'
log_capture.check_present(
(LOGGER_PATH, 'WARNING', expected_log)
)

0 comments on commit 52735ad

Please sign in to comment.