CSC/ECE 517 Fall 2018/E1850. Write unit tests for review response map.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 173: Line 173:
     review_final_versions[symbol][:response_ids] = response_ids
     review_final_versions[symbol][:response_ids] = response_ids
   end
   end
</pre>
Below is our Rspec code:
<pre>
describe ReviewResponseMap do
  let(:participant) { build(:participant, id: 1, user: build(:student, name: 'no name', fullname: 'no one')) }
  let(:participant2) { build(:participant, id: 2) }
  let(:assignment) { build(:assignment, id: 1, name: 'Test Assgt') }
  let(:team1) { build(:assignment_team) }
  let(:review_response_map) { build(:review_response_map, assignment: assignment, reviewer: participant, reviewee: team1) }
  let(:response) { build(:response, id: 1, map_id: 1, response_map: review_response_map, scores: [answer]) }
  let(:answer) { Answer.new(answer: 1, comments: 'Answer text', question_id: 1) }
  let(:answer2) { Answer.new(answer: 2, comments: 'Answer text', question_id: 2) }
  let(:question) { Criterion.new(id: 1, weight: 2, break_before: true) }
  let(:question2) { TextArea.new(id: 1, weight: 2, break_before: true) }
  let(:questionnaire) { ReviewQuestionnaire.new(id: 1, questions: [question], max_question_score: 5) }
  let(:questionnaire2) { ReviewQuestionnaire.new(id: 2, questions: [question2], max_question_score: 5) }
  let(:tag_prompt) {TagPrompt.new(id: 1, prompt: "prompt")}
  let(:tag_prompt_deployment) {TagPromptDeployment.new(id: 1, tag_prompt_id: 1, assignment_id: 1, questionnaire_id: 1, question_type: 'Criterion')}
  let(:empty_response) {build(:response, id: nil, map_id: nil, response_map: nil, scores: [answer])}
  let(:feed_back_response_map) {double('feed_back_response_map', reviewed_object_id: 1, response: empty_response)}
  let(:metareview_response_map) {double('somemap') }
 
  before(:each) do
    allow(response).to receive(:map).and_return(review_response_map)
  end
 
  describe "#questionnaire" do
    context "when round is not nil" do
      it "returns the questionnaire in a certain round" do
        # Write your test here!
        allow(Questionnaire).to receive(:find_by).and_return(questionnaire)
        expect(review_response_map.questionnaire).to eq(questionnaire)
      end
    end
    context "when round is nil" do
      it "returns the questionnaire" do
        # Write your test here!
        allow(Questionnaire).to receive(:find_by).and_return(questionnaire)
        expect(review_response_map.questionnaire).to eq(questionnaire)
      end
    end
  end
  describe "#get_title" do
    it "returns 'Review'" do
      expect(review_response_map.get_title).to eq('Review')
    end
  end
  describe "#delete" do
    it "deletes author feedback response records, metareview response records, and review response records" do
      response_map = double("ResponseMap", :reviewed_object_id => 1)
      allow(review_response_map).to receive(:response).and_return(response)
      #allow(ResponseMap).to receive(:where).and_return([review_response_map])
      #allow(response_map).to receive(:reviewed_object_id).and_return(1)
      #allow(response).to receive(:id).and_return(response_id)
      # allow(response).to receive(:response_id).and_return(1)
      #allow(FeedbackResponseMap).to receive(:where).and_return(feed_back_response_map)
      #allow(MetareviewResponseMap).to receive(:where).and_return(metareview_response_map)
      expect(review_response_map.delete).to eq(review_response_map)
    end
  end
  describe ".export_fields" do
    it "exports the fields of the csv file " do
      expect(ReviewResponseMap.export_fields('_options')).to eq(["contributor", "reviewed by"])
    end
  end
  describe ".export" do
    it "exports reviewer names and reviewee names to an array" do
      #response_map = double("ResponseMap", :reviewed_object_id => 1, :type => ReviewResponseMap, :reviewer_id => 1, :reviewee_id => 1)
      #allow(ReviewResponseMap).to receive(:where).and_return([review_response_map])
      #allow(review_response_map).to receive(:)reviewed_object_id: 1.and_return(review_response_map)
      #allow(review_response_map).to receive(:assignment).and_return(assignment)
      #allow(assignment).to receive(:id).and_return(1)
      #allow(review_response_map).to receive(:reviewer).and_return(participant)
      #allow(participant).to receive(:user_name).and_return('no name')
      #allow(review_response_map).to receive(:reviewee).and_return(team1)
      #allow(response_map).to receive(:reviewed_object_id).and_return(1)
      #allow(team1).to receive(:name).and_return('team1')
      #allow(response_map).to receive(:reviewed_object_id).and_return(1)
      expect(ReviewResponseMap.export([],1,'_options')).to eq([])
    end
  end
  describe ".import" do
    context "when the user of the reviewee is nil" do
      it "raises an ArgumentError saying 'cannot find reviewee user'" do
        allow(User).to receive(:find_by).and_return(nil)
        expect {ReviewResponseMap.import(review_response_map,'_session',1)}.to raise_error(ArgumentError)
      end
    end
    context "when the user of the reviewee is not nil" do
      context "when the participant of the reviewee is nil" do
        it "raises an ArgumentError saying 'Reviewee user is not a participant in this assignment'" do
          allow(AssignmentParticipant).to receive(:find_by).and_return(nil)
          expect {ReviewResponseMap.import(review_response_map,'_session',1)}.to raise_error(ArgumentError)
        end
      end
      # context "when the participant of the reviewee is not nil" do
      #  context "when reviewee does not have a team" do
      #    it "creates a team for reviewee and finds/creates a review response map record" do
      #      user1 = double("User", :id => 5, :name => 'stu')
      #      allow(User).to receive(:find_by).and_return(user1)
      #      allow(user1).to receive(:id).and_return(5)
      #      #allow(assignment).to receive(:id).and_return(1)
      #      participant2 = double("AssignmentParticipant", :user_id => 5, :parent_id => 1)
      #      allow(AssignmentParticipant).to receive(:find_by).and_return(participant2)
      #      #allow(AssignmentTeam).to receive(:find_by).and_return(nil)
      #      t_user = double("TeamUser", :team_id => 1, user_id: 5)
      #
      #      #allow(ResponseMap).to receive(:where).and_return([review_response_map])
      #      expect(ReviewResponseMap.import([],'_session',1)).to eq(review_response_map)
      #    end
      #  end
      #
      #
      #  context "when reviewee has a team" do
      #    it "finds/creates a review response map record" do
      #      team = team1
      #      allow(team).to receive(:id).and_return(1)
      #      allow(ResponseMap).to receive(:where).and_return([review_response_map])
      #      expect(review_response_map.import).to eq([review_response_map])
      #    end
      #  end
      #
      # end
    end
  end
  describe "#show_feedback" do
    context "when there is no review responses and the response parameter is nil" do
      it "returns nil" do
        expect(review_response_map.show_feedback(nil)).to eq(nil)
        expect(review_response_map.show_feedback(empty_response)).to eq(nil)
      end
    end
    context "when there exist review responses or the response parameter is not nil" do
      context "when author feedback response map record does not exist or there aren't corresponding responses" do
        it "returns the map variable" do
          allow(review_response_map).to receive_message_chain(:response, :any?) {true}
          allow(FeedbackResponseMap).to receive(:find_by).and_return(feed_back_response_map)
          map = feed_back_response_map
          allow(map).to receive_message_chain(:response, :any?) {false}
          expect(review_response_map.show_feedback(response)).to eq(nil)
        end
      end
    end
    context "when author feedback response map record exists and there exist corresponding responses" do
      it "returns the HTML code which displays the lastest author feedback response" do
        allow(review_response_map).to receive_message_chain(:response, :any?) {true}
        allow(FeedbackResponseMap).to receive(:find_by).and_return(feed_back_response_map)
        map = feed_back_response_map
        allow(map).to receive_message_chain(:response, :any?) {true}
        allow(map).to receive_message_chain(:response, :last).and_return(response)
        expect(review_response_map.show_feedback(response)).to eq("<table width=\"100%\"><tr><td align=\"left\" width=\"70%\"><b>Review </b>&nbsp;&nbsp;&nbsp;<a href=\"#\" name= \"review_1Link\" onClick=\"toggleElement('review_1','review');return false;\">show review</a></td><td align=\"left\"><b>Last Reviewed:</b><span>Not available</span></td></tr></table><table id=\"review_1\" style=\"display: none;\" class=\"table table-bordered\"><tr><td><b>Additional Comment: </b></td></tr></table>")
      end
    end
  end
  describe "#metareview_response_maps" do
    it "returns metareviews related to current review response map" do
      allow(Response).to receive(:where).and_return([response])
      allow(MetareviewResponseMap).to receive(:where).with(reviewed_object_id: 1).and_return([metareview_response_map])
      expect(review_response_map.metareview_response_maps).to eq([metareview_response_map])
    end
  end
  describe ".get_responses_for_team_round" do
    context "when the team id is nil" do
      it "returns an empty array" do
        expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([])
      end
    end
    before(:each) do
      team = team1
      allow(team).to receive(:id).and_return(1)
      allow(ResponseMap).to receive(:where).and_return([review_response_map])
      map = review_response_map
      allow(map).to receive(:response).and_return([response])
    end
    context "when the team id is not nil" do
      context "when current response map does not have responses" do
        it "returns an array with satisfied responses" do
          map = review_response_map
          allow(response).to receive(:any?).and_return(false)
          expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([])
        end
      end
    end
    before(:each) do
      allow(response).to receive(:any?).and_return(true)
    end
    context "when current response map has responses" do
      context "when all these responses don't belong to this round or have been submitted" do
        it "returns an array with satisfied responses" do
          allow(response).to receive(:round).and_return(2)
          expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([])
        end
      end
    end
    context "when one or more responses belong to this round and haven't been submitted" do
      it "returns an array with satisfied responses" do
        allow(response).to receive(:round).and_return(1)
        allow(response).to receive(:is_submitted).and_return(true)
        expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([response])
      end
    end
  end
  describe ".final_versions_from_reviewer" do
    it "returns a hash with the latest version of response for each response map record and corresponding questionnaire ids" do
      review_id = double('1', :to_i => 1)
      maps = []
      allow(ReviewResponseMap).to receive(:where).and_return(maps)
      allow(Assignment).to receive(:find).and_return(assignment)
      allow(Participant).to receive_message_chain(:find, :parent_id).and_return(participant)
      expect(ReviewResponseMap.final_versions_from_reviewer(review_id)).to eq(ReviewResponseMap.prepare_final_review_versions(assignment,maps))
    end
  end
  describe ".review_response_report" do
    context "when the review user is nil" do
      xit "returns sorted reviewers of a certain type of response map" do
        response_maps_with_distinct_participant_id = 1
        id = 1
        type = 'type'
        reviewers = double('reviewers')
        allow(ResponseMap).to receive_message_chain(:select, :where).and_return(1)
        allow(AssignmentParticipant).to receive(:find).and_return(1)
        allow(Participant).to receive(:sort_by_name).and_return([reviewers])
        expect(ReviewResponseMap.review_response_report(id, assignment, type, review_response_map.reviewer)).to eq([reviewers])
      end
    end
    context "when the review user is not nil" do
      xit "return reviewers users' full name" do
        user_ids = []
        id = double('id', id: 1)
        type = double('type', type: 'type')
        reviewers = double('reviewers')
        allow(User).to receive(:where).and_return([user_ids])
        allow(AssignmentParticipant).to receive(:where).and_return([reviewers])
        expect(ReviewResponseMap.review_response_report(id = 1, assignment, type, review_response_map.reviewer)).to eq([reviewers])
      end
    end
  end
  describe "#email" do
    it "sends emails to team members whose work has been reviewed" do
      user = double('user', id: 1)
      defn = {:body => {:type => "peer review", :obj_name => "name1", :first_name => "fname", :partial_name => "name2"}, :to => "email1"}
      allow(AssignmentTeam).to receive_message_chain(:find, :users).and_return([user])
      allow(assignment).to receive(:name).and_return('')
      allow(User).to receive_message_chain(:find, :fullname).and_return('')
      allow(User).to receive_message_chain(:find, :email).and_return('')
      allow(Mailer).to receive_message_chain(:sync_message, :deliver_now).and_return('')
      expect(review_response_map.email(defn, participant, assignment)).to eq([user])
    end
  end
  describe ".prepare_final_review_versions" do
    context "when round number is not nil and is bigger than 1" do
      xit "returns the final version of responses in each round" do
        maps = []
        review_final_versions = {}
        round = 2
        allow(assignment).to receive(:rounds_of_reviews).and_return(round)
        expect(ReviewResponseMap.prepare_final_review_versions(assignment, maps)).to eq(ReviewResponseMap.prepare_review_response(assignment, maps, review_final_versions, round))
      end
    end
    context "when round number is nil or is smaller than or equal to 1" do
      xit "returns the final version of responses" do
        maps = []
        review_final_versions = {}
        round = nil
        round_of_reviews = nil
        review_questionnaire_id = nil
        assignment = double('assignment', round_of_reviews: 3, review_questionnaire_id: 1)
        allow(assignment).to receive(:rounds_of_reviews).and_return(round)
        expect(ReviewResponseMap.prepare_final_review_versions(assignment, maps)).to eq(ReviewResponseMap.prepare_review_response(assignment, maps, review_final_versions, nil))
      end
    end
  end
  describe ".prepare_review_response" do
    context "when the round is nil" do
      it "uses :review as hash key and populate the hash with review questionnaire id and response ids" do
        symbol = :review
        review_final_versions = {symbol => {:questionnaire_id => 1, :response_ids => 1}}
        where_map = {}
        responses = 1
        allow(assignment).to receive(:review_questionnaire_id).and_return(1)
        allow(Response).to receive(:where).and_return(1)
      end
    end
    context "when the round is not nil" do
      it "uses review round number as hash key and populate the hash with review questionnaire id, round, and response ids" do
        round = 1
        symbol = ("review round" + round.to_s).to_sym
        review_final_versions = {symbol => {:questionnaire_id => 1, :response_ids => 1}}
        where_map = {}
        responses = 1
        allow(assignment).to receive(:review_questionnaire_id).and_return(1)
        allow(Response).to receive(:where).and_return(1)
      end
    end
  end
end
</pre>
</pre>



Revision as of 21:53, 2 November 2018

Introduction

Background

Expertiza is an open source web based peer review system developed and maintained by students and faculty members at North Carolina State University. It enables students enrolled in a particular course to form online teams and complete assignments.

Team

Zhewei Hu (zhu6) (mentor)

  • Cheng Yuan (cyuan7)
  • Yue Tian (ytian22)
  • Ke Zhao (kzhao2)

Task

Our job is to use Rspec to write unit tests for review_response_map.rb.


Work

We have the following functions to test.

  def questionnaire(round = nil)
    Questionnaire.find_by(id: self.assignment.review_questionnaire_id(round))
  end

  def get_title
    "Review"
  end

  def delete(_force = nil)
    fmaps = FeedbackResponseMap.where(reviewed_object_id: self.response.response_id)
    fmaps.each(&:destroy)
    maps = MetareviewResponseMap.where(reviewed_object_id: self.id)
    maps.each(&:destroy)
    self.destroy
  end

  def self.export_fields(_options)
    ["contributor", "reviewed by"]
  end

  def self.export(csv, parent_id, _options)
    mappings = where(reviewed_object_id: parent_id).to_a
    mappings.sort! {|a, b| a.reviewee.name <=> b.reviewee.name }
    mappings.each do |map|
      csv << [
        map.reviewee.name,
        map.reviewer.name
      ]
    end
  end

  def self.import(row_hash, _session, assignment_id)
    reviewee_user_name = row_hash[:reviewee].to_s
    reviewee_user = User.find_by(name: reviewee_user_name)
    raise ArgumentError, "Cannot find reviewee user." unless reviewee_user
    reviewee_participant = AssignmentParticipant.find_by(user_id: reviewee_user.id, parent_id: assignment_id)
    raise ArgumentError, "Reviewee user is not a participant in this assignment." unless reviewee_participant
    reviewee_team = AssignmentTeam.team(reviewee_participant)
    if reviewee_team.nil? # lazy team creation: if the reviewee does not have team, create one.
      reviewee_team = AssignmentTeam.create(name: 'Team' + '_' + rand(1000).to_s,
                                            parent_id: assignment_id, type: 'AssignmentTeam')
      t_user = TeamsUser.create(team_id: reviewee_team.id, user_id: reviewee_user.id)
      team_node = TeamNode.create(parent_id: assignment_id, node_object_id: reviewee_team.id)
      TeamUserNode.create(parent_id: team_node.id, node_object_id: t_user.id)
    end
    row_hash[:reviewers].each do |reviewer|
      reviewer_user_name = reviewer.to_s
      reviewer_user = User.find_by(name: reviewer_user_name)
      raise ArgumentError, "Cannot find reviewer user." unless reviewer_user
      next if reviewer_user_name.empty?
      reviewer_participant = AssignmentParticipant.find_by(user_id: reviewer_user.id, parent_id: assignment_id)
      raise ArgumentError, "Reviewer user is not a participant in this assignment." unless reviewer_participant
      ReviewResponseMap.find_or_create_by(reviewed_object_id: assignment_id,
                                          reviewer_id: reviewer_participant.id,
                                          reviewee_id: reviewee_team.id,
                                          calibrate_to: false)
    end
  end

  def show_feedback(response)
    return unless self.response.any? and response
    map = FeedbackResponseMap.find_by(reviewed_object_id: response.id)
    return map.response.last.display_as_html if map and map.response.any?
  end

  def metareview_response_maps
    responses = Response.where(map_id: self.id)
    metareview_list = []
    responses.each do |response|
      metareview_response_maps = MetareviewResponseMap.where(reviewed_object_id: response.id)
      metareview_response_maps.each {|metareview_response_map| metareview_list << metareview_response_map }
    end
    metareview_list
  end

  # return the responses for specified round, for varying rubric feature -Yang
  def self.get_responses_for_team_round(team, round)
    responses = []
    if team.id
      maps = ResponseMap.where(reviewee_id: team.id, type: "ReviewResponseMap")
      maps.each do |map|
        if map.response.any? and map.response.reject {|r| (r.round != round || !r.is_submitted) }.any?
          responses << map.response.reject {|r| (r.round != round || !r.is_submitted) }.last
        end
      end
      responses.sort! {|a, b| a.map.reviewer.fullname <=> b.map.reviewer.fullname }
    end
    responses
  end

  # wrap lastest version of responses in each response map, together withe the questionnaire_id
  # will be used to display the reviewer summary
  def self.final_versions_from_reviewer(reviewer_id)
    maps = ReviewResponseMap.where(reviewer_id: reviewer_id)
    assignment = Assignment.find(Participant.find(reviewer_id).parent_id)
    prepare_final_review_versions(assignment, maps)
  end

  def self.review_response_report(id, assignment, type, review_user)
    if review_user.nil?
      # This is not a search, so find all reviewers for this assignment
      response_maps_with_distinct_participant_id =
        ResponseMap.select("DISTINCT reviewer_id").where('reviewed_object_id = ? and type = ? and calibrate_to = ?', id, type, 0)
      @reviewers = []
      response_maps_with_distinct_participant_id.each do |reviewer_id_from_response_map|
        @reviewers << AssignmentParticipant.find(reviewer_id_from_response_map.reviewer_id)
      end
      @reviewers = Participant.sort_by_name(@reviewers)
    else
      # This is a search, so find reviewers by user's full name
      user_ids = User.select("DISTINCT id").where('fullname LIKE ?', '%' + review_user[:fullname] + '%')
      @reviewers = AssignmentParticipant.where('user_id IN (?) and parent_id = ?', user_ids, assignment.id)
    end
    # @review_scores[reveiwer_id][reviewee_id] = score for assignments not using vary_rubric_by_rounds feature
    # @review_scores[reviewer_id][round][reviewee_id] = score for assignments using vary_rubric_by_rounds feature
  end

  def email(defn, _participant, assignment)
    defn[:body][:type] = "Peer Review"
    AssignmentTeam.find(reviewee_id).users.each do |user|
      defn[:body][:obj_name] = assignment.name
      defn[:body][:first_name] = User.find(user.id).fullname
      defn[:to] = User.find(user.id).email
      Mailer.sync_message(defn).deliver_now
    end
  end

  def self.prepare_final_review_versions(assignment, maps)
    review_final_versions = {}
    rounds_num = assignment.rounds_of_reviews
    if rounds_num and rounds_num > 1
      (1..rounds_num).each do |round|
        prepare_review_response(assignment, maps, review_final_versions, round)
      end
    else
      prepare_review_response(assignment, maps, review_final_versions, nil)
    end
    review_final_versions
  end

  def self.prepare_review_response(assignment, maps, review_final_versions, round)
    symbol = if round.nil?
               :review
             else
               ("review round" + round.to_s).to_sym
             end
    review_final_versions[symbol] = {}
    review_final_versions[symbol][:questionnaire_id] = assignment.review_questionnaire_id(round)
    response_ids = []
    maps.each do |map|
      where_map = {map_id: map.id}
      where_map[:round] = round unless round.nil?
      responses = Response.where(where_map)
      response_ids << responses.last.id unless responses.empty?
    end
    review_final_versions[symbol][:response_ids] = response_ids
  end

Below is our Rspec code:

describe ReviewResponseMap do
  let(:participant) { build(:participant, id: 1, user: build(:student, name: 'no name', fullname: 'no one')) }
  let(:participant2) { build(:participant, id: 2) }
  let(:assignment) { build(:assignment, id: 1, name: 'Test Assgt') }
  let(:team1) { build(:assignment_team) }
  let(:review_response_map) { build(:review_response_map, assignment: assignment, reviewer: participant, reviewee: team1) }
  let(:response) { build(:response, id: 1, map_id: 1, response_map: review_response_map, scores: [answer]) }
  let(:answer) { Answer.new(answer: 1, comments: 'Answer text', question_id: 1) }
  let(:answer2) { Answer.new(answer: 2, comments: 'Answer text', question_id: 2) }
  let(:question) { Criterion.new(id: 1, weight: 2, break_before: true) }
  let(:question2) { TextArea.new(id: 1, weight: 2, break_before: true) }
  let(:questionnaire) { ReviewQuestionnaire.new(id: 1, questions: [question], max_question_score: 5) }
  let(:questionnaire2) { ReviewQuestionnaire.new(id: 2, questions: [question2], max_question_score: 5) }
  let(:tag_prompt) {TagPrompt.new(id: 1, prompt: "prompt")}
  let(:tag_prompt_deployment) {TagPromptDeployment.new(id: 1, tag_prompt_id: 1, assignment_id: 1, questionnaire_id: 1, question_type: 'Criterion')}
  let(:empty_response) {build(:response, id: nil, map_id: nil, response_map: nil, scores: [answer])}
  let(:feed_back_response_map) {double('feed_back_response_map', reviewed_object_id: 1, response: empty_response)}
  let(:metareview_response_map) {double('somemap') }
  
  before(:each) do
    allow(response).to receive(:map).and_return(review_response_map)
  end
  
  describe "#questionnaire" do
    context "when round is not nil" do
      it "returns the questionnaire in a certain round" do
        # Write your test here!
        allow(Questionnaire).to receive(:find_by).and_return(questionnaire)
        expect(review_response_map.questionnaire).to eq(questionnaire)
      end
    end
    context "when round is nil" do
      it "returns the questionnaire" do
        # Write your test here!
        allow(Questionnaire).to receive(:find_by).and_return(questionnaire)
        expect(review_response_map.questionnaire).to eq(questionnaire)
      end
    end
  end

  describe "#get_title" do
    it "returns 'Review'" do
      expect(review_response_map.get_title).to eq('Review')
    end
  end

  describe "#delete" do
    it "deletes author feedback response records, metareview response records, and review response records" do
      response_map = double("ResponseMap", :reviewed_object_id => 1)
      allow(review_response_map).to receive(:response).and_return(response)
      #allow(ResponseMap).to receive(:where).and_return([review_response_map])
      #allow(response_map).to receive(:reviewed_object_id).and_return(1)
      #allow(response).to receive(:id).and_return(response_id)
      # allow(response).to receive(:response_id).and_return(1)
      #allow(FeedbackResponseMap).to receive(:where).and_return(feed_back_response_map)
      #allow(MetareviewResponseMap).to receive(:where).and_return(metareview_response_map)
      expect(review_response_map.delete).to eq(review_response_map)
    end
  end

  describe ".export_fields" do
    it "exports the fields of the csv file " do
      expect(ReviewResponseMap.export_fields('_options')).to eq(["contributor", "reviewed by"])
    end
  end

  describe ".export" do
    it "exports reviewer names and reviewee names to an array" do
      #response_map = double("ResponseMap", :reviewed_object_id => 1, :type => ReviewResponseMap, :reviewer_id => 1, :reviewee_id => 1)
      #allow(ReviewResponseMap).to receive(:where).and_return([review_response_map])
      #allow(review_response_map).to receive(:)reviewed_object_id: 1.and_return(review_response_map)
      #allow(review_response_map).to receive(:assignment).and_return(assignment)
      #allow(assignment).to receive(:id).and_return(1)
      #allow(review_response_map).to receive(:reviewer).and_return(participant)
      #allow(participant).to receive(:user_name).and_return('no name')
      #allow(review_response_map).to receive(:reviewee).and_return(team1)
      #allow(response_map).to receive(:reviewed_object_id).and_return(1)
      #allow(team1).to receive(:name).and_return('team1')
      #allow(response_map).to receive(:reviewed_object_id).and_return(1)
      expect(ReviewResponseMap.export([],1,'_options')).to eq([])
    end
  end

  describe ".import" do
    context "when the user of the reviewee is nil" do
      it "raises an ArgumentError saying 'cannot find reviewee user'" do
        allow(User).to receive(:find_by).and_return(nil)
        expect {ReviewResponseMap.import(review_response_map,'_session',1)}.to raise_error(ArgumentError)
      end
    end

    context "when the user of the reviewee is not nil" do
      context "when the participant of the reviewee is nil" do
        it "raises an ArgumentError saying 'Reviewee user is not a participant in this assignment'" do
          allow(AssignmentParticipant).to receive(:find_by).and_return(nil)
          expect {ReviewResponseMap.import(review_response_map,'_session',1)}.to raise_error(ArgumentError)
        end
      end


      # context "when the participant of the reviewee is not nil" do
      #   context "when reviewee does not have a team" do
      #     it "creates a team for reviewee and finds/creates a review response map record" do
      #       user1 = double("User", :id => 5, :name => 'stu')
      #       allow(User).to receive(:find_by).and_return(user1)
      #       allow(user1).to receive(:id).and_return(5)
      #       #allow(assignment).to receive(:id).and_return(1)
      #       participant2 = double("AssignmentParticipant", :user_id => 5, :parent_id => 1)
      #       allow(AssignmentParticipant).to receive(:find_by).and_return(participant2)
      #       #allow(AssignmentTeam).to receive(:find_by).and_return(nil)
      #       t_user = double("TeamUser", :team_id => 1, user_id: 5)
      #
      #       #allow(ResponseMap).to receive(:where).and_return([review_response_map])
      #       expect(ReviewResponseMap.import([],'_session',1)).to eq(review_response_map)
      #     end
      #   end
      #
      #
      #   context "when reviewee has a team" do
      #     it "finds/creates a review response map record" do
      #       team = team1
      #       allow(team).to receive(:id).and_return(1)
      #       allow(ResponseMap).to receive(:where).and_return([review_response_map])
      #       expect(review_response_map.import).to eq([review_response_map])
      #     end
      #   end
      #
      # end
    end
  end

  describe "#show_feedback" do
    context "when there is no review responses and the response parameter is nil" do
      it "returns nil" do
        expect(review_response_map.show_feedback(nil)).to eq(nil)
        expect(review_response_map.show_feedback(empty_response)).to eq(nil)

      end
    end

    context "when there exist review responses or the response parameter is not nil" do
      context "when author feedback response map record does not exist or there aren't corresponding responses" do
        it "returns the map variable" do
          allow(review_response_map).to receive_message_chain(:response, :any?) {true}
          allow(FeedbackResponseMap).to receive(:find_by).and_return(feed_back_response_map)
          map = feed_back_response_map
          allow(map).to receive_message_chain(:response, :any?) {false}

          expect(review_response_map.show_feedback(response)).to eq(nil)
        end
      end
    end

    context "when author feedback response map record exists and there exist corresponding responses" do
      it "returns the HTML code which displays the lastest author feedback response" do
        allow(review_response_map).to receive_message_chain(:response, :any?) {true}
        allow(FeedbackResponseMap).to receive(:find_by).and_return(feed_back_response_map)
        map = feed_back_response_map
        allow(map).to receive_message_chain(:response, :any?) {true}
        allow(map).to receive_message_chain(:response, :last).and_return(response)

        expect(review_response_map.show_feedback(response)).to eq("<table width=\"100%\"><tr><td align=\"left\" width=\"70%\"><b>Review </b>   <a href=\"#\" name= \"review_1Link\" onClick=\"toggleElement('review_1','review');return false;\">show review</a></td><td align=\"left\"><b>Last Reviewed:</b><span>Not available</span></td></tr></table><table id=\"review_1\" style=\"display: none;\" class=\"table table-bordered\"><tr><td><b>Additional Comment: </b></td></tr></table>")
      end
    end
  end

  describe "#metareview_response_maps" do
    it "returns metareviews related to current review response map" do
      allow(Response).to receive(:where).and_return([response])
      allow(MetareviewResponseMap).to receive(:where).with(reviewed_object_id: 1).and_return([metareview_response_map])

      expect(review_response_map.metareview_response_maps).to eq([metareview_response_map])

    end
  end

  describe ".get_responses_for_team_round" do
    context "when the team id is nil" do
      it "returns an empty array" do

        expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([])

      end
    end

    before(:each) do
      team = team1
      allow(team).to receive(:id).and_return(1)
      allow(ResponseMap).to receive(:where).and_return([review_response_map])
      map = review_response_map
      allow(map).to receive(:response).and_return([response])
    end

    context "when the team id is not nil" do
      context "when current response map does not have responses" do
        it "returns an array with satisfied responses" do
          map = review_response_map
          allow(response).to receive(:any?).and_return(false)

          expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([])
        end
      end
    end

    before(:each) do
      allow(response).to receive(:any?).and_return(true)
    end

    context "when current response map has responses" do
      context "when all these responses don't belong to this round or have been submitted" do
        it "returns an array with satisfied responses" do
          allow(response).to receive(:round).and_return(2)

          expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([])
        end
      end
    end

    context "when one or more responses belong to this round and haven't been submitted" do
      it "returns an array with satisfied responses" do
        allow(response).to receive(:round).and_return(1)
        allow(response).to receive(:is_submitted).and_return(true)

        expect(ReviewResponseMap.get_responses_for_team_round(team1, 1)).to eq([response])
      end
    end
  end

  describe ".final_versions_from_reviewer" do
    it "returns a hash with the latest version of response for each response map record and corresponding questionnaire ids" do
      review_id = double('1', :to_i => 1)
      maps = []
      allow(ReviewResponseMap).to receive(:where).and_return(maps)
      allow(Assignment).to receive(:find).and_return(assignment)
      allow(Participant).to receive_message_chain(:find, :parent_id).and_return(participant)
      expect(ReviewResponseMap.final_versions_from_reviewer(review_id)).to eq(ReviewResponseMap.prepare_final_review_versions(assignment,maps))
    end
  end

  describe ".review_response_report" do
    context "when the review user is nil" do
      xit "returns sorted reviewers of a certain type of response map" do
        response_maps_with_distinct_participant_id = 1
        id = 1
        type = 'type'
        reviewers = double('reviewers')
        allow(ResponseMap).to receive_message_chain(:select, :where).and_return(1)
        allow(AssignmentParticipant).to receive(:find).and_return(1)
        allow(Participant).to receive(:sort_by_name).and_return([reviewers])
        expect(ReviewResponseMap.review_response_report(id, assignment, type, review_response_map.reviewer)).to eq([reviewers])
      end
    end

    context "when the review user is not nil" do
      xit "return reviewers users' full name" do
        user_ids = []
        id = double('id', id: 1)
        type = double('type', type: 'type')
        reviewers = double('reviewers')
        allow(User).to receive(:where).and_return([user_ids])
        allow(AssignmentParticipant).to receive(:where).and_return([reviewers])
        expect(ReviewResponseMap.review_response_report(id = 1, assignment, type, review_response_map.reviewer)).to eq([reviewers])
      end
    end
  end

  describe "#email" do
    it "sends emails to team members whose work has been reviewed" do
      user = double('user', id: 1)
      defn = {:body => {:type => "peer review", :obj_name => "name1", :first_name => "fname", :partial_name => "name2"}, :to => "email1"}
      allow(AssignmentTeam).to receive_message_chain(:find, :users).and_return([user])
      allow(assignment).to receive(:name).and_return('')
      allow(User).to receive_message_chain(:find, :fullname).and_return('')
      allow(User).to receive_message_chain(:find, :email).and_return('')
      allow(Mailer).to receive_message_chain(:sync_message, :deliver_now).and_return('')
      expect(review_response_map.email(defn, participant, assignment)).to eq([user])
    end
  end

  describe ".prepare_final_review_versions" do
    context "when round number is not nil and is bigger than 1" do
      xit "returns the final version of responses in each round" do
        maps = []
        review_final_versions = {}
        round = 2
        allow(assignment).to receive(:rounds_of_reviews).and_return(round)
        expect(ReviewResponseMap.prepare_final_review_versions(assignment, maps)).to eq(ReviewResponseMap.prepare_review_response(assignment, maps, review_final_versions, round))
      end
    end
    context "when round number is nil or is smaller than or equal to 1" do
      xit "returns the final version of responses" do
        maps = []
        review_final_versions = {}
        round = nil
        round_of_reviews = nil
        review_questionnaire_id = nil
        assignment = double('assignment', round_of_reviews: 3, review_questionnaire_id: 1)
        allow(assignment).to receive(:rounds_of_reviews).and_return(round)
        expect(ReviewResponseMap.prepare_final_review_versions(assignment, maps)).to eq(ReviewResponseMap.prepare_review_response(assignment, maps, review_final_versions, nil))
      end
    end
  end

  describe ".prepare_review_response" do
    context "when the round is nil" do
      it "uses :review as hash key and populate the hash with review questionnaire id and response ids" do
        symbol = :review
        review_final_versions = {symbol => {:questionnaire_id => 1, :response_ids => 1}}
        where_map = {}
        responses = 1
        allow(assignment).to receive(:review_questionnaire_id).and_return(1)
        allow(Response).to receive(:where).and_return(1)
      end
    end
    context "when the round is not nil" do
      it "uses review round number as hash key and populate the hash with review questionnaire id, round, and response ids" do
        round = 1
        symbol = ("review round" + round.to_s).to_sym
        review_final_versions = {symbol => {:questionnaire_id => 1, :response_ids => 1}}
        where_map = {}
        responses = 1
        allow(assignment).to receive(:review_questionnaire_id).and_return(1)
        allow(Response).to receive(:where).and_return(1)
      end
    end
  end

end

Run the Test

The tests can be run on the terminal from inside the expertiza folder using following commands:

 rspec spec/models/review_response_map_spec.rb


References

Expertiza documentation

Expertiza in Github

Code

Demo

Rspec Documentation