CSC/ECE 517 Fall 2017/E1769 Refactor assignment form.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 123: Line 123:
After:
After:
   log = Version.find_by(item_type: "Delayed::Backend::ActiveRecord::Job", item_id: delayed_job_id)
   log = Version.find_by(item_type: "Delayed::Backend::ActiveRecord::Job", item_id: delayed_job_id)
=== Avoid more than 3 levels of block nesting.===
  if qTypeHash.fetch(questionnaireType, {}).fetch(courseId, nil).nil?
    if qTypeHash.fetch(questionnaireType, nil).nil?
        ...
    end
    ...
  end
This is the third and fourth level of the block nesting. We need to merge them together.
  if q_type_hash.fetch(questionnaire_type, {}).fetch(course_id, nil).nil? && q_type_hash.fetch(questionnaire_type, nil).nil?
    ...
  elseif  q_type_hash.fetch(questionnaire_type, {}).fetch(course_id, nil).nil? && !q_type_hash.fetch(questionnaire_type, nil).nil?
    ...
  end
Then the 4-levels loop has been changed to 3-levels one.


== Rspec Test ==
== Rspec Test ==

Revision as of 15:23, 27 October 2017

This page give a description of the changes made for the assignment_form.rb and related files of Expertiza based OSS project.

Expertiza Background

Expertiza is a web application where students can submit and peer-review learning objects (articles, codes, websites, etc). Instructors add and edit assignments to Expertiza. Students can be assigned in teams based on their selection of the topics.

Problem Statement

The task of the project is to refactor assignment_form.rb and write unit tests for it. assignment_form.rb is a fairly complex file. It contains a lot of methods that are long and hard to understand. These methods need to be broken down into simpler and more specific methods that are easier to read/understand. Also, the few instances of code duplication that exist should be removed.

  • Refactor `add_to_delayed_queue` method
  • Rename method `is_calibrated` to `calibrated?` and all other places it is used
  • Use `find_by` instead of `where.first`
  • Complete pending tests in sign_up_sheet_controller.rb

Modified File

  • Refactor the assignment_form.rb
  • Refactor the assignment.rb
  • Complete relating tests in assignment_form_rspec.rb and assignment.rb
  • Complete tests in sign_up_sheet_controller_rspec.rb

Thoughts

Tools

Refactor

Refactor `add_to_delayed_queue` method

`add_to_delayed_queue` is long method with duplicate codes. So we split it into three methods, name the added two as `get_diff_from_now` and `add_delayed_job`. For example, we changed this method.

Original Code:

 # Adds items to delayed_jobs queue for this assignment
 def add_to_delayed_queue
   duedates = AssignmentDueDate.where(parent_id: @assignment.id)
   duedates.each do |due_date|
     deadline_type = DeadlineType.find(due_date.deadline_type_id).name
     due_at = due_date.due_at.to_s(:db)
     Time.parse(due_at)
     due_at = Time.parse(due_at)
     mi = find_min_from_now(due_at)
     diff = mi - due_date.threshold * 60
     next unless diff > 0
     dj = DelayedJob.enqueue(DelayedMailer.new(@assignment.id, deadline_type, due_date.due_at.to_s(:db)),
                             1, diff.minutes.from_now)
     change_item_type(dj.id)
     due_date.update_attribute(:delayed_job_id, dj.id)
     # If the deadline type is review, add a delayed job to drop outstanding review
     if deadline_type == "review"
       dj = DelayedJob.enqueue(DelayedMailer.new(@assignment.id, "drop_outstanding_reviews", due_date.due_at.to_s(:db)),
                               1, mi.minutes.from_now)
       change_item_type(dj.id)
     end
     # If the deadline type is team_formation, add a delayed job to drop one member team
     next unless deadline_type == "team_formation" and @assignment.team_assignment?
     dj = DelayedJob.enqueue(DelayedMailer.new(@assignment.id, "drop_one_member_topics", due_date.due_at.to_s(:db)),
                             1, mi.minutes.from_now)
     change_item_type(dj.id)
   end
 endBoth variable names and method names are not in a good manner. We changed it to the version below.

Modified Code:

 # Adds items to delayed_jobs queue for this assignment
 def add_to_delayed_queue
   duedates = AssignmentDueDate.where(parent_id: @assignment.id)
   duedates.each do |due_date|
     deadline_type = DeadlineType.find(due_date.deadline_type_id).name
     diff, mi = get_diff_from_now(due_date)
     next unless diff > 0
     dj = add_delayed_job(@assignment, deadline_type, due_date, diff)
     due_date.update_attribute(:delayed_job_id, dj.id)
     # If the deadline type is review, add a delayed job to drop outstanding review
     if deadline_type == "review"
       add_delayed_job(@assignment, "drop_outstanding_reviews", due_date, mi)
     end
     # If the deadline type is team_formation, add a delayed job to drop one member team
     next unless deadline_type == "team_formation" and @assignment.team_assignment?
     add_delayed_job(@assignment, "drop_one_member_topics", due_date, mi)
   end
 end
 # get time difference between due_date and now
 def get_diff_from_now(due_date)
   due_at = due_date.due_at.to_s(:db)
   Time.parse(due_at)
   due_at = Time.parse(due_at)
   mi = find_min_from_now(due_at)
   diff = mi - due_date.threshold * 60
   [diff, mi]
 end
 # add DelayedJob into queue and return it
 def add_delayed_job(assignment, deadline_type, due_date, diff)
   dj = DelayedJob.enqueue(DelayedMailer.new(assignment.id, deadline_type, due_date.due_at.to_s(:db)),
                           1, diff.minutes.from_now)
   change_item_type(dj.id)
   dj
 end

Rename method 'is_calibrated?' to 'calibrated?' and all other places it is used

We refactorer this method by using 'Refactor' operation in RubyMine. We renamed it to 'calibrated?' and RubyMine found all place it appeared then changed them.

Before:

 def is_calibrated?
   self.is_calibrated
 end

After:

 def calibrated?
   self.is_calibrated
 end

Use 'find_by' instead of 'where.first'

If we use 'where' and then use 'first' method to return the first record matches the condition, the application will find all eligable records and return the first one. However, 'find_by' method will return a record once it find one and then terminate itself. Therefore, conpared with 'where.first' method, 'find_by' is much more efficient. That's why we use 'find_by' instead of 'where.first'.

Before:

 log = Version.where(item_type: "Delayed::Backend::ActiveRecord::Job", item_id: delayed_job_id).first

After:

 log = Version.find_by(item_type: "Delayed::Backend::ActiveRecord::Job", item_id: delayed_job_id)

Rspec Test

We write rspec test for relating methods in assignment_form_spec.rb. We test `add_to_delayed_queue` method

   describe '#add_to_delayed_queue' do
   before(:each) do
     future_due = build(:assignment_due_date, due_at: Time.now.utc.advance(weeks: 1))
     allow(AssignmentDueDate).to receive_message_chain(:where).and_return([future_due])
     allow_any_instance_of(AssignmentDueDate).to receive(:update_attribute)
   end
   context 'when the deadline type is review' do
     it 'adds two delayed jobs and changes the # of DelayedJob by 2' do
       allow(DeadlineType).to receive_message_chain(:find, :name).and_return("review")
       num_delayed_job = DelayedJob.count
       assignment_form.add_to_delayed_queue
       expect(DelayedJob.count).to eq(num_delayed_job + 2)
     end
   end
   context 'when the deadline type is team formation and current assignment is team-based assignment' do
     it 'adds a delayed job and changes the # of DelayedJob by 2' do
       allow(DeadlineType).to receive_message_chain(:find, :name).and_return("team_formation")
       num_delayed_job = DelayedJob.count
       assignment_form.add_to_delayed_queue
       expect(DelayedJob.count).to eq(num_delayed_job + 2)
     end
   end
 end

Next we test `change_item_type` method

 describe '#change_item_type' do
   let(:log) { double(Version) }
   it 'changes the item_type displayes in the log' do
     allow(Version).to receive(:find_by).with(any_args).and_return(log)
     allow(log).to receive(:update_attribute).with(any_args)
     expect(log).to receive(:update_attribute).with(:item_type, String)
     assignment_form.change_item_type(1)
   end
 end

We tested the methods by seeing whether if we got the right amount of elements in our arrays/hashes like the following.

 it "getAssignmentsInCourses should return an assignment" do
   expect(Leaderboard.get_assignments_in_courses(1)).to have(1).items
 end

We also tested whether the code could handle invalid arguments like such.

 it "leaderboard_heading should return No Entry with invalid input" do    
   expect(Leaderboard.leaderboard_heading("Invalid")).to eq("No Entry")
 end

We did face situations where methods were being called that were not defined anywhere so we had to use stubs to imitate their expected behavior.

 it "leaderboard_heading should return name" do
   allow(Leaderboard).to receive(:find_by_qtype).and_return(@questionnaire).with(@questionnaire.id)
   expect(Leaderboard.leaderboard_heading(@questionnaire.id)).to eq("Test questionaire")
 end

Additional

To explain our work in a further step, we did a video on Youtube to show the broken leaderboard page, why we did work in this way and our work for the unit tests and the refactor part.

There are still some problems we need to figure out. For the code revising part, there are some methods with this problem: Assignment Branch Condition size for get_participants_score is too high.

I think we cannot make too many functions in one method so that we need to make some of these be executed outside of this method. Also, some of the code lines are too long so that it is not clear enough.

During the rspec testing we noticed that the methods get_participant_entries_in_assignment_list and find_by_qtype were being called but where not created in the program. To cover these tests we had to use stubs for these methods for the expected outputs.

There was also no declaration of ScoreCache anywhere in the program which caused other methods to fail. Trying to stub this was a problem since we were not sure of how it was laid out.

References