<commit_before>#
# Copyright (C) 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require_relative '../../../../../spec/spec_helper'

describe CachedGradeDistribution do
  describe "#recalculate!" do
    before :each do
      @course = course_model
      @enrollment = student_in_course
      @enrollment.workflow_state = 'active'
      @enrollment.scores.create!(current_score: 12)
      @enrollment.save!
      @dist = @course.cached_grade_distribution
    end

    it "should count grades from active student enrollments" do
      @dist.recalculate!
      expect(@dist.s12).to eq 1
    end

    it "should count grades from completed student enrollments" do
      @enrollment.workflow_state = 'completed'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 1
    end

    it "should not count grades from invited student enrollments" do
      @enrollment.workflow_state = 'invited'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should not count grades from deleted student enrollments" do
      @enrollment.workflow_state = 'deleted'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should not count grades from fake student enrollments" do
      @enrollment.type = 'StudentViewEnrollment'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should not count grades from teacher enrollments" do
      @enrollment.type = 'TeacherEnrollment'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should count same grade only once per student" do
      other_section = @course.course_sections.create!
      @second_enrollment = @course.enroll_student(@student,
        :enrollment_state => 'active',
        :section => other_section,
        :allow_multiple_enrollments => true)
      @second_enrollment.scores.create!(current_score: 12)

      @dist.recalculate!
      expect(@dist.s12).to eq 1 # not 2
    end

    it "should zero out scores it doesn't see" do
      @dist.recalculate!
      expect(@dist.s12).to eq 1

      @enrollment.find_score.update!(current_score: 11)
      @enrollment.save!
<commit_msg>spec: update cached_grade_distribution_spec<commit_after>#
# Copyright (C) 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require_relative '../../../../../spec/spec_helper'

describe CachedGradeDistribution do
  describe "#recalculate!" do
    before :each do
      @course = course_model
      @enrollment = student_in_course
      @enrollment.workflow_state = 'active'
      @enrollment.scores.create!(current_score: 12)
      @enrollment.save!
      @dist = @course.cached_grade_distribution
    end

    it "should count grades from active student enrollments" do
      @dist.recalculate!
      expect(@dist.s12).to eq 1
    end

    it "should count grades from completed student enrollments" do
      @enrollment.workflow_state = 'completed'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 1
    end

    it "should not count grades from invited student enrollments" do
      @enrollment.workflow_state = 'invited'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should not count grades from deleted student enrollments" do
      @enrollment.workflow_state = 'deleted'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should not count grades from fake student enrollments" do
      @enrollment.type = 'StudentViewEnrollment'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should not count grades from teacher enrollments" do
      @enrollment.type = 'TeacherEnrollment'
      @enrollment.save!

      @dist.recalculate!
      expect(@dist.s12).to eq 0
    end

    it "should count same grade only once per student" do
      other_section = @course.course_sections.create!
      @second_enrollment = @course.enroll_student(@student,
        :enrollment_state => 'active',
        :section => other_section,
        :allow_multiple_enrollments => true)
      score = @second_enrollment.scores.find_or_create_by!(grading_period_id: nil)
      score.update!(current_score: 12)
      @dist.recalculate!
      expect(@dist.s12).to eq 1 # not 2
    end

    it "should zero out scores it doesn't see" do
      @dist.recalculate!
      expect(@dist.s12).to eq 1

      @enrollment.find_score.update!(current_score: 11)
      @enrollment.save!

{'fraction_non_alphanumeric': 0.08985457063711912, 'fraction_numerical': 0.012465373961218837, 'mean_word_length': 3.0034650034650037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5212164', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1710, 'n_words': 553}