Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Aug 28, 2024
1 parent c7b2b60 commit 184e896
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/services/tasks/download_stats_migration_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 184e896

Please sign in to comment.