Skip to content

Commit

Permalink
refactor: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Oct 21, 2024
1 parent 9490bef commit 32f3f38
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def get_variants(self, product):
Returns:
list: A list of variant dicts
"""
variant_keys = ['variant', 'variants', 'future_variants', 'custom_persentations']
variant_keys = ['variant', 'variants', 'future_variants', 'custom_presentations']
variants = []

for key in variant_keys:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase):

SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS = copy.deepcopy(SUCCESS_API_RESPONSE)
SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS['products'][0].update({
'custom_persentations': [{**copy.deepcopy(variant_1), 'websiteVisibility': 'private', 'status': 'active'}],
'custom_presentations': [{**copy.deepcopy(variant_1), 'websiteVisibility': 'private', 'status': 'active'}],
'future_variants': [
{
**copy.deepcopy(variant_2), 'websiteVisibility': 'public', 'status': 'scheduled',
Expand Down Expand Up @@ -239,12 +239,16 @@ def test_skip_products_ingestion_if_variants_data_empty(self, mock_get_smarter_c
assert not any(reader)

@mock.patch("course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient")
def test_populate_executive_education_data_csv_with_new_variants_structure_changes(self, mock_get_smarter_client):
def test_populate_executive_education_data_csv_with_new_variants_structure_changes(
self, mock_get_smarter_client
):
"""
Verify the successful population has data from API response if getsmarter flag is provided and
the product can have multiple variants
"""
success_api_response = copy.deepcopy(self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS)
success_api_response = copy.deepcopy(
self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS
)
mock_get_smarter_client.return_value.request.return_value.json.return_value = (
self.mock_get_smarter_client_response(
override_get_smarter_client_response=success_api_response
Expand All @@ -253,19 +257,41 @@ def test_populate_executive_education_data_csv_with_new_variants_structure_chang
with NamedTemporaryFile() as output_csv:
call_command(
"populate_executive_education_data_csv",
"--output_csv", output_csv.name,
"--use_getsmarter_api_client", True,
"--output_csv",
output_csv.name,
"--use_getsmarter_api_client",
True,
)

output_csv.seek(0)
simple_variant = self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS["products"][0]["variant"]
future_variant = self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS["products"][0]["future_variants"][0]
custom_variant = self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS[
"products"
][0]["custom_presentations"][0]

with open(output_csv.name, "r") as csv_file:
reader = csv.DictReader(csv_file)
assert len(list(reader)) == (
len(self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS["products"][0]["custom_persentations"]) +
len(self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS["products"][0]["future_variants"]) +
1 if self.SUCCESS_API_RESPONSE_CUSTOM_AND_FUTURE_VARIANTS["products"][0]["variant"]
else 0
)

data_row = next(reader)
assert data_row["Variant Id"] == simple_variant["id"]
assert data_row["Start Date"] == simple_variant["startDate"]
assert data_row["End Date"] == simple_variant["endDate"]
assert data_row["Reg Close Date"] == simple_variant["finalRegCloseDate"]
assert data_row["Restriction Type"] == "None"

data_row = next(reader)
assert data_row["Variant Id"] == future_variant["id"]
assert data_row["Start Date"] == future_variant["startDate"]
assert data_row["End Date"] == future_variant["endDate"]
assert data_row["Reg Close Date"] == future_variant["finalRegCloseDate"]
assert data_row["Restriction Type"] == "None"

data_row = next(reader)
assert data_row["Variant Id"] == custom_variant["id"]
assert data_row["Start Date"] == custom_variant["startDate"]
assert data_row["End Date"] == custom_variant["endDate"]
assert data_row["Reg Close Date"] == custom_variant["finalRegCloseDate"]
assert data_row["Restriction Type"] == "custom-b2b-enterprise"

@mock.patch('course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient')
def test_successful_file_data_population_with_getsmarter_flag_with_multiple_variants(self, mock_get_smarter_client):
Expand Down

0 comments on commit 32f3f38

Please sign in to comment.