Skip to content

Commit

Permalink
test: testcases update for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Dec 18, 2024
1 parent 3faf886 commit a526612
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def ingest(self): # pylint: disable=too-many-statements
except Exception as exc: # pylint: disable=broad-except
exception_message = exc
if hasattr(exc, 'response'):
error_message = CSVIngestionErrorMessages.COURSE_UPDATE_ERROR.format(
error_message = CSVIngestionErrorMessages.COURSE_ENTITLEMENT_PRICE_UPDATE_ERROR.format(
course_title=course_title,
exception_message=exception_message
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@

LOGGER_PATH = 'course_discovery.apps.course_metadata.data_loaders.csv_loader'


class MockExceptionWithResponse(Exception):
def __init__(self, response_content):
self.response = mock.Mock(content=response_content)


@ddt
@mock.patch(
'course_discovery.apps.course_metadata.data_loaders.configured_jwt_decode_handler',
Expand Down Expand Up @@ -715,6 +717,81 @@ def test_exception_flow_for_update_course(self, jwt_decode_patch): # pylint: di
assert Course.everything.count() == 1
assert CourseRun.everything.count() == 1

@responses.activate
def test_exception_flow_for_update_course_entitlement_price(self, jwt_decode_patch): # pylint: disable=unused-argument
"""
Verify that the course update fails if an exception is raised while updating the course.
"""
self._setup_prerequisites(self.partner)
self.mock_studio_calls(self.partner)
studio_url = '{root}/api/v1/course_runs/'.format(root=self.partner.studio_url.strip('/'))
responses.add(responses.POST, f'{studio_url}{self.COURSE_RUN_KEY}/rerun/', status=200)
self.mock_studio_calls(self.partner, run_key='course-v1:edx+csv_123+1T2020a')
self.mock_ecommerce_publication(self.partner)
_, _ = self.mock_image_response()

course = CourseFactory(
key=self.COURSE_KEY,
partner=self.partner,
type=self.course_type,
draft=True,
key_for_reruns=''
)

CourseRunFactory(
course=course,
start=datetime.datetime(2014, 3, 1, tzinfo=UTC),
end=datetime.datetime(2040, 3, 1, tzinfo=UTC),
key=self.COURSE_RUN_KEY,
type=self.course_run_type,
status='published',
draft=True,
)

mocked_data = copy.deepcopy(mock_data.VALID_COURSE_AND_COURSE_RUN_CSV_DICT)
mocked_data.update(
{
"publish_date": "01/26/2022",
"start_date": "01/25/2022",
"start_time": "00:00",
"end_date": "02/25/2055",
"end_time": "00:00",
"reg_close_date": "01/25/2055",
"reg_close_time": "00:00",
"variant_id": "11111111-1111-1111-1111-111111111111",
}
)

with NamedTemporaryFile() as csv:
csv = self._write_csv(csv, [mock_data.VALID_COURSE_AND_COURSE_RUN_CSV_DICT, mocked_data])
with override_waffle_switch(IS_COURSE_RUN_VARIANT_ID_EDITABLE, active=True):
with mock.patch.object(
CSVDataLoader, "_call_course_api", self.mock_call_course_api
):
loader = CSVDataLoader(
self.partner, csv_path=csv.name, product_source=self.source.slug
)
# pylint: disable=protected-access
loader._register_ingestion_error = mock.MagicMock()
loader._update_course_entitlement_price = mock.MagicMock()

# pylint: disable=protected-access
loader._update_course_entitlement_price.side_effect = (
MockExceptionWithResponse('Entitlement Price Update Error')
)

with LogCapture(LOGGER_PATH):
loader.ingest()

expected_error_message = CSVIngestionErrorMessages.COURSE_ENTITLEMENT_PRICE_UPDATE_ERROR.format(
course_title=mock_data.VALID_COURSE_AND_COURSE_RUN_CSV_DICT["title"],
exception_message="Entitlement Price Update Error",
)
# pylint: disable=protected-access
loader._register_ingestion_error.assert_called_once_with(
CSVIngestionErrors.COURSE_UPDATE_ERROR, expected_error_message
)

@responses.activate
@mock.patch("course_discovery.apps.course_metadata.data_loaders.csv_loader.reverse")
@mock.patch("course_discovery.apps.course_metadata.data_loaders.csv_loader.settings")
Expand Down

0 comments on commit a526612

Please sign in to comment.