'Tests that the following scenario will not cause an unhandled exception:
- Multiple concurrent requests are made for the same non-existent CourseOverview.
- A race condition in the django ORM\'s save method that checks for the presence
of the primary key performs an Insert instead of an Update operation.
- An IntegrityError is raised when attempting to create duplicate entries.
- This should be handled gracefully in CourseOverview.get_from_id.
Created in response to https://openedx.atlassian.net/browse/MA-1061.'

def test_course_overview_saving_race_condition(self):

    course = CourseFactory.create()
    with mock.patch('openedx.core.djangoapps.content.course_overviews.models.CourseOverview.objects.get') as mock_getter:
        mock_getter.side_effect = CourseOverview.DoesNotExist
        with mock.patch('openedx.core.djangoapps.content.course_overviews.models.CourseOverview._get_pk_val') as mock_get_pk_val:
            mock_get_pk_val.return_value = None
            with mock.patch('django.db.models.Field.get_pk_value_on_save') as mock_get_pk_value_on_save:
                mock_get_pk_value_on_save.return_value = None
                with mock.patch('openedx.core.djangoapps.content.course_overviews.models.CourseOverviewTab.objects.bulk_create') as mock_bulk_create:
                    mock_bulk_create.return_value = None
                    for _ in range(2):
                        self.assertIsInstance(CourseOverview.get_from_id(course.id), CourseOverview)
