|
| 1 | +RSpec.describe Reports::AnnualSurveyReportService, type: :service do |
| 2 | + let(:organization) { create(:organization) } |
| 3 | + |
| 4 | + subject(:service) do |
| 5 | + described_class.new(organization: organization, year_start: year_start, year_end: year_end) |
| 6 | + end |
| 7 | + |
| 8 | + describe '#call' do |
| 9 | + context "with a valid year range" do |
| 10 | + let(:year_start) { 2020 } |
| 11 | + let(:year_end) { 2022 } |
| 12 | + |
| 13 | + let(:report_2020) { instance_double(AnnualReport) } |
| 14 | + let(:report_2021) { instance_double(AnnualReport) } |
| 15 | + let(:report_2022) { instance_double(AnnualReport) } |
| 16 | + |
| 17 | + before do |
| 18 | + allow(Reports).to receive(:retrieve_report) |
| 19 | + .with(hash_including(year: 2020)).and_return(report_2020) |
| 20 | + allow(Reports).to receive(:retrieve_report) |
| 21 | + .with(hash_including(year: 2021)).and_return(report_2021) |
| 22 | + allow(Reports).to receive(:retrieve_report) |
| 23 | + .with(hash_including(year: 2022)).and_return(report_2022) |
| 24 | + end |
| 25 | + |
| 26 | + it "returns one report per year in the range" do |
| 27 | + expect(service.call).to eq([report_2020, report_2021, report_2022]) |
| 28 | + end |
| 29 | + |
| 30 | + it "calls retrieve_report with recalculate: true for each year" do |
| 31 | + service.call |
| 32 | + expect(Reports).to have_received(:retrieve_report) |
| 33 | + .with(hash_including(organization: organization, year: 2020, recalculate: true)) |
| 34 | + expect(Reports).to have_received(:retrieve_report) |
| 35 | + .with(hash_including(organization: organization, year: 2021, recalculate: true)) |
| 36 | + expect(Reports).to have_received(:retrieve_report) |
| 37 | + .with(hash_including(organization: organization, year: 2022, recalculate: true)) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context "with a single year range" do |
| 42 | + let(:year_start) { 2021 } |
| 43 | + let(:year_end) { 2021 } |
| 44 | + |
| 45 | + let(:report_2021) { instance_double(AnnualReport) } |
| 46 | + |
| 47 | + before do |
| 48 | + allow(Reports).to receive(:retrieve_report) |
| 49 | + .with(hash_including(year: 2021)).and_return(report_2021) |
| 50 | + end |
| 51 | + |
| 52 | + it "returns a single report" do |
| 53 | + expect(service.call).to eq([report_2021]) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context "when a year's report raises ActiveRecord::RecordInvalid" do |
| 58 | + let(:year_start) { 2020 } |
| 59 | + let(:year_end) { 2022 } |
| 60 | + |
| 61 | + let(:report_2020) { instance_double(AnnualReport) } |
| 62 | + let(:report_2022) { instance_double(AnnualReport) } |
| 63 | + |
| 64 | + before do |
| 65 | + allow(Reports).to receive(:retrieve_report) |
| 66 | + .with(hash_including(year: 2020)).and_return(report_2020) |
| 67 | + allow(Reports).to receive(:retrieve_report) |
| 68 | + .with(hash_including(year: 2021)).and_raise(ActiveRecord::RecordInvalid) |
| 69 | + allow(Reports).to receive(:retrieve_report) |
| 70 | + .with(hash_including(year: 2022)).and_return(report_2022) |
| 71 | + end |
| 72 | + |
| 73 | + it "skips the failed year and returns the remaining reports" do |
| 74 | + expect(service.call).to eq([report_2020, report_2022]) |
| 75 | + end |
| 76 | + |
| 77 | + it "logs the error" do |
| 78 | + allow(Rails.logger).to receive(:error) |
| 79 | + service.call |
| 80 | + expect(Rails.logger).to have_received(:error).with(/Failed to retrieve annual report for year 2021/) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | + |
0 commit comments