CSC/ECE 517 Fall 2018/E1852 Unit Test for Participant Model
Test Cases
TASKS IDENTIFIED
Test team
A Participant can be a member of a Team. When team method is called on a Participant, it should return the Team it belongs to.
describe "#team" do
  it "returns the team id for the user" do
    expect(participant.team).to eq(nil)
  end   
end
Test responses
   def responses
    response_maps.map(&:response)
   end
A Participant has many ResponseMaps that map a connection between this participant as a reviewer and as a reviewee. Each ResponseMap has many Responses associated with it. When responses method is called on a Participant, it should return an array of responses associated to this Participant.
 describe '#responses' do
     it 'returns an array of responses of the participant' do
       expect(participant.responses).to eql([])
     end
end
The above test will test a participant with no corresponding mapping. Hence the result of the responses provided by the participant is a nil list
Test name
 def name(ip_address = nil)
    self.user.name(ip_address)
end
 describe "#name" do
    it "returns the name of the user" do
      expect(participant.name).to eq "Student"
    end
end
A Participant is a User. When name method is called on a Participant, it should return the name of this User.
Link title
Test fullname
  def fullname(ip_address = nil)
    self.user.fullname(ip_address)
end
A Participant is a User. When fullname method is called on a Participant, it should return the full name of this User.
Test handle
  def handle(ip_address = nil)
    User.anonymized_view?(ip_address) ? 'handle' : self[:handle]
end
Test delete
  def delete(force = nil)
    maps = ResponseMap.where('reviewee_id = ? or reviewer_id = ?', self.id, self.id)
    if force or (maps.blank? and self.team.nil?)
      force_delete(maps)
    else
      raise "Associations exist for this participant."
    end
end
Test force_delete
 
 def force_delete(maps)
    maps and maps.each(&:destroy)
    if self.team and self.team.teams_users.length == 1
      self.team.delete
    elsif self.team
      self.team.teams_users.each {|teams_user| teams_user.destroy if teams_user.user_id == self.id }
    end
    self.destroy
end
Test topic_name
 
 describe '#topic_name' do     
     context 'when the participant has an assignment without a topic' do
       it 'returns error message' do
         expect(participant.topic_name).to eql('<center>—</center>')
       end
end
Test able_to_review
def able_to_review can_review end
When able_to_review method is called on a Participant, it should return true if it can review and false otherwise.
Test email
  def email(pw, home_page)
    user = User.find_by(id: self.user_id)
    assignment = Assignment.find_by(id: self.assignment_id)
    Mailer.sync_message(
      recipients: user.email,
      subject: "You have been registered as a participant in the Assignment #{assignment.name}",
      body: {
        home_page: home_page,
        first_name: ApplicationHelper.get_user_first_name(user),
        name: user.name,
        password: pw,
        partial_name: "register"
      }
    ).deliver
end
Test scores
def scores(questions)
    scores = {}
    scores[:participant] = self
    self.assignment.questionnaires.each do |questionnaire|
      round = AssignmentQuestionnaire.find_by(assignment_id: self.assignment.id, questionnaire_id: questionnaire.id).used_in_round
      questionnaire_symbol = if round
                               (questionnaire.symbol.to_s + round.to_s).to_sym
                             else
                               questionnaire.symbol
                             end
      scores[questionnaire_symbol] = {}
      scores[questionnaire_symbol][:assessments] = questionnaire.get_assessments_for(self)
      scores[questionnaire_symbol][:scores] = Answer.compute_scores(scores[questionnaire_symbol][:assessments], questions[questionnaire_symbol])
    end
    scores[:total_score] = assignment.compute_total_score(scores)
    scores
end
When scores method is called on a Participant, it should return the total scores it received for a given number of questions.
Test get_permissions
def self.get_permissions(authorization)
    can_submit = true
    can_review = true
    can_take_quiz = true
    case authorization
    when 'reader'
      can_submit = false
    when 'reviewer'
      can_submit = false
      can_take_quiz = false
    when 'submitter'
      can_review = false
      can_take_quiz = false
    end
    {can_submit: can_submit, can_review: can_review, can_take_quiz: can_take_quiz}
end
Test get_authorization
 def self.get_authorization(can_submit, can_review, can_take_quiz)
    authorization = 'participant'
    authorization = 'reader' if can_submit == false and can_review == true and can_take_quiz == true
    authorization = 'submitter' if can_submit == true and can_review == false and can_take_quiz == false
    authorization = 'reviewer' if can_submit == false and can_review == true and can_take_quiz == false
    authorization
end
Test sort_by_name
def self.sort_by_name(participants)
    users = []
    participants.each {|p| users << p.user }
    users.sort! {|a, b| a.name.downcase <=> b.name.downcase } # Sort the users based on the name
    participants.sort_by {|p| users.map(&:id).index(p.user_id) }
end
Participants are Users. When self.sort_by_name method is called on a Participant, it should sort a given set of participants based on their usernames.
Steps for Implementation
Plan of Work
The task is to write unit test cases for testing the participant model file. The Rspec file participant_spec.rb existed with test cases for name and fullname methods. The procedure followed :-
1. Setting up the Expertiza environment
2. Understand the functionality of model file in participant.rb
3. Understand the linked data attributes being used, like assignment, topic, questionnaire
4. Writing testing conditions for different functions.