Add subcategory report

This commit is contained in:
rebuilt 2023-05-19 16:50:10 -07:00
parent 2e3461b685
commit 87ca23df79
4 changed files with 146 additions and 16 deletions

View file

@ -3,10 +3,12 @@ RSpec.describe Report::Subcategory, type: :model do
let(:school) { create(:school, name: 'Milford High', slug: 'milford-high') }
let(:academic_year) { create(:academic_year, range: '2018-2019') }
let(:subcategory) { create(:subcategory, subcategory_id: '1A') }
let(:respondent) { create(:respondent, school:, academic_year:) }
before :each do
school
academic_year
subcategory
respondent
end
let(:measure) { create(:measure, subcategory:) }
let(:scale) { create(:scale, measure:) }
@ -32,4 +34,58 @@ RSpec.describe Report::Subcategory, type: :model do
expect(subcategory_id).to eq('1A')
end
end
describe '.create_report' do
before do
allow(Report::Subcategory).to receive(:write_csv)
end
it 'generates a CSV report' do
expect(FileUtils).to receive(:mkdir_p).with(Rails.root.join('tmp', 'reports'))
Report::Subcategory.create_report
expect(Report::Subcategory).to have_received(:write_csv)
end
it 'returns the report data' do
data = Report::Subcategory.create_report
expect(data).to be_an(Array)
end
end
describe '.write_csv' do
it 'writes the data to a CSV file' do
data = [['School', 'Academic Year', 'Subcategory'], ['School A', '2022-2023', 'Category A']]
filepath = Rails.root.join('tmp', 'spec', 'reports', 'subcategories.csv')
FileUtils.mkdir_p Rails.root.join('tmp', 'spec', 'reports')
Report::Subcategory.write_csv(data:, filepath:)
csv_data = File.read(filepath)
expect(csv_data).to include('School,Academic Year,Subcategory')
expect(csv_data).to include('School A,2022-2023,Category A')
end
end
describe '.student_score' do
let(:response_rate) { create(:response_rate, subcategory:, school:, academic_year:) }
let(:row) { [response_rate, subcategory, school, academic_year] }
it 'returns student score if response rate meets student threshold' do
allow(subcategory).to receive(:student_score).and_return(80)
allow(response_rate).to receive(:meets_student_threshold?).and_return(true)
score = Report::Subcategory.student_score(row:)
expect(score).to eq(80)
end
it 'returns "N/A" if response rate does not meet student threshold' do
allow(response_rate).to receive(:meets_student_threshold?).and_return(false)
score = Report::Subcategory.student_score(row:)
expect(score).to eq('N/A')
end
end
end