E1850. Write unit tests for review response map.rb team100

From Expertiza_Wiki
Jump to navigation Jump to search

This wiki page is for the description of unit test for E1850 OSS assignment for Fall 2018, CSC/ECE 517.

Background

Expertiza is an open source web based on peer review system developed and maintained by students and faculty members at North Carolina State University. It enables students who enrolled in a particular course to form online teams, complete assignments, review other's work and receive feedbacks of their works.

Problem

Review_response_map.rb is used to prepare data for peer review. But there are no unit tests for it.

Work to be done

  • Create a new file named review_response_map_spec.rb under spec/models folder
  • Write RSpec unit tests to make the path coverage above 90%.
  • Coverage edge cases.
  • Achieve high branch coverage. Use the mutant-rspec gem to measure test thoroughness and fault-finding capability of tests.

Files Created

  • spec/models/review_response_map_spec.rb

Test Plan

Setup expertiza environment

Follow the instruction on http://wiki.expertiza.ncsu.edu/index.php/Development:Setup:OSX#Get_Expertiza

Model functions

To better test the methods, we read the whole review response map model and understand the functionalities of what we want to test.

  • Questionnaire: find the specific questionnaire.
  • Get title: set the title.
  • Delete: delete the feedback response map and metareview response map.
  • Export fields: set the fields of export.
  • Export: export the review response map.
  • Import: import the review response map from local.
  • Show feedback: show the feedback via html.
  • Metareview response maps: fetch all the metareview response map and return.
  • Get responses for team round: get the responses for given round.
  • Final versions from reviewer: return the final review version from reviewers.
  • Review response report: find the reviewers for a given assignment.
  • Email: send an email to the members in a team.
  • Prepare final review versions: prepare the final review versions for an assignment.
  • Prepare review response: return the review responses id for an assignment.

Mock instance

We mock the necessary instances for the test in the beginning of test file.

  let(:team) { build(:assignment_team, id: 1, name: 'team no name', assignment: assignment, users: [student], parent_id: 1) }
  let(:team1) { build(:assignment_team, id: 2, name: 'team has name', assignment: assignment, users: [student]) }
  let(:review_response_map) { build(:review_response_map, id: 1, assignment: assignment, reviewer: participant, reviewee: team, reviewed_object_id: 1) }
  let(:review_response_map1) { build(:review_response_map, id: 2, assignment: assignment, reviewer: participant1, reviewee: team1, reviewed_object_id: 1) }
  let(:feedback) { FeedbackResponseMap.new(id: 1, reviewed_object_id: 1, reviewer_id: 1, reviewee_id: 1) }
  let(:participant) { build(:participant, id: 1, parent_id: 1, user: build(:student, parent_id: 1, name: 'no name', fullname: 'no one')) }
  let(:participant1) { build(:participant, id: 2, parent_id: 2, user: build(:student, parent_id: 1, name: 'has name', fullname: 'has one')) }
  let(:assignment) { build(:assignment, id: 1, name: 'Test Assgt', rounds_of_reviews: 2) }
  let(:assignment1) { build(:assignment, id: 2, name: 'Test Assgt', rounds_of_reviews: 1) }
  let(:response) { build(:response, id: 1, map_id: 1, round: 1, response_map: review_response_map,  is_submitted: true) }
  let(:response1) { build(:response, id: 2, map_id: 1, round: 2, response_map: review_response_map) }
  let(:response2) { build(:response, id: 3, map_id: 1, round: nil, response_map: review_response_map, is_submitted: true) }
  let(:metareview_response_map) { MetareviewResponseMap.new(reviewed_object_id: 1) }
  let(:student) { build(:student, id: 1, fullname: 'no one', email: 'expertiza@mailinator.com') }
  let(:questionnaire) { Questionnaire.new(id: 1, type: 'ReviewQuestionnaire') }
  let(:response_map) { ResponseMap.new(id: 1, reviewed_object_id: 1, reviewee_id: 1, reviewer_id: 1, type: "ReviewResponseMap", response: [response], calibrate_to: 0) }
  let(:user) { User.new(id: 1, name: "name", fullname: 'fullname') }
  let(:user1) { User.new(id: 2, name: "name1", fullname: 'fullname') }
  let(:assignment_participant) { AssignmentParticipant.new(user_id: 1, parent_id: 1) }
  let(:assignment_participant1) { AssignmentParticipant.new(id: 1, user_id: 2, parent_id: 1) }

Implementation

Write test for the methods in review_response_map.rb.

Questionnaire

Questionnaire method returns a questionnaire associated with an assignment.

  it '#questionnaire' do
    round = 1
    allow(assignment).to receive(:review_questionnaire_id).and_return(1)
    allow(Questionnaire).to receive(:find_by).with(id: 1).and_return(questionnaire)
    expect(review_response_map.questionnaire(1)).to eq(questionnaire)
  end

Get title

Get_title method returns the title.

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

Delete

Delete method deletes the feedback_response_map and metareview_response_map and return review_response_map.

  it '#delete' do
    allow(Response).to receive(:find).and_return(response)
    allow(FeedbackResponseMap).to receive(:where).with(reviewed_object_id: 1).and_return([feedback])
    allow(MetareviewResponseMap).to receive(:where).and_return([metareview_response_map])
    expect(review_response_map.delete).to equal(review_response_map)
  end

Export fields

Export_fields method which shows the title of export csv file, "contributor" and "reviewed by".

  it '#export_fields' do
    expect(ReviewResponseMap.export_fields(options)).to eq(["contributor", "reviewed by"])
  end

Export

Export method exports the name of reviewer and reviewee and return the map.

  it '#export' do
    csv = []
    parent_id = 1
    options = nil
    allow(ReviewResponseMap).to receive(:where).with(reviewed_object_id: 1).and_return([review_response_map, review_response_map1])
    expect(ReviewResponseMap.export(csv, parent_id, options)).to eq([review_response_map1, review_response_map])
  end

Import

Import method allows user to import the csv file and check it. After that, it returns the name of reviewers.

  it '#import' do
    row_hash = {reviewee: "name", reviewers: ["name1"]}
    session = nil
    assignment_id = 1
    # when reviewee user = nil
    allow(User).to receive(:find_by).and_return(nil)
    expect { ReviewResponseMap.import(row_hash, session, 1) }.to raise_error(ArgumentError, message = "Cannot find reviewee user.")
    # when reviewee user exists but reviewee user is not a participant in this assignment
    allow(User).to receive(:find_by).with(name: "name").and_return(user)
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: 1, parent_id: 1).and_return(nil)
    expect { ReviewResponseMap.import(row_hash, session, 1) }.to raise_error(ArgumentError, message = "Reviewee user is not a participant in this assignment.")
    # when reviewee user exists and reviewee user is a participant in this assignment
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: 1, parent_id: 1).and_return(assignment_participant)
    allow(AssignmentTeam).to receive(:team).with(assignment_participant).and_return(team)
    ## when reviewer user doesn't exist
    allow(User).to receive(:find_by).with(name: "name1").and_return(nil)
    expect { ReviewResponseMap.import(row_hash, session, 1) }.to raise_error(ArgumentError, message = "Cannot find reviewer user.")
    ## when reviewer user exist
    allow(User).to receive(:find_by).with(name: "name1").and_return(user1)
    ### when reviewer user is not a participant in this assignment.
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: 2, parent_id: 1).and_return(nil)
    expect { ReviewResponseMap.import(row_hash, session, 1) }.to raise_error(ArgumentError, message = "Reviewer user is not a participant in this assignment.")
    ### when reviewer user is a participant in this assignment.
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: 2, parent_id: 1).and_return(assignment_participant1)
    allow(ReviewResponseMap).to receive(:find_or_create_by).with(reviewed_object_id: 1, reviewer_id: 1, reviewee_id: 1, calibrate_to: false).and_return(review_response_map)
    expect(ReviewResponseMap.import(row_hash, session, 1)).to eq(["name1"])
    # when reviewee_team = nil
    allow(AssignmentTeam).to receive(:team).with(assignment_participant).and_return(nil)
    allow(AssignmentTeam).to receive(:create).and_return(double('team', id: 1))
    allow(TeamsUser).to receive(:create).with(team_id: 1, user_id: 1).and_return(double('teams_users', id: 1, team_id: 1, user_id: 1))
    allow(TeamNode).to receive(:create).with(parent_id: assignment_id, node_object_id: 1).and_return(double('team_node', id: 1, parent_id: 1, node_object_id: 1))
    allow(TeamUserNode).to receive(:create).with(parent_id: 1, node_object_id: 1).and_return(double('team_user_node', id: 1, parent_id: 1, node_object_id: 1))
    allow(User).to receive(:find_by).with(name: "name1").and_return(user1)
    allow(AssignmentParticipant).to receive(:find_by).with(user_id: 2, parent_id: 1).and_return(assignment_participant1)
    allow(ReviewResponseMap).to receive(:find_or_create_by).with(reviewed_object_id: 1, reviewer_id: 1, reviewee_id: 1, calibrate_to: false).and_return(review_response_map)
    expect(ReviewResponseMap.import(row_hash, session, 1)).to eq(["name1"])
  end

Show feedback

Show_feedback method returns the html associated with response.

    it '#show_feedback' do
      allow(review_response_map).to receive(:response).and_return([response])
      allow(Response).to receive(:find).and_return(response)
      allow(FeedbackResponseMap).to receive(:find_by).with(reviewed_object_id: 1).and_return(feedback)
      allow(feedback).to receive(:response).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

Metareview response maps

Metareview_response_maps method returns metareview_list associated with responses.

  it '#metareview_response_maps' do
    allow(Response).to receive(:where).with(map_id: 1).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

Get responses for team round

Get_responses_for_team_round method returns the responses associated with team and round.

  it '#get_responses_for_team_round' do
    allow(Team).to receive(:find).and_return(team)
    round = 1
    allow(ResponseMap).to receive(:where).with(reviewee_id: team.id, type: "ReviewResponseMap").and_return([response_map])
    expect(ReviewResponseMap.get_responses_for_team_round(team, 1)).to eq([response])
  end

Final versions from reviewer

Final_versions_from_reviewer method returns the questionnaire and response ids associated with reviewer.

  it '#final_versions_from_reviewer' do
    reviewer_id = 1
    allow(ReviewResponseMap).to receive(:where).with(reviewer_id: 1).and_return([review_response_map])
    allow(Participant).to receive(:find).with(1).and_return(participant)
    allow(participant).to receive(:parent_id).and_return(1)
    allow(Assignment).to receive(:find).with(1).and_return(assignment)
    allow(Response).to receive(:where).with(map_id: 1, round: 1).and_return([response])
    allow(assignment).to receive(:review_questionnaire_id).with(1).and_return(1)
    allow(Response).to receive(:where).with(map_id: 1, round: 2).and_return([response1])
    allow(assignment).to receive(:review_questionnaire_id).with(2).and_return(1)
    expect(ReviewResponseMap.final_versions_from_reviewer(1)).to eq("review round1": {questionnaire_id: 1, response_ids: [1]}, "review round2": {questionnaire_id: 1, response_ids: [2]})
  end

Review response report

Review_response_report method returns the participants associated with assignment.

  it '#review_response_report' do
    id = 1
    type = "MetareviewResponseMap"
    reviewer_id = 1
    user_ids = []

When review user exists.

    review_user = user
    allow(Participant).to receive(:find).with(1).and_return(participant)
    allow(Assignment).to receive(:find).with(1).and_return(assignment)
    allow(User).to receive_message_chain(:select, :where).and_return([user])
    allow(AssignmentParticipant).to receive(:where).and_return([assignment_participant])
    expect(ReviewResponseMap.review_response_report(id, Assignment.find(Participant.find(reviewer_id).parent_id), type, review_user)).to eq([assignment_participant])

When review user doesn't exists.

    review_user = nil
    allow(ResponseMap).to receive_message_chain(:select, :where).and_return([response_map])
    allow([response_map]).to receive(:reviewer_id).and_return(1)
    allow(AssignmentParticipant).to receive(:find).with(1).and_return([assignment_participant])
    allow(Participant).to receive(:sort_by_name).and_return([assignment_participant])
    expect(ReviewResponseMap.review_response_report(id, Assignment.find(Participant.find(reviewer_id).parent_id), type, review_user)).to eq([assignment_participant])
  end

Email

Email method can successfully send an email.

  it '#email' do
    reviewer_id = 1
    allow(Participant).to receive(:find).with(1).and_return(participant)
    allow(Assignment).to receive(:find).with(1).and_return(assignment)
    allow(AssignmentTeam).to receive(:find).with(1).and_return(team)
    allow(AssignmentTeam).to receive(:users).and_return(student)
    allow(User).to receive(:find).with(1).and_return(student)
    review_response_map.reviewee_id = 1
    defn = {body: {type: "Peer Review", obj_name: "Test Assgt", first_name: "no one", partial_name: "new_submission"}, to: "expertiza@mailinator.com"}
    expect { review_response_map.email(defn, participant, Assignment.find(Participant.find(reviewer_id).parent_id)) }.to change { ActionMailer::Base.deliveries.count }.by (1)
  end

Prepare final review versions

Prepare_final_review_versions method returns the final review versions.

  it '#prepare_final_review_versions' do
    review_final_versions = {}

When round exists.

    reviewer_id = 1
    allow(metareview_response_map).to receive(:id).and_return(1)
    allow(Participant).to receive(:find).with(1).and_return(participant)
    allow(Assignment).to receive(:find).with(1).and_return(assignment)
    allow(MetareviewResponseMap).to receive(:where).with(reviewed_object_id: 1).and_return([metareview_response_map])
    allow(Response).to receive(:where).with(map_id: 1, round: 1).and_return([response])
    allow(assignment).to receive(:review_questionnaire_id).with(1).and_return(1)
    allow(Response).to receive(:where).with(map_id: 1, round: 2).and_return([response1])
    allow(assignment).to receive(:review_questionnaire_id).with(2).and_return(1)
    expect(ReviewResponseMap.prepare_final_review_versions(Assignment.find(Participant.find(reviewer_id).parent_id), MetareviewResponseMap.where(reviewed_object_id: 1))).to eq("review round1": {questionnaire_id: 1, response_ids: [1]}, "review round2": {questionnaire_id: 1, response_ids: [2]})

When round = nil.

    reviewer_id = 2
    allow(Participant).to receive(:find).with(2).and_return(participant1)
    allow(Assignment).to receive(:find).with(2).and_return(assignment1)
    allow(MetareviewResponseMap).to receive(:where).with(reviewed_object_id: 1).and_return([metareview_response_map])
    allow(assignment).to receive(:review_questionnaire_id).with(nil).and_return(1)
    allow(Response).to receive(:where).with(map_id: 1).and_return([response2])
    expect(ReviewResponseMap.prepare_final_review_versions(Assignment.find(Participant.find(reviewer_id).parent_id), MetareviewResponseMap.where(reviewed_object_id: 1))).to eq(review: {questionnaire_id: nil, response_ids: [3]})
  end

Prepare review response

Prepare_review_response method returns the response id associated with assignment.

  it '#prepare_review_response' do
    review_final_versions = {}
    review_response_map.id = 1
    round = 1
    maps = [review_response_map]
    allow(Assignment).to receive(:find).with(1).and_return(assignment)
    allow(Response).to receive(:where).with(map_id: 1, round: 1).and_return([response])
    expect(ReviewResponseMap.prepare_review_response(assignment, maps, review_final_versions, round)).to eq([1])
    round = nil
    allow(Assignment).to receive(:find).with(1).and_return(assignment)
    allow(Response).to receive(:where).with(map_id: 1).and_return([response2])
    expect(ReviewResponseMap.prepare_review_response(assignment, maps, review_final_versions, round)).to eq([3])
  end

Running Rspec

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

 rspec spec/models/review_response_map_spec.rb

Unit Test Result

app/models/review_response_map.rb
100.0 % covered
102 relevant lines. 102 lines covered and 0 lines missed.

Full video for this test can be found at https://drive.google.com/file/d/18vr3q2dCyh3_tsFLH8w9y6Y9uhdz0Ljj/view?usp=sharing

External Links

https://github.com/Sauve-moi/expertiza