<commit_before>require 'spec_helper'

describe Assessment do

  context "when completing" do
    
    before(:each) do
      config = File.join( __dir__, "..", "lib", "test-survey.xls" )    
      QuestionnaireImporter.load(1, config)    
    end

    let(:assessment) { FactoryGirl.create :unfinished_assessment }
    let!(:assessment_answer) { AssessmentAnswer.create( assessment: assessment, question: Question.first, answer: Answer.find_by_code("Q1.2") ) }
          
    it "should identify when complete" do
      assessment = FactoryGirl.create(:assessment)
      expect( assessment.status ).to eql(:complete)
    end
    
    it "should identify when incomplete" do
      expect( assessment.status ).to eql(:incomplete)
    end
    
    it "should set completion date" do
      assessment.complete
      expect( assessment.completion_date ).to_not be_nil
      expect( assessment.status ).to eql(:complete)      
    end
    
    it "should only complete if allowed" do
      AssessmentAnswer.destroy_all
      assessment.complete
      expect( assessment.status ).to eql(:incomplete)   
    end
    
    it "should store scores" do
      assessment.complete
      expect( assessment.scores.length ).to eql(1)
      expect( Score.first.score ).to eql(1)
    end

    describe "#update_targets" do 
      it "should change all the scores for an assessment" do
        score_targets = {}
        assessment.complete
        Rails.logger.info("\n #{assessment.scores.count} scores to change \n")
        assessment.scores.each { |s| score_targets.merge!(s.id.to_s => rand(1..5)) }
        assessment.update_targets(score_targets)
        scores_changed = true
        score_targets.each {|score_id, target| scores_changed = false if Score.find(score_id).target != target }
        expect(scores_changed).to eq(true)
      end
    end

    describe "#next_activity" do
      it "should return the next unstarted activity" do<commit_msg>extends test coverage of update_targets method<commit_after>require 'spec_helper'

describe Assessment do

  context "when completing" do
    
    before(:each) do
      config = File.join( __dir__, "..", "lib", "test-survey.xls" )    
      QuestionnaireImporter.load(1, config)    
    end

    let(:assessment) { FactoryGirl.create :unfinished_assessment }
    let!(:assessment_answer) { AssessmentAnswer.create( assessment: assessment, question: Question.first, answer: Answer.find_by_code("Q1.2") ) }
          
    it "should identify when complete" do
      assessment = FactoryGirl.create(:assessment)
      expect( assessment.status ).to eql(:complete)
    end
    
    it "should identify when incomplete" do
      expect( assessment.status ).to eql(:incomplete)
    end
    
    it "should set completion date" do
      assessment.complete
      expect( assessment.completion_date ).to_not be_nil
      expect( assessment.status ).to eql(:complete)      
    end
    
    it "should only complete if allowed" do
      AssessmentAnswer.destroy_all
      assessment.complete
      expect( assessment.status ).to eql(:incomplete)   
    end
    
    it "should store scores" do
      assessment.complete
      expect( assessment.scores.length ).to eql(1)
      expect( Score.first.score ).to eql(1)
    end

    describe "#update_targets" do 
      it "should change all the scores for an assessment" do
        score_targets = {}
        assessment.complete
        assessment.scores.each { |s| score_targets.merge!(s.id.to_s => rand(1..5)) }
        assessment.update_targets(score_targets)
        scores_changed = true
        score_targets.each {|score_id, target| scores_changed = false if Score.find(score_id).target != target }
        expect(scores_changed).to eq(true)
      end

      it "should not change all the scores when there is an invalid target value" do
        score_targets = {}
        assessment.complete
        assessment.scores.each { |s| score_targets.merge!(s.id.to_s => 0) }
        assessment.update_targets(score_targets)
        scores_changed = false
        score_targets.each do |score_id, target| 
          if Score.find(score_id).target == target 
            Rails.logger.info "\n target equals #{score_id} - #{target}"
            scores_changed = true 
          end
        end
        expect(scores_changed).to eq(false)
      end
    end

    describe "#next_activity" do
      it "should return the next unstarted activity" do
{'fraction_non_alphanumeric': 0.09522732413481112, 'fraction_numerical': 0.0033928975344944584, 'mean_word_length': 2.6727574750830563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4415842', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1248, 'n_words': 342}