diff --git a/app/presenters/analyze/graph/column/racial_score.rb b/app/presenters/analyze/graph/column/racial_score.rb index c40826f7..6cf12a7b 100644 --- a/app/presenters/analyze/graph/column/racial_score.rb +++ b/app/presenters/analyze/graph/column/racial_score.rb @@ -12,8 +12,8 @@ module Analyze students = StudentRace.where(race:).pluck(:student_id).uniq averages = grouped_responses(school:, academic_year:, survey_items:, students:) - number_of_responses = total_responses(school:, academic_year:, students:, survey_items:) - scorify(responses: averages, number_of_responses:) + meets_student_threshold = sufficient_responses(school:, academic_year:, students:) + scorify(responses: averages, meets_student_threshold:) end private @@ -43,20 +43,19 @@ module Analyze @response_rate[[school, academic_year]] end - def scorify(responses:, number_of_responses:) + def scorify(responses:, meets_student_threshold:) averages = bubble_up_averages(responses:) average = averages.average - meets_student_threshold = sufficient_responses(averages:, number_of_responses:) average = 0 unless meets_student_threshold Score.new(average, false, meets_student_threshold, false) end - def sufficient_responses(averages:, number_of_responses:) - total_questions = averages.count - average_num_of_responses = number_of_responses.to_f / total_questions - meets_student_threshold = average_num_of_responses >= 10 + def sufficient_responses(school:, academic_year:, students:) + number_of_students_for_a_racial_group = SurveyItemResponse.where(school:, academic_year:, + student: students).map(&:student).uniq.count + number_of_students_for_a_racial_group >= 10 end def bubble_up_averages(responses:) diff --git a/spec/presenters/analyze/graph/racial_score_spec.rb b/spec/presenters/analyze/graph/racial_score_spec.rb index 1a2e0e07..69725ed1 100644 --- a/spec/presenters/analyze/graph/racial_score_spec.rb +++ b/spec/presenters/analyze/graph/racial_score_spec.rb @@ -8,11 +8,15 @@ describe RacialScore do let(:school) { create(:school) } let(:academic_year) { create(:academic_year) } let(:race) { create(:race) } - let(:student) do - s = create(:student) - s.races << race - s.save - s + let(:students) do + [].tap do |arr| + 10.times do + s = create(:student) + s.races << race + s.save + arr << s + end + end end let(:survey_item_1) { measure.survey_items[0] } let(:survey_item_2) { measure.survey_items[1] } @@ -21,26 +25,49 @@ describe RacialScore do create(:response_rate, school:, academic_year:, subcategory: measure.subcategory, meets_student_threshold: true) end - context 'when survey item responses exist' do + context 'when sufficient survey item responses exist' do before :each do response_rate - create(:survey_item_response, school:, academic_year:, likert_score: 1, survey_item: survey_item_1, student:) - create_list(:survey_item_response, 8, school:, academic_year:, likert_score: 2, survey_item: survey_item_1, - student:) - create(:survey_item_response, school:, academic_year:, likert_score: 3, survey_item: survey_item_1, student:) - - create(:survey_item_response, school:, academic_year:, likert_score: 2, survey_item: survey_item_2, student:) - create_list(:survey_item_response, 8, school:, academic_year:, likert_score: 3, survey_item: survey_item_2, - student:) - create(:survey_item_response, school:, academic_year:, likert_score: 4, survey_item: survey_item_2, student:) + students.each do |student| + create(:survey_item_response, school:, academic_year:, likert_score: 2, survey_item: survey_item_1, student:) + end + students.each do |student| + create(:survey_item_response, school:, academic_year:, likert_score: 3, survey_item: survey_item_2, student:) + end end xit 'returns a list of averages' do expect(measure.student_survey_items.count).to eq 2 - students = StudentRace.where(race:).pluck(:student_id) + + expect(students.count).to eq 10 + + expect(SurveyItemResponse.count).to eq 20 american_indian_score = RaceScoreCalculator.new(measure:, school:, academic_year:, race:).score expect(american_indian_score).to eq Score.new(2.5, false, true, false) end end + + context 'when there NOT sufficient survey item responses' do + before :each do + response_rate + 9.times do |index| + create(:survey_item_response, school:, academic_year:, likert_score: 2, survey_item: survey_item_1, + student: students[index]) + end + + 9.times do |index| + create(:survey_item_response, school:, academic_year:, likert_score: 3, survey_item: survey_item_2, + student: students[index]) + end + end + xit 'returns a list of averages' do + expect(measure.student_survey_items.count).to eq 2 + + expect(SurveyItemResponse.count).to eq 18 + + american_indian_score = RaceScoreCalculator.new(measure:, school:, academic_year:, race:).score + expect(american_indian_score).to eq Score.new(0, false, false, false) + end + end end diff --git a/spec/services/student_loader_spec.rb b/spec/services/student_loader_spec.rb index 2b297c72..814f9332 100644 --- a/spec/services/student_loader_spec.rb +++ b/spec/services/student_loader_spec.rb @@ -21,6 +21,18 @@ describe StudentLoader do describe '#process_races' do context 'as a standalone function' do it 'race codes of 6 or 7 get classified as an unknown race' do + codes = [1] + expect(StudentLoader.process_races(codes:)).to eq [american_indian] + codes = [2] + expect(StudentLoader.process_races(codes:)).to eq [asian] + codes = [3] + expect(StudentLoader.process_races(codes:)).to eq [black] + codes = [4] + expect(StudentLoader.process_races(codes:)).to eq [latinx] + codes = [5] + expect(StudentLoader.process_races(codes:)).to eq [white] + codes = [8] + expect(StudentLoader.process_races(codes:)).to eq [middle_eastern] codes = [6] expect(StudentLoader.process_races(codes:)).to eq [unknown] codes = [7] @@ -29,22 +41,27 @@ describe StudentLoader do expect(StudentLoader.process_races(codes:)).to eq [unknown] codes = [1, 6, 7] expect(StudentLoader.process_races(codes:)).to eq [american_indian] + codes = [1, 6, 7, 2] + expect(StudentLoader.process_races(codes:)).to eq [american_indian, asian, multiracial] + codes = [3, 6, 7, 6, 6, 7, 7, 6, 2] + expect(StudentLoader.process_races(codes:)).to eq [black, asian, multiracial] + codes = [8, 2] + expect(StudentLoader.process_races(codes:)).to eq [middle_eastern, asian, multiracial] end end end describe '#add_multiracial_designation' do - it 'adds the multiracial race code to the list of races' do - races = [unknown] - expect(StudentLoader.add_multiracial_designation(races:)).to eq [unknown] - races = [american_indian, asian] - expect(StudentLoader.add_multiracial_designation(races:)).to eq [american_indian, asian, multiracial] - races = [white] - expect(StudentLoader.add_multiracial_designation(races:)).to eq [white] - end + it 'adds the multiracial race code to the list of races' do + races = [unknown] + expect(StudentLoader.add_multiracial_designation(races:)).to eq [unknown] + races = [american_indian, asian] + expect(StudentLoader.add_multiracial_designation(races:)).to eq [american_indian, asian, multiracial] + races = [white] + expect(StudentLoader.add_multiracial_designation(races:)).to eq [white] + end end - # This fails in CI because github does not know what the key derivation salt is. # I'm not sure how to securely set the key derivation salt as an environment variable in CI xdescribe 'self.load_data' do