def gdrive_sync(self, request, **kwargs): # pylint:disable=unused-argument """ Trigger a task to sync all non-video Google Drive files""" website = Website.objects.get( name=self.kwargs.get("parent_lookup_website")) website.sync_status = WebsiteSyncStatus.PENDING website.save() import_website_files.delay(website.name) return Response(status=200)
def test_import_website_files_query_error(mocker): """import_website_files should run process_file_result for each drive file and trigger tasks""" mocker.patch("gdrive_sync.tasks.api.is_gdrive_enabled", return_value=True) mock_log = mocker.patch("gdrive_sync.tasks.log.exception") mocker.patch( "gdrive_sync.tasks.api.query_files", side_effect=Exception("Error querying google drive"), ) website = WebsiteFactory.create() import_website_files.delay(website.name) sync_errors = [] for folder in [DRIVE_FOLDER_VIDEOS_FINAL, DRIVE_FOLDER_FILES_FINAL]: sync_error = ( f"An error occurred when querying the {folder} google drive subfolder" ) sync_errors.append(sync_error) mock_log.assert_any_call("%s for %s", sync_error, website.short_id) website.refresh_from_db() assert website.sync_status == WebsiteSyncStatus.FAILED assert sorted(website.sync_errors) == sorted(sync_errors)
def test_import_website_files(mocker, mocked_celery, mock_gdrive_files): # pylint:disable=unused-argument """import_website_files should run process_file_result for each drive file and trigger tasks""" mocker.patch("gdrive_sync.tasks.api.is_gdrive_enabled", return_value=True) website = WebsiteFactory.create() drive_files = DriveFileFactory.create_batch(2, website=website) mock_process_file_result = mocker.patch( "gdrive_sync.tasks.api.process_file_result", side_effect=drive_files) mock_process_gdrive_file = mocker.patch( "gdrive_sync.tasks.process_drive_file.s") mock_sync_content = mocker.patch( "gdrive_sync.tasks.sync_website_content.si") mock_update_status = mocker.patch( "gdrive_sync.tasks.update_website_status.si") with pytest.raises(mocked_celery.replace_exception_class): import_website_files.delay(website.name) assert mock_process_file_result.call_count == 2 for drive_file in drive_files: mock_process_gdrive_file.assert_any_call(drive_file.file_id) mock_sync_content.assert_called_once_with(website.name) website.refresh_from_db() mock_update_status.assert_called_once_with(website.pk, website.synced_on)
def test_import_website_files_processing_error(mocker, mock_gdrive_files): # pylint:disable=unused-argument """import_website_files should log exceptions raised by process_file_result and update website status""" mocker.patch("gdrive_sync.tasks.api.is_gdrive_enabled", return_value=True) mock_log = mocker.patch("gdrive_sync.tasks.log.exception") mocker.patch( "gdrive_sync.tasks.api.process_file_result", side_effect=Exception("Error processing the file"), ) website = WebsiteFactory.create() import_website_files.delay(website.name) sync_errors = [] for gdfile in LIST_FILE_RESPONSES[0]["files"]: sync_errors.append( f"Error processing gdrive file {gdfile.get('name')}") mock_log.assert_any_call( "Error processing gdrive file %s for %s", gdfile.get("name"), website.short_id, ) website.refresh_from_db() assert website.sync_status == WebsiteSyncStatus.FAILED assert sorted(website.sync_errors) == sorted(sync_errors)
def test_import_website_files_missing_folder(mocker): """import_website_files should run process_file_result for each drive file and trigger tasks""" mocker.patch("gdrive_sync.tasks.api.is_gdrive_enabled", return_value=True) website = WebsiteFactory.create() mock_log = mocker.patch("gdrive_sync.tasks.log.error") mocker.patch( "gdrive_sync.tasks.api.query_files", side_effect=[ [], [], ], ) import_website_files.delay(website.name) for folder in [DRIVE_FOLDER_VIDEOS_FINAL, DRIVE_FOLDER_FILES_FINAL]: mock_log.assert_any_call("%s for %s", f"Could not find drive subfolder {folder}", website.short_id) website.refresh_from_db() assert website.sync_status == WebsiteSyncStatus.FAILED assert sorted(website.sync_errors) == sorted([ f"Could not find drive subfolder {DRIVE_FOLDER_VIDEOS_FINAL}", f"Could not find drive subfolder {DRIVE_FOLDER_FILES_FINAL}", ])