From 184e896a0c16bd61f454ad34c60862f60990fbd8 Mon Sep 17 00:00:00 2001 From: David Campbell Date: Wed, 28 Aug 2024 15:44:42 -0400 Subject: [PATCH] tests --- .../download_stats_migration_service_spec.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spec/services/tasks/download_stats_migration_service_spec.rb b/spec/services/tasks/download_stats_migration_service_spec.rb index 90e5f6cf6..04ea74f5d 100644 --- a/spec/services/tasks/download_stats_migration_service_spec.rb +++ b/spec/services/tasks/download_stats_migration_service_spec.rb @@ -134,8 +134,29 @@ service.migrate_to_new_table(output_path) expect(Rails.logger).to have_received(:error).with('An error occurred while migrating work stats: Simulated CSV read failure') end + + context 'if a failure occurs during a private function' do + it 'handles and logs errors from create_hyc_download_stat' do + allow(Rails.logger).to receive(:error) + # Simulate a failure during the creation of a HycDownloadStat object for a specific file_id + allow(HycDownloadStat).to receive(:find_or_initialize_by).and_call_original + allow(HycDownloadStat).to receive(:find_or_initialize_by).with({:date=>"2023-03-01 00:00:00 UTC", :fileset_id=>"file_id_1"}).and_raise(StandardError, 'Simulated database query failure') + service.migrate_to_new_table(output_path) + expect(Rails.logger).to have_received(:error).with(a_string_including('Failed to create HycDownloadStat for')) + end + + it 'handles and logs errors from save_hyc_download_stat' do + allow(Rails.logger).to receive(:error) + # Simulate a failure during the creation of a HycDownloadStat object for a specific file_id + allow(HycDownloadStat).to receive(:find_or_initialize_by).and_call_original + allow(HycDownloadStat).to receive(:find_or_initialize_by).with({:date=>"2023-03-01 00:00:00 UTC", :fileset_id=>"file_id_1"}).and_raise(StandardError, 'Simulated database query failure') + service.migrate_to_new_table(output_path) + expect(Rails.logger).to have_received(:error).with(a_string_including('Failed to create HycDownloadStat for')) + end + end end + private def csv_to_hash_array(file_path) CSV.read(file_path, headers: true).map { |row| row.to_h.symbolize_keys }