From 33b740002da4b46752ea387a4cb31e8d2ab3c3a2 Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Wed, 16 Oct 2024 19:54:32 +0500 Subject: [PATCH] refactor: updated a test case --- ...t_populate_executive_education_data_csv.py | 587 ++++++++++-------- 1 file changed, 345 insertions(+), 242 deletions(-) diff --git a/course_discovery/apps/course_metadata/management/commands/tests/test_populate_executive_education_data_csv.py b/course_discovery/apps/course_metadata/management/commands/tests/test_populate_executive_education_data_csv.py index a2b9728dc28..83173e30573 100644 --- a/course_discovery/apps/course_metadata/management/commands/tests/test_populate_executive_education_data_csv.py +++ b/course_discovery/apps/course_metadata/management/commands/tests/test_populate_executive_education_data_csv.py @@ -1,10 +1,12 @@ """ Unit tests for populate_executive_education_data_csv management command. """ + import copy import csv import json from datetime import date +from ddt import ddt, data, unpack from tempfile import NamedTemporaryFile import mock @@ -15,18 +17,22 @@ from testfixtures import LogCapture from course_discovery.apps.course_metadata.data_loaders.tests import mock_data -from course_discovery.apps.course_metadata.data_loaders.tests.mixins import CSVLoaderMixin +from course_discovery.apps.course_metadata.data_loaders.tests.mixins import ( + CSVLoaderMixin, +) -LOGGER_PATH = 'course_discovery.apps.course_metadata.management.commands.populate_executive_education_data_csv' +LOGGER_PATH = "course_discovery.apps.course_metadata.management.commands.populate_executive_education_data_csv" +@ddt class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase): """ Test suite for populate_executive_education_data_csv management command. """ - AUTH_TOKEN = 'auth_token' + + AUTH_TOKEN = "auth_token" SUCCESS_API_RESPONSE = { - 'products': [ + "products": [ { "id": "12345678", "name": "CSV Course", @@ -45,9 +51,9 @@ class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase): "edxPlpUrl": "aHR0cHM6Ly9leGFtcGxlLmNvbS8=", "durationWeeks": 10, "effort": "7–10 hours per week", - 'introduction': 'Very short description\n', - 'isThisCourseForYou': 'This is supposed to be a long description', - 'whatWillSetYouApart': "New ways to learn", + "introduction": "Very short description\n", + "isThisCourseForYou": "This is supposed to be a long description", + "whatWillSetYouApart": "New ways to learn", "videoURL": "", "lcfURL": "d3d3LmV4YW1wbGUuY29tL2xlYWQtY2FwdHVyZT9pZD0xMjM=", "logoUrl": "aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc=g", @@ -65,7 +71,7 @@ class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase): "startDate": "2022-03-06", "regCloseDate": "2022-02-06", "finalRegCloseDate": "2022-02-15", - "enterprisePriceUsd": "333.3" + "enterprisePriceUsd": "333.3", }, "curriculum": { "heading": "Course curriculum", @@ -74,41 +80,42 @@ class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase): { "module_number": 0, "heading": "Module 0", - "description": "Welcome to your course" + "description": "Welcome to your course", }, { "module_number": 1, "heading": "Module 1", - "description": "Welcome to Module 1" + "description": "Welcome to Module 1", }, - ] + ], }, "testimonials": [ { "name": "Lorem Ipsum", "title": "Gibberish", - "text": " This is a good course" + "text": " This is a good course", }, ], "faqs": [ { "id": "faq-1", "headline": "FAQ 1", - "blurb": "This should answer it" + "blurb": "This should answer it", } ], "certificate": { "headline": "About the certificate", - "blurb": "how this makes you special" + "blurb": "how this makes you special", }, "stats": { "stat1": "90%", "stat1Blurb": "

A vast number of special beings take this course

", "stat2": "100 million", - "stat2Blurb": "

VC fund

" - } + "stat2Blurb": "

VC fund

", + }, }, - ]} + ] + } variant_1 = { "id": "00000000-0000-0000-0000-000000000000", @@ -122,7 +129,7 @@ class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase): "endDate": "2024-04-28", "finalRegCloseDate": "2024-03-26", "websiteVisibility": "private", - "enterprisePriceUsd": 3510.0 + "enterprisePriceUsd": 3510.0, } variant_2 = { @@ -140,8 +147,15 @@ class TestPopulateExecutiveEducationDataCsv(CSVLoaderMixin, TestCase): } SUCCESS_API_RESPONSE_V2 = copy.deepcopy(SUCCESS_API_RESPONSE) - SUCCESS_API_RESPONSE_V2['products'][0].pop('variant') - SUCCESS_API_RESPONSE_V2["products"][0].update({"variants": [variant_1, variant_2,]}) + SUCCESS_API_RESPONSE_V2["products"][0].pop("variant") + SUCCESS_API_RESPONSE_V2["products"][0].update( + { + "variants": [ + variant_1, + variant_2, + ] + } + ) SUCCESS_API_RESPONSE_V2["products"][0].update({"edxTaxiFormId": None}) def mock_product_api_call(self, override_product_api_response=None): @@ -153,12 +167,14 @@ def mock_product_api_call(self, override_product_api_response=None): api_response = override_product_api_response responses.add( responses.GET, - settings.PRODUCT_API_URL + '/?detail=2', + settings.PRODUCT_API_URL + "/?detail=2", body=json.dumps(api_response), status=200, ) - def mock_get_smarter_client_response(self, override_get_smarter_client_response=None): + def mock_get_smarter_client_response( + self, override_get_smarter_client_response=None + ): """ Mock get_smarter_client response with success response. """ @@ -166,33 +182,49 @@ def mock_get_smarter_client_response(self, override_get_smarter_client_response= return override_get_smarter_client_response return self.SUCCESS_API_RESPONSE - @mock.patch('course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient') - def test_successful_file_data_population_with_getsmarter_flag(self, mock_get_smarter_client): + @mock.patch( + "course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient" + ) + def test_successful_file_data_population_with_getsmarter_flag( + self, mock_get_smarter_client + ): """ Verify the successful population has data from API response if getsmarter flag is provided. """ - mock_get_smarter_client.return_value.request.return_value.json.return_value = self.mock_get_smarter_client_response() # pylint: disable=line-too-long + mock_get_smarter_client.return_value.request.return_value.json.return_value = ( + self.mock_get_smarter_client_response() + ) # pylint: disable=line-too-long with LogCapture(LOGGER_PATH) as log_capture: - output_csv = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + output_csv = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--output_csv', output_csv.name, - '--use_getsmarter_api_client', True, + "populate_executive_education_data_csv", + "--output_csv", + output_csv.name, + "--use_getsmarter_api_client", + True, ) output_csv.seek(0) - reader = csv.DictReader(open(output_csv.name, 'r')) # lint-amnesty, pylint: disable=consider-using-with + reader = csv.DictReader( + open(output_csv.name, "r") + ) # lint-amnesty, pylint: disable=consider-using-with data_row = next(reader) self._assert_api_response(data_row) log_capture.check_present( ( LOGGER_PATH, - 'INFO', - 'Data population and transformation completed for CSV row title CSV Course' + "INFO", + "Data population and transformation completed for CSV row title CSV Course", ), ) - @mock.patch('course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient') - def test_skip_products_ingestion_if_variants_data_empty(self, mock_get_smarter_client): + @mock.patch( + "course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient" + ) + def test_skip_products_ingestion_if_variants_data_empty( + self, mock_get_smarter_client + ): """ Verify that the command skips the product ingestion if the variants data is empty """ @@ -222,127 +254,124 @@ def test_skip_products_ingestion_if_variants_data_empty(self, mock_get_smarter_c ) output_csv.seek(0) - with open(output_csv.name, 'r') as csv_file: + with open(output_csv.name, "r") as csv_file: reader = csv.DictReader(csv_file) # check for empty csv file assert not any(reader) - @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): + @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 + ): """ Verify the successful population has data from API response if getsmarter flag is provided and the product can have multiple variants """ mock_get_smarter_client.return_value.request.return_value.json.return_value = ( - self.mock_get_smarter_client_response(override_get_smarter_client_response=self.SUCCESS_API_RESPONSE_V2) + self.mock_get_smarter_client_response( + override_get_smarter_client_response=self.SUCCESS_API_RESPONSE_V2 + ) ) with NamedTemporaryFile() as output_csv: with LogCapture(LOGGER_PATH) as log_capture: call_command( - 'populate_executive_education_data_csv', - '--output_csv', output_csv.name, - '--use_getsmarter_api_client', True, + "populate_executive_education_data_csv", + "--output_csv", + output_csv.name, + "--use_getsmarter_api_client", + True, ) output_csv.seek(0) - with open(output_csv.name, 'r') as csv_file: + with open(output_csv.name, "r") as csv_file: reader = csv.DictReader(csv_file) data_row = next(reader) - assert data_row['Variant Id'] == self.variant_1['id'] - assert data_row['Start Time'] == '00:00:00' - assert data_row['Start Date'] == self.variant_1['startDate'] - assert data_row['End Time'] == '00:00:00' - assert data_row['End Date'] == self.variant_1['endDate'] - assert data_row['Reg Close Date'] == self.variant_1['finalRegCloseDate'] - assert data_row['Reg Close Time'] == '00:00:00' - assert data_row['Verified Price'] == str(self.variant_1['finalPrice']) - assert data_row['Restriction Type'] == 'custom-b2b-enterprise' - assert data_row['Fixed Price Usd'] == '3510.0' - assert data_row['Taxi Form Id'] == '' - assert data_row['Post Submit Url'] == 'https://www.getsmarter.com/blog/career-advice' + assert data_row["Variant Id"] == self.variant_1["id"] + assert data_row["Start Time"] == "00:00:00" + assert data_row["Start Date"] == self.variant_1["startDate"] + assert data_row["End Time"] == "00:00:00" + assert data_row["End Date"] == self.variant_1["endDate"] + assert data_row["Reg Close Date"] == self.variant_1["finalRegCloseDate"] + assert data_row["Reg Close Time"] == "00:00:00" + assert data_row["Verified Price"] == str(self.variant_1["finalPrice"]) + assert data_row["Restriction Type"] == "custom-b2b-enterprise" + assert data_row["Fixed Price Usd"] == "3510.0" + assert data_row["Taxi Form Id"] == "" + assert ( + data_row["Post Submit Url"] + == "https://www.getsmarter.com/blog/career-advice" + ) data_row = next(reader) - assert data_row['Variant Id'] == self.variant_2['id'] - assert data_row['Start Time'] == '00:00:00' - assert data_row['Start Date'] == self.variant_2['startDate'] - assert data_row['End Time'] == '00:00:00' - assert data_row['End Date'] == self.variant_2['endDate'] - assert data_row['Reg Close Date'] == self.variant_2['finalRegCloseDate'] - assert data_row['Reg Close Time'] == '00:00:00' - assert data_row['Verified Price'] == str(self.variant_2['finalPrice']) - assert data_row['Restriction Type'] == 'None' - assert data_row['Fixed Price Usd'] == '' - assert data_row['Taxi Form Id'] == '' - assert data_row['Post Submit Url'] == 'https://www.getsmarter.com/blog/career-advice' + assert data_row["Variant Id"] == self.variant_2["id"] + assert data_row["Start Time"] == "00:00:00" + assert data_row["Start Date"] == self.variant_2["startDate"] + assert data_row["End Time"] == "00:00:00" + assert data_row["End Date"] == self.variant_2["endDate"] + assert data_row["Reg Close Date"] == self.variant_2["finalRegCloseDate"] + assert data_row["Reg Close Time"] == "00:00:00" + assert data_row["Verified Price"] == str(self.variant_2["finalPrice"]) + assert data_row["Restriction Type"] == "None" + assert data_row["Fixed Price Usd"] == "" + assert data_row["Taxi Form Id"] == "" + assert ( + data_row["Post Submit Url"] + == "https://www.getsmarter.com/blog/career-advice" + ) log_capture.check_present( ( LOGGER_PATH, - 'INFO', - 'Data population and transformation completed for CSV row title CSV Course' + "INFO", + "Data population and transformation completed for CSV row title CSV Course", ), ) - @mock.patch('course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient') - def test_successful_file_data_population_with_getsmarter_flag_with_future_variants(self, mock_get_smarter_client): + @mock.patch( + "course_discovery.apps.course_metadata.utils.GetSmarterEnterpriseApiClient" + ) + @data(("active", "scheduled", str(date.today().isoformat()), "2024-03-20")) + @unpack + def test_successful_file_data_population_with_getsmarter_flag_with_future_variants( + self, + variant_1_status, + variant_2_status, + expected_variant_1_publish_date, + expected_variant_2_publish_date, + 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 + the product can have variants with future dates """ success_api_response = copy.deepcopy(self.SUCCESS_API_RESPONSE_V2) - success_api_response['products'][0]['variants'][0]['status'] = 'scheduled' - success_api_response['products'][0]['variants'][1]['status'] = 'active' + success_api_response["products"][0]["variants"][0]["status"] = variant_1_status + success_api_response["products"][0]["variants"][1]["status"] = variant_2_status + 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) + self.mock_get_smarter_client_response( + override_get_smarter_client_response=success_api_response + ) ) + with NamedTemporaryFile() as output_csv: - with LogCapture(LOGGER_PATH) as log_capture: - call_command( - 'populate_executive_education_data_csv', - '--output_csv', output_csv.name, - '--use_getsmarter_api_client', True, - ) + call_command( + 'populate_executive_education_data_csv', + '--output_csv', output_csv.name, + '--use_getsmarter_api_client', True, + ) output_csv.seek(0) with open(output_csv.name, 'r') as csv_file: reader = csv.DictReader(csv_file) - data_row = next(reader) - assert data_row['Variant Id'] == self.variant_1['id'] - assert data_row['Publish Date'] == self.variant_1['startDate'] - assert data_row['Start Time'] == '00:00:00' - assert data_row['Start Date'] == self.variant_1['startDate'] - assert data_row['End Time'] == '00:00:00' - assert data_row['End Date'] == self.variant_1['endDate'] - assert data_row['Reg Close Date'] == self.variant_1['finalRegCloseDate'] - assert data_row['Reg Close Time'] == '00:00:00' - assert data_row['Verified Price'] == str(self.variant_1['finalPrice']) - assert data_row['Restriction Type'] == 'custom-b2b-enterprise' - assert data_row['Fixed Price Usd'] == '3510.0' - assert data_row['Taxi Form Id'] == '' - assert data_row['Post Submit Url'] == 'https://www.getsmarter.com/blog/career-advice' data_row = next(reader) - assert data_row['Variant Id'] == self.variant_2['id'] - assert data_row['Publish Date'] == str(date.today().isoformat()) - assert data_row['Start Time'] == '00:00:00' - assert data_row['Start Date'] == self.variant_2['startDate'] - assert data_row['End Time'] == '00:00:00' - assert data_row['End Date'] == self.variant_2['endDate'] - assert data_row['Reg Close Date'] == self.variant_2['finalRegCloseDate'] - assert data_row['Reg Close Time'] == '00:00:00' - assert data_row['Verified Price'] == str(self.variant_2['finalPrice']) - assert data_row['Restriction Type'] == 'None' - assert data_row['Fixed Price Usd'] == '' - assert data_row['Taxi Form Id'] == '' - assert data_row['Post Submit Url'] == 'https://www.getsmarter.com/blog/career-advice' + assert data_row['Publish Date'] == expected_variant_1_publish_date - log_capture.check_present( - ( - LOGGER_PATH, - 'INFO', - 'Data population and transformation completed for CSV row title CSV Course' - ), - ) + data_row = next(reader) + assert data_row['Publish Date'] == expected_variant_2_publish_date @responses.activate def test_successful_file_data_population_with_input_csv(self): @@ -352,57 +381,86 @@ def test_successful_file_data_population_with_input_csv(self): self.mock_product_api_call() with NamedTemporaryFile() as input_csv: - input_csv = self._write_csv(input_csv, [mock_data.VALID_COURSE_AND_COURSE_RUN_CSV_DICT]) + input_csv = self._write_csv( + input_csv, [mock_data.VALID_COURSE_AND_COURSE_RUN_CSV_DICT] + ) with LogCapture(LOGGER_PATH) as log_capture: - output_csv = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + output_csv = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--input_csv', input_csv.name, - '--output_csv', output_csv.name, - '--auth_token', self.AUTH_TOKEN + "populate_executive_education_data_csv", + "--input_csv", + input_csv.name, + "--output_csv", + output_csv.name, + "--auth_token", + self.AUTH_TOKEN, ) output_csv.seek(0) - reader = csv.DictReader(open(output_csv.name, 'r')) # lint-amnesty, pylint: disable=consider-using-with + reader = csv.DictReader( + open(output_csv.name, "r") + ) # lint-amnesty, pylint: disable=consider-using-with data_row = next(reader) # Asserting certain data items to verify that both CSV and API # responses are present in the final CSV - assert data_row['Organization Short Code Override'] == 'altEdx' - assert data_row['External Identifier'] == '12345678' - assert data_row['Start Time'] == '00:00:00' - assert data_row['Short Description'] == 'A short description for CSV course' - assert data_row['Long Description'] == 'Very short description\n' \ - 'This is supposed to be a long description' - assert data_row['End Time'] == '00:00:00' - assert data_row['Reg Close Date'] == '01/25/2050' - assert data_row['Reg Close Time'] == '00:00:00' - assert data_row['Course Enrollment Track'] == 'Executive Education(2U)' - assert data_row['Course Run Enrollment Track'] == 'Unpaid Executive Education' - assert data_row['Length'] == '10' - assert data_row['Number'] == 'TC' - assert data_row['Redirect Url'] == 'https://example.com/' - assert data_row['Organic Url'] == 'https://example.com/' - assert data_row['Image'] == 'https://example.com/image.jpg' - assert data_row['Organization Logo Override'] == 'https://example.com/image.jpg' - assert data_row['Course Level'] == 'Introductory' - assert data_row['Course Pacing'] == 'Instructor-Paced' - assert data_row['Content Language'] == 'Spanish - Spain (Modern)' - assert data_row['Transcript Language'] == 'Spanish - Spain (Modern)' - assert data_row['Primary Subject'] == 'Design and Marketing' - assert data_row['Frequently Asked Questions'] == '

FAQ 1

This should answer it
' - assert data_row['Syllabus'] == '

Test Curriculum

Module 0: Welcome to your course' \ - '

Module 1: Welcome to Module 1

' - assert data_row['Learner Testimonials'] == '

" This is a good course"

-Lorem ' \ - 'Ipsum (Gibberish)

' - assert str(date.today().year) in data_row['Publish Date'] - assert data_row['Restriction Type'] == 'None' + assert data_row["Organization Short Code Override"] == "altEdx" + assert data_row["External Identifier"] == "12345678" + assert data_row["Start Time"] == "00:00:00" + assert ( + data_row["Short Description"] + == "A short description for CSV course" + ) + assert ( + data_row["Long Description"] == "Very short description\n" + "This is supposed to be a long description" + ) + assert data_row["End Time"] == "00:00:00" + assert data_row["Reg Close Date"] == "01/25/2050" + assert data_row["Reg Close Time"] == "00:00:00" + assert data_row["Course Enrollment Track"] == "Executive Education(2U)" + assert ( + data_row["Course Run Enrollment Track"] + == "Unpaid Executive Education" + ) + assert data_row["Length"] == "10" + assert data_row["Number"] == "TC" + assert data_row["Redirect Url"] == "https://example.com/" + assert data_row["Organic Url"] == "https://example.com/" + assert data_row["Image"] == "https://example.com/image.jpg" + assert ( + data_row["Organization Logo Override"] + == "https://example.com/image.jpg" + ) + assert data_row["Course Level"] == "Introductory" + assert data_row["Course Pacing"] == "Instructor-Paced" + assert data_row["Content Language"] == "Spanish - Spain (Modern)" + assert data_row["Transcript Language"] == "Spanish - Spain (Modern)" + assert data_row["Primary Subject"] == "Design and Marketing" + assert ( + data_row["Frequently Asked Questions"] + == "

FAQ 1

This should answer it
" + ) + assert ( + data_row["Syllabus"] + == "

Test Curriculum

Module 0: Welcome to your course" + "

Module 1: Welcome to Module 1

" + ) + assert ( + data_row["Learner Testimonials"] + == '

" This is a good course"

-Lorem ' + "Ipsum (Gibberish)

" + ) + assert str(date.today().year) in data_row["Publish Date"] + assert data_row["Restriction Type"] == "None" log_capture.check_present( ( LOGGER_PATH, - 'INFO', - 'Data population and transformation completed for CSV row title CSV Course' + "INFO", + "Data population and transformation completed for CSV row title CSV Course", ), ) @@ -414,14 +472,20 @@ def test_successful_file_data_population_without_input_csv(self): self.mock_product_api_call() with LogCapture(LOGGER_PATH) as log_capture: - output_csv = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + output_csv = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--output_csv', output_csv.name, - '--auth_token', self.AUTH_TOKEN + "populate_executive_education_data_csv", + "--output_csv", + output_csv.name, + "--auth_token", + self.AUTH_TOKEN, ) output_csv.seek(0) - reader = csv.DictReader(open(output_csv.name, 'r')) # lint-amnesty, pylint: disable=consider-using-with + reader = csv.DictReader( + open(output_csv.name, "r") + ) # lint-amnesty, pylint: disable=consider-using-with data_row = next(reader) self._assert_api_response(data_row) @@ -429,8 +493,8 @@ def test_successful_file_data_population_without_input_csv(self): log_capture.check_present( ( LOGGER_PATH, - 'INFO', - 'Data population and transformation completed for CSV row title CSV Course' + "INFO", + "Data population and transformation completed for CSV row title CSV Course", ), ) @@ -443,22 +507,29 @@ def test_successful_file_data_population_input_csv_no_product_info(self): self.mock_product_api_call() mismatched_product = { **mock_data.VALID_COURSE_AND_COURSE_RUN_CSV_DICT, - 'title': 'Not present in CSV' + "title": "Not present in CSV", } with NamedTemporaryFile() as input_csv: input_csv = self._write_csv(input_csv, [mismatched_product]) with LogCapture(LOGGER_PATH) as log_capture: - output_csv = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + output_csv = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--input_csv', input_csv.name, - '--output_csv', output_csv.name, - '--auth_token', self.AUTH_TOKEN + "populate_executive_education_data_csv", + "--input_csv", + input_csv.name, + "--output_csv", + output_csv.name, + "--auth_token", + self.AUTH_TOKEN, ) output_csv.seek(0) - reader = csv.DictReader(open(output_csv.name, 'r')) # lint-amnesty, pylint: disable=consider-using-with + reader = csv.DictReader( + open(output_csv.name, "r") + ) # lint-amnesty, pylint: disable=consider-using-with data_row = next(reader) self._assert_api_response(data_row) @@ -466,13 +537,13 @@ def test_successful_file_data_population_input_csv_no_product_info(self): log_capture.check_present( ( LOGGER_PATH, - 'INFO', - 'Data population and transformation completed for CSV row title CSV Course' + "INFO", + "Data population and transformation completed for CSV row title CSV Course", ), ( LOGGER_PATH, - 'WARNING', - '[MISSING PRODUCT IN CSV] Unable to find product details for product CSV Course in CSV' + "WARNING", + "[MISSING PRODUCT IN CSV] Unable to find product details for product CSV Course in CSV", ), ) @@ -481,14 +552,19 @@ def test_invalid_csv_path(self): Test that the command raises CommandError if an invalid csv path is provided. """ with self.assertRaisesMessage( - CommandError, 'Error opening csv file at path /tmp/invalid_csv.csv' + CommandError, "Error opening csv file at path /tmp/invalid_csv.csv" ): - output_csv = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + output_csv = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--input_csv', '/tmp/invalid_csv.csv', - '--output_csv', output_csv.name, - '--auth_token', self.AUTH_TOKEN + "populate_executive_education_data_csv", + "--input_csv", + "/tmp/invalid_csv.csv", + "--output_csv", + output_csv.name, + "--auth_token", + self.AUTH_TOKEN, ) def test_missing_json_and_auth_token(self): @@ -496,13 +572,16 @@ def test_missing_json_and_auth_token(self): Test that the command raises CommandError if both auth token and input JSON are missing. """ with self.assertRaisesMessage( - CommandError, - 'auth_token or dev_input_json or getsmarter_flag should be provided to perform data transformation.' + CommandError, + "auth_token or dev_input_json or getsmarter_flag should be provided to perform data transformation.", ): - output_csv = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + output_csv = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--output_csv', output_csv.name, + "populate_executive_education_data_csv", + "--output_csv", + output_csv.name, ) @responses.activate @@ -512,18 +591,23 @@ def test_product_api_call_failure(self): """ responses.add( responses.GET, - settings.PRODUCT_API_URL + '/?detail=2', + settings.PRODUCT_API_URL + "/?detail=2", status=400, ) with self.assertRaisesMessage( - CommandError, 'Unexpected error occurred while fetching products' + CommandError, "Unexpected error occurred while fetching products" ): - csv_file = NamedTemporaryFile() # lint-amnesty, pylint: disable=consider-using-with + csv_file = ( + NamedTemporaryFile() + ) # lint-amnesty, pylint: disable=consider-using-with call_command( - 'populate_executive_education_data_csv', - '--input_csv', csv_file.name, - '--output_csv', csv_file.name, - '--auth_token', self.AUTH_TOKEN + "populate_executive_education_data_csv", + "--input_csv", + csv_file.name, + "--output_csv", + csv_file.name, + "--auth_token", + self.AUTH_TOKEN, ) def _assert_api_response(self, data_row): @@ -531,57 +615,76 @@ def _assert_api_response(self, data_row): Assert the default API response in output CSV dict. """ # pylint: disable=too-many-statements - assert data_row['Organization Short Code Override'] == 'altEdx' - assert data_row['2U Organization Code'] == 'edX' - assert data_row['Number'] == 'TC' - assert data_row['Alternate Number'] == 'UCT' - assert data_row['Title'] == 'Alternative CSV Course' - assert data_row['2U Title'] == 'CSV Course' - assert data_row['Edx Title'] == 'Alternative CSV Course' - assert data_row['2U Primary Subject'] == 'Marketing' - assert data_row['Primary Subject'] == 'Design and Marketing' - assert data_row['Subject Subcategory'] == 'Marketing, Sales, and Techniques' - assert data_row['External Identifier'] == '12345678' - assert data_row['Start Time'] == '00:00:00' - assert data_row['Start Date'] == '2022-03-06' - assert data_row['End Time'] == '00:00:00' - assert data_row['End Date'] == '2022-05-06' - assert data_row['Reg Close Date'] == '2022-02-15' - assert data_row['Reg Close Time'] == '00:00:00' - assert data_row['Verified Price'] == '1998' - assert data_row['Short Description'] == 'A short description for CSV course' - assert data_row['Long Description'] == 'Very short description\n' \ - 'This is supposed to be a long description' - assert data_row['Course Enrollment Track'] == 'Executive Education(2U)' - assert data_row['Course Run Enrollment Track'] == 'Unpaid Executive Education' - assert data_row['Lead Capture Form Url'] == "www.example.com/lead-capture?id=123" - assert data_row['Certificate Header'] == "About the certificate" - assert data_row['Certificate Text'] == 'how this makes you special' - assert data_row['Stat1'] == '90%' - assert data_row['Stat1 Text'] == '

A vast number of special beings take this course

' - assert data_row['Stat2'] == '100 million' - assert data_row['Stat2 Text'] == '

VC fund

' - assert data_row['Length'] == '10' - assert data_row['Redirect Url'] == 'https://example.com/' - assert data_row['Organic Url'] == 'https://example.com/' - assert data_row['Image'] == 'https://example.com/image.jpg' - assert data_row['Course Level'] == 'Introductory' - assert data_row['Course Pacing'] == 'Instructor-Paced' - assert data_row['Content Language'] == 'Spanish - Spain (Modern)' - assert data_row['Transcript Language'] == 'Spanish - Spain (Modern)' - - assert data_row['Frequently Asked Questions'] == '

FAQ 1

This should answer it
' - assert data_row['Syllabus'] == '

Test Curriculum

Module 0: Welcome to your course' \ - '

Module 1: Welcome to Module 1

' - assert data_row['Learner Testimonials'] == '

" This is a good course"

-Lorem ' \ - 'Ipsum (Gibberish)

' - assert str(date.today().year) in data_row['Publish Date'] - assert data_row['Variant Id'] == '00000000-0000-0000-0000-000000000000' - assert data_row['Meta Title'] == 'SEO Title' - assert data_row['Meta Description'] == 'SEO Description' - assert data_row['Meta Keywords'] == 'Keyword 1, Keyword 2' - assert data_row['Slug'] == 'csv-course-slug' - assert data_row['External Course Marketing Type'] == "short_course" - assert data_row['Fixed Price Usd'] == "333.3" - assert data_row['Taxi Form Id'] == 'test-form-id' - assert data_row['Post Submit Url'] == 'https://www.getsmarter.com/blog/career-advice' + assert data_row["Organization Short Code Override"] == "altEdx" + assert data_row["2U Organization Code"] == "edX" + assert data_row["Number"] == "TC" + assert data_row["Alternate Number"] == "UCT" + assert data_row["Title"] == "Alternative CSV Course" + assert data_row["2U Title"] == "CSV Course" + assert data_row["Edx Title"] == "Alternative CSV Course" + assert data_row["2U Primary Subject"] == "Marketing" + assert data_row["Primary Subject"] == "Design and Marketing" + assert data_row["Subject Subcategory"] == "Marketing, Sales, and Techniques" + assert data_row["External Identifier"] == "12345678" + assert data_row["Start Time"] == "00:00:00" + assert data_row["Start Date"] == "2022-03-06" + assert data_row["End Time"] == "00:00:00" + assert data_row["End Date"] == "2022-05-06" + assert data_row["Reg Close Date"] == "2022-02-15" + assert data_row["Reg Close Time"] == "00:00:00" + assert data_row["Verified Price"] == "1998" + assert data_row["Short Description"] == "A short description for CSV course" + assert ( + data_row["Long Description"] == "Very short description\n" + "This is supposed to be a long description" + ) + assert data_row["Course Enrollment Track"] == "Executive Education(2U)" + assert data_row["Course Run Enrollment Track"] == "Unpaid Executive Education" + assert ( + data_row["Lead Capture Form Url"] == "www.example.com/lead-capture?id=123" + ) + assert data_row["Certificate Header"] == "About the certificate" + assert data_row["Certificate Text"] == "how this makes you special" + assert data_row["Stat1"] == "90%" + assert ( + data_row["Stat1 Text"] + == "

A vast number of special beings take this course

" + ) + assert data_row["Stat2"] == "100 million" + assert data_row["Stat2 Text"] == "

VC fund

" + assert data_row["Length"] == "10" + assert data_row["Redirect Url"] == "https://example.com/" + assert data_row["Organic Url"] == "https://example.com/" + assert data_row["Image"] == "https://example.com/image.jpg" + assert data_row["Course Level"] == "Introductory" + assert data_row["Course Pacing"] == "Instructor-Paced" + assert data_row["Content Language"] == "Spanish - Spain (Modern)" + assert data_row["Transcript Language"] == "Spanish - Spain (Modern)" + + assert ( + data_row["Frequently Asked Questions"] + == "

FAQ 1

This should answer it
" + ) + assert ( + data_row["Syllabus"] + == "

Test Curriculum

Module 0: Welcome to your course" + "

Module 1: Welcome to Module 1

" + ) + assert ( + data_row["Learner Testimonials"] + == '

" This is a good course"

-Lorem ' + "Ipsum (Gibberish)

" + ) + assert str(date.today().year) in data_row["Publish Date"] + assert data_row["Variant Id"] == "00000000-0000-0000-0000-000000000000" + assert data_row["Meta Title"] == "SEO Title" + assert data_row["Meta Description"] == "SEO Description" + assert data_row["Meta Keywords"] == "Keyword 1, Keyword 2" + assert data_row["Slug"] == "csv-course-slug" + assert data_row["External Course Marketing Type"] == "short_course" + assert data_row["Fixed Price Usd"] == "333.3" + assert data_row["Taxi Form Id"] == "test-form-id" + assert ( + data_row["Post Submit Url"] + == "https://www.getsmarter.com/blog/career-advice" + )