-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffd6d14
commit adb8fbf
Showing
9 changed files
with
217 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Report | ||
REGISTERED_REPORTS = { | ||
home_office: Reports::HomeOffice, | ||
standing_data: Reports::StandingData, | ||
payroll: Reports::Payroll, | ||
applications: Reports::Applications, | ||
qa: Reports::QaReport, | ||
}.freeze | ||
|
||
def self.call(...) | ||
service = new(...) | ||
service.data | ||
service | ||
end | ||
|
||
def initialize(report_id, **kwargs) | ||
@kwargs = kwargs&.symbolize_keys || {} | ||
@report_class = REGISTERED_REPORTS.with_indifferent_access.fetch(report_id) | ||
rescue KeyError | ||
raise(ArgumentError, "Invalid report id #{report_id}") | ||
end | ||
|
||
def report_name | ||
report_class.to_s.capitalize | ||
end | ||
|
||
def filename | ||
report.name | ||
end | ||
|
||
def data | ||
report.csv | ||
end | ||
|
||
private | ||
|
||
attr_reader :report_class, :kwargs | ||
|
||
def report | ||
return @report if @report | ||
return @report = report_class.new(*report_args) if report_args | ||
|
||
@report = report_class.new | ||
end | ||
|
||
def report_args | ||
return qa_report_args if report_class == Reports::QaReport | ||
|
||
nil | ||
end | ||
|
||
def qa_report_args | ||
return @qa_report_args if @qa_report_args | ||
|
||
status = kwargs.fetch(:status) | ||
applications = Application.filter_by_status(status).reject(&:qa?) | ||
applications.each(&:mark_as_qa!) | ||
|
||
@qa_report_args = [applications, status] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Report do | ||
subject(:report) { described_class } | ||
|
||
describe "registered reports" do | ||
subject(:registered_reports) { described_class::REGISTERED_REPORTS } | ||
|
||
let(:expected_ids) do | ||
%i[home_office standing_data payroll applications qa] | ||
end | ||
|
||
let(:expected_classes) do | ||
[ | ||
Reports::HomeOffice, | ||
Reports::StandingData, | ||
Reports::Payroll, | ||
Reports::Applications, | ||
Reports::QaReport, | ||
] | ||
end | ||
|
||
it { expect(registered_reports.keys).to match_array(expected_ids) } | ||
it { expect(registered_reports.values).to match_array(expected_classes) } | ||
end | ||
|
||
describe ".call" do | ||
subject(:service) { described_class.new(report_id, status:) } | ||
|
||
let(:report_id) { "qa" } | ||
let(:status) { "initial_checks" } | ||
|
||
context "report_name" do | ||
it { expect(service.report_name).to eq("Reports::qareport") } | ||
end | ||
|
||
context "filename" do | ||
include ActiveSupport::Testing::TimeHelpers | ||
it "returns the name of the Report" do | ||
frozen_time = Time.zone.local(2023, 7, 17, 12, 30, 45) | ||
travel_to frozen_time do | ||
expected_name = "QA-Report-initial_checks-2023_07_17-12_30_45.csv" | ||
|
||
expect(service.filename).to eq(expected_name) | ||
end | ||
end | ||
end | ||
|
||
# rubocop:disable RSpec/VerifiedDoubles | ||
context "qa report data" do | ||
let(:report) { spy(Reports::QaReport) } | ||
|
||
before do | ||
allow(Reports::QaReport).to receive(:new).and_return(report) | ||
service.data | ||
end | ||
|
||
it { expect(Reports::QaReport).to have_received(:new).with(kind_of(Array), status) } | ||
it { expect(report).to have_received(:csv) } | ||
end | ||
|
||
context "other report data" do | ||
let(:report_id) { "home_office" } | ||
let(:report) { spy(Reports::HomeOffice) } | ||
|
||
before do | ||
allow(Reports::HomeOffice).to receive(:new).and_return(report) | ||
service.data | ||
end | ||
|
||
it { expect(Reports::HomeOffice).to have_received(:new) } | ||
it { expect(report).to have_received(:csv) } | ||
end | ||
# rubocop:enable RSpec/VerifiedDoubles | ||
end | ||
end |