From acb01ec92f621ee93d6dff228595a75ff182c7b3 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Thu, 8 Jun 2023 15:24:32 -0700 Subject: [PATCH] Add tests to codify behavior of weighted average logic --- spec/models/response_rate_calculator_spec.rb | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/models/response_rate_calculator_spec.rb b/spec/models/response_rate_calculator_spec.rb index 08ba51d1..6a89dece 100644 --- a/spec/models/response_rate_calculator_spec.rb +++ b/spec/models/response_rate_calculator_spec.rb @@ -159,6 +159,36 @@ describe ResponseRateCalculator, type: :model do end end + context 'when two grades have different numbers of students' do + before do + create(:respondent, school:, academic_year:, total_students: 60, one: 40, two: 20) + create_list(:survey_item_response, 20, survey_item: sufficient_student_survey_item_1, academic_year:, + school:, grade: 1) # 50% + create_list(:survey_item_response, 15, survey_item: sufficient_student_survey_item_2, academic_year:, + school:, grade: 2) # 75% + end + it 'weights the average response rate by the number of students in each grade' do + expect(StudentResponseRateCalculator.new(subcategory:, school:, + academic_year:).rate).to be_within(0.01).of(58.333333) + end + end + + context 'when three grades have different numbers of students' do + before do + create(:respondent, school:, academic_year:, total_students: 120, one: 40, two: 20, three: 60) + create_list(:survey_item_response, 20, survey_item: sufficient_student_survey_item_1, academic_year:, + school:, grade: 1) # 50% + create_list(:survey_item_response, 15, survey_item: sufficient_student_survey_item_2, academic_year:, + school:, grade: 2) # 75% + create_list(:survey_item_response, 15, survey_item: sufficient_student_survey_item_2, academic_year:, + school:, grade: 3) # 25% + end + it 'weights the average response rate by the number of students in each grade' do + expect(StudentResponseRateCalculator.new(subcategory:, school:, + academic_year:).rate).to be_within(0.01).of(41.6666) + end + end + context 'when one grade gets surveyed but another does not, the grade that does not get surveyed is not counted' do before do create(:respondent, school:, academic_year:, total_students: 20, one: 20, two: 20)