Skip to content

Commit bf394bd

Browse files
committed
update specs
1 parent 5fe5761 commit bf394bd

3 files changed

Lines changed: 93 additions & 10 deletions

File tree

app/controllers/reports/annual_reports_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def recalculate
3333
end
3434

3535
def range
36-
year_start, year_end = range_params[:year_start], range_params[:year_end]
36+
year_start = range_params[:year_start].to_i
37+
year_end = range_params[:year_end].to_i
3738

3839
if year_end < year_start
3940
flash[:error] = "End year must be greater than or equal to start year."

spec/requests/reports/annual_reports_requests_spec.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,19 @@
101101
expect(row_2017["New Field"]).to eq("42")
102102
expect(row_2017["Total Distributed"]).to eq("200")
103103
end
104-
105-
it "uses the earliest(smallest) year between year_start and organization's earliest_reporting_year" do
106-
get range_reports_annual_reports_path(year_start: 2004, year_end: 2008, format: :csv)
107-
# the organization was created in 2006 (created_at_2006)
108-
# so the below years should not be in the output
109-
expect(response.body).not_to include("2004")
110-
expect(response.body).not_to include("2005")
111-
response.body.split("\n")
112-
end
113104
end
114105

115106
context "invalid year ranges given" do
116107
it "should raise a URL error" do
117108
expect { get range_reports_annual_reports_path(year_start: 'test', year_end: 'test', format: :csv) }
118109
.to raise_error(ActionController::UrlGenerationError)
119110
end
111+
112+
it "should redirect and show an error message if end year is less than start year" do
113+
get range_reports_annual_reports_path(year_start: 2018, year_end: 2016, format: :csv)
114+
expect(response).to have_http_status(:found)
115+
expect(flash[:error]).to eq("End year must be greater than or equal to start year.")
116+
end
120117
end
121118
end
122119

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)