E2422. Reimplement questionnaire.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(48 intermediate revisions by 3 users not shown)
Line 49: Line 49:
   end
   end


I changed the validate_questionnaire from a method that needs to be called for validation into individual validates in the order I expected so they could each have single responsibility.
The validate_questionnaire method was changed in order to follow the Single Responsibility Principle in the SOLID guidelines. Before refactoring, validate_questionnaire was responsible for checking max, min, and if the min was less than max. So, after refactoring, the methods are split up to accomplish one check in each method. This also helps the readability of the code as the checks are very clear.


=== get_weighted_score ===
=== get_weighted_score ===
Line 93: Line 93:
   end
   end


I changed the two methods into two public methods and one private method.  Since the original get_weighted_score got the score and looked up the symbol to use I felt it violated the single use principle.  I created a private method to do the lookup of the symbol to use on its own.  I created an elegant line of code to attempt to find an assignment questionnaire and if not null and used in a round it yields the result to be used in the return value.  If not then it will just use this symbol.  I had this line but without the yield as I was attempting to get it doing what I wanted it to do.  I asked chatGPT and it presented me with the yield statement which I then used. Now each method only does what it should, gets the results, computes the result, or evaluates the symbol. 
I decided to change this into two public methods and one private method.  Since the original get_weighted_score got the score and looked up the symbol to use I felt it violated the single use principle.  I created a private method to do the lookup of the symbol to use on its own.  I created an elegant line of code to attempt to find an assignment questionnaire and if not null and used in a round it yields the result to be used in the return value.  If not then it will just use this symbol.  I had this line but without the yield as I was attempting to get it doing what I wanted it to do.  I asked chatGPT and it presented me with the yield statement which I then used. Now each method only does what it should, gets the results, computes the result, or evaluates the symbol. 


=== true_false_questions? ===
=== true_false_questions? ===
Line 103: Line 103:
   end
   end


This method was doing the job very succinctly already. I no direct changes to this method and left it as is.
This method was doing the job very succinctly already. No direct changes were made to this method and the method was left as is.


=== delete ===  
=== delete ===  
Line 134: Line 134:
   end
   end


I only changed a few pieces of this method.  First of all there was no reason to do an each on the list if the first one was going to throw an exception already, so I simplified that.  Also just doing a null check on the dereference to the destroy on questionnaire node was more succinct than doing an if.
There was no reason to do an each on the list if the first one was going to throw an exception already so that was altered to just get first.  Also just doing a null check on the dereference to the destroy on questionnaire node was more succinct than doing an if.


=== max_possible_score ===
=== max_possible_score ===
Line 156: Line 156:
   end
   end


This refactoring is similar to [[ https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2023_-_E2319._Reimplement_questionnaire.rb | E2419.]]
This refactoring is similar to [https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2023_-_E2319._Reimplement_questionnaire.rb E2319. Reimplement questionnaire.rb]. Instead of using the complicated SQL query, this should be easier to understand and is rather straightforward. Additionally, comments were added to improve the readability of the code. Adding the error check at the beginning should make the code slightly more robust in the case of questionnaires with no questions, and the variable names have been changed for clarity.


=== copy_questionnaire_details ===
=== copy_questionnaire_details ===
Line 199: Line 199:
   end
   end


I have removed the unnecessary assignment of questions variable since we can directly access questions associated with the original questionnaire. I used string interpolation, instead of concatenation, for setting the name of the copied questionnaire. I wrapped the copying process in a transaction to ensure data consistency. Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action.
This method handles copying a questionnaire and its attributes and dependencies in the model structure. We find the questionnaire to copy from the database and assign it to orig_questionnaire. In the original code, they used a questions variable to access different questions associated with the original questionnaire. However, we can directly access the questions associated with a questionnaire with how the models are set up. So, it has been eliminated when reimplementing the method. String interpolation was used instead of string concatenation to improve readability and simplicity. It is also less error-prone when compared to concatenation. A big change in the implementation of this method is wrapping the copying process in a transaction. This is done to ensure data consistency. Active Record Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action. (Ref. https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html)  We started an ActiveRecord transaction to save this new questionnaire and to duplicate the questions associated with the original questionnaire.


== Required migrations ==
== Required migrations ==
Line 456: Line 456:
   end
   end


=== Questionnaire model ===
----


Some of the variables that we will need in all or nearly all tests we have declared at the top within the whole describe for the Questionnaire class tests.
[[File:Rspec-header.png]]


=== Validate(s) ===
=== Validate(s) ===
----
----
  describe '#name' do
The following list of tests are to assert all the validations for the class. Using the predefined factory variables we test validations.
    # Test validates the name of the questionnaire
Are all names what we expect. Validate we cannot have a valid questionnaire without a name. Create two questionnaires with the same name and instructor to ensure we cannot save the second one. Also validate that the questionnaire returns the assigned instructors id.
    it 'returns the name of the Questionnaire' do
 
      # Act Assert
 
      expect(questionnaire.name).to eq('abc')
[[File:Validation 1.png]]</br></br>
      expect(questionnaire1.name).to eq('xyz')
 
      expect(questionnaire3.name).to eq('pqr')
There are a few tests to run which can help us test the maximum_score method. We can make sure it matches what is expected, we can assert that alpha characters and floats are not valid. We can follow that up with tests to ensure it is a positive integer, and that the max is larger than the minimum.
    end
 
    # Test ensures that the name field of the questionnaire is not blank
 
    it 'Validate presence of name which cannot be blank' do
[[File:Validation 2.png]]</br></br>
      # Arrange
 
      questionnaire.name = '  '
The final validations are minimum score and associations. The few tests for minimum score are to check that it is what it should be, that it is positive, and cannot be an alpha character.
      # Act Assert
 
      expect(questionnaire).not_to be_valid
 
    end
[[File:Validation 3.png]]</br></br>
    # Test ensures that the name field of the questionnaire is unique per instructor
    it 'Validate name field must be unique per instructor' do
      # Arrange Act
      questionnaire.save!
      questionnaire1.name = questionnaire.name
      questionnaire3.name = questionnaire.name
      instructor2 = Instructor.create(name: 'testinstructortwo', email: 'test2@test.com', full_name: 'Test Instructor 2', password: '123456', role: role)
      instructor2.save!
      questionnaire3.instructor_id = instructor2.id
      # Assert
      expect(questionnaire).to be_valid
      expect(questionnaire1).not_to be_valid
      expect(questionnaire3).to be_valid
    end
  end
  describe '#instructor_id' do
    # Test validates the instructor id in the questionnaire
    it 'returns the instructor id' do
      expect(questionnaire.instructor_id).to eq(instructor.id)
    end
  end
  describe '#maximum_score' do
    # Test validates the maximum score in the questionnaire
    it 'validate maximum score' do
      expect(questionnaire.max_question_score).to eq(10)
    end
    # Test ensures maximum score is an integer
    it 'validate maximum score is integer' do
      expect(questionnaire.max_question_score).to eq(10)
      questionnaire.max_question_score = 'a'
      expect(questionnaire).not_to be_valid
      questionnaire.max_question_score = 1.1
      expect(questionnaire).not_to be_valid
    end
    # Test ensures maximum score is positive
    it 'validate maximum score should be positive' do
      expect(questionnaire.max_question_score).to eq(10)
      questionnaire.max_question_score = -10
      expect(questionnaire).not_to be_valid
      questionnaire.max_question_score = 0
      expect(questionnaire).not_to be_valid
    end
    # Test ensures maximum score is greater than the minimum score
    it 'validate maximum score should be bigger than minimum score' do
      expect(questionnaire).to be_valid
      questionnaire.max_question_score = 0
      expect(questionnaire).not_to be_valid
      questionnaire.max_question_score = 2
      questionnaire.min_question_score = 3
      expect(questionnaire).not_to be_valid
    end
  end
  describe '#minimum_score' do
    # Test validates minimum score of a questionnaire
    it 'validate minimum score' do
      questionnaire.min_question_score = 5
      expect(questionnaire.min_question_score).to eq(5)
    end
    # Test ensures minimum score is smaller than maximum score
    it 'validate minimum should be smaller than maximum' do
      expect(questionnaire.min_question_score).to eq(0)
      questionnaire.min_question_score = 10
      expect(questionnaire).not_to be_valid
      questionnaire.min_question_score = 0
    end
    # Test ensures minimum score is an integer
    it 'validate minimum score is integer' do
      expect(questionnaire.min_question_score).to eq(0)
      questionnaire.min_question_score = 'a'
      expect(questionnaire).not_to be_valid
    end
  end
  describe 'associations' do
    # Test validates the association that a questionnaire comprises of several questions
    it 'has many questions' do
      expect(questionnaire.questions).to include(question1, question2)
    end
  end


=== get_weighted_score ===
=== get_weighted_score ===
Line 620: Line 547:
=== true_false_questions? ===
=== true_false_questions? ===
----
----
  describe "#true_false_questions?" do
 
    before :each do
We decided to use before each to setup the questionnaire that we can use for the duration of this describe. For these the test the skeleton provided was used and the scenarios felt like complete tests. Testing both a single and multiple valid checkbox questions assert to true. We also want to validate that single or multiple non checkbox questions assert to false.
      @questionnaire = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
 
    end
 
    context "when there are true/false questions" do
[[File:True-false-rspec.png]]
      it "returns true" do
        # Test scenario 2: Single question with type 'Checkbox'
        # Arrange
        @questionnaire.questions.create(weight: 1, id: 1, seq: 1, txt: "que 1", question_type: "Checkbox", break_before: true)
        # Act Assert
        expect(@questionnaire.true_false_questions?).to eq(true)
        # Test scenario 1: Multiple questions with type 'Checkbox'
        # Arrange
        @questionnaire.questions.create(weight: 1, id: 2, seq: 1, txt: "que 1", question_type: "Checkbox", break_before: true)
        @questionnaire.questions.create(weight: 1, id: 3, seq: 1, txt: "que 1", question_type: "Checkbox", break_before: true)
        # Act Assert
        expect(@questionnaire.true_false_questions?).to eq(true)
      end
    end
    context "when there are no true/false questions" do
      it "returns false" do
        # Test scenario 2: Single question with no 'Checkbox' type
        # Arrange
        @questionnaire.questions.create(weight: 1, id: 1, seq: 1, txt: "que 1", question_type: "Scale", break_before: true)
        # Act Assert
        expect(@questionnaire.true_false_questions?).to eq(false)
        # Test scenario 1: Multiple questions with no 'Checkbox' type
        # Arrange
        @questionnaire.questions.create(weight: 1, id: 2, seq: 1, txt: "que 1", question_type: "Scale", break_before: true)
        @questionnaire.questions.create(weight: 1, id: 3, seq: 1, txt: "que 1", question_type: "Scale", break_before: true)
        # Act Assert
        expect(@questionnaire.true_false_questions?).to eq(false)
      end
    end
  end


=== delete ===  
=== delete ===  
----
----
  describe "#delete" do
 
    context "when there are assignments using the questionnaire" do
[[File:Delete-rspec-1.png]]</br></br>
      it "raises an error with a message asking if the user wants to delete the assignment" do
 
        # Arrange
It is important to test that deleting a questionnaire also deletes all the questions associated with that questionnaire. In this case we test two situations, first we need to test just one question on the questionnaire, and then multiple questions on the questionnaire.
        questionnaire_single_assignment = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
 
        single_assignment = FactoryBot.create(:assignment)
 
        FactoryBot.create(:assignment_questionnaire, { assignment_id: single_assignment.id, questionnaire_id: questionnaire_single_assignment.id})
[[File:Delete-rspec-2.png]]</br></br>
        questionnaire_multi_assignment = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
 
        multi_assignment1 = FactoryBot.create(:assignment)
For the last parts of delete testing I used 2 more of the test skeletons. There are two scenarios to test in relation to the question node associated to a questionnaire.  We should assert that questions are deleted with and without an associated node, and that the questionnaire node does not exist any longer in the case where there was an associated node. 
        FactoryBot.create(:assignment_questionnaire, { assignment_id: multi_assignment1.id, questionnaire_id: questionnaire_multi_assignment.id})
 
        multi_assignment2 = FactoryBot.create(:assignment)
 
        FactoryBot.create(:assignment_questionnaire, { assignment_id: multi_assignment2.id, questionnaire_id: questionnaire_multi_assignment.id})
[[File:Delete-rspec-3.png]]</br></br>
        # Test scenario 1
        # Given: There are assignments using the questionnaire
        # When: The delete method is called
        # Then: An error is raised with a message asking if the user wants to delete the assignment
        expect { questionnaire_single_assignment.delete }.to raise_exception(RuntimeError) do |error|
          expect(error.message).to match(/^The assignment .* uses this questionnaire/)
        end
        # Test scenario 2
        assignment1_pattern = /^The assignment #{Regexp.escape(multi_assignment1.name)} uses this questionnaire/
        assignment2_pattern = /^The assignment #{Regexp.escape(multi_assignment2.name)} uses this questionnaire/
        # Given: There are multiple assignments using the questionnaire
        # When: The delete method is called
        # Then: An error is raised for each assignment with a message asking if the user wants to delete the assignment
        expect { questionnaire_multi_assignment.delete }.to raise_exception(RuntimeError) do |error|
          expect(error.message).to match(assignment1_pattern)
        end
        multi_assignment1.destroy!
        expect { questionnaire_multi_assignment.delete }.to raise_exception(RuntimeError) do |error|
          expect(error.message).to match(assignment2_pattern)
        end
      end
    end
    context "when there are no assignments using the questionnaire" do
      it "deletes all the questions associated with the questionnaire" do
        # Test scenario 1
        # Given: There are no assignments using the questionnaire
        # When: The delete method is called
        # Then: All the questions associated with the questionnaire are deleted
        # Arrange
        questionnaire_one_question = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
        question1 = questionnaire_one_question.questions.build(weight: 1, id: 1, seq: 1, txt: "que 1", question_type: "Scale", break_before: true)
        question1.save!
        # Act
        questionnaire_one_question.delete
        # Assert
        expect(Question.find_by(id: question1.id)).to be_nil
        # Test scenario 2
        # Given: There are no assignments using the questionnaire and there are multiple questions
        # When: The delete method is called
        # Then: All the questions associated with the questionnaire are deleted
        # Arrange
        questionnaire_multi_question = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
        question1 = questionnaire_multi_question.questions.build(weight: 1, id: 1, seq: 1, txt: "que 1", question_type: "Scale", break_before: true)
        question1.save!
        question2 = questionnaire_multi_question.questions.build(weight: 1, id: 2, seq: 1, txt: "que 1", question_type: "Scale", break_before: true)
        question2.save!
        # Act
        questionnaire_multi_question.delete
        # Assert
        expect(Question.find_by(id: question1.id)).to be_nil
        expect(Question.find_by(id: question2.id)).to be_nil
      end
      it "deletes the questionnaire node if it exists" do
        # Test scenario 1
        # Given: There are no assignments using the questionnaire and the questionnaire node exists
        # When: The delete method is called
        # Then: The questionnaire node is deleted
        questionnaire = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
        q_node = QuestionnaireNode.create(parent_id: 0, node_object_id: questionnaire.id, type: 'QuestionnaireNode')
        # Act
        questionnaire.delete
        # Assert
        expect(QuestionnaireNode.find_by(id: q_node.id)).to be_nil
        # Test scenario 2
        # Given: There are no assignments using the questionnaire and the questionnaire node does not exist
        # When: The delete method is called
        # Then: No error is raised and the method completes successfully
        questionnaire = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
        # Act Assert
        expect { questionnaire.delete }.not_to raise_error
        expect(Questionnaire.find_by(id: questionnaire.id)).to be_nil
      end


=== copy_questionnaire_details ===  
=== copy_questionnaire_details ===  
----
----
describe '.copy_questionnaire_details' do
    # Test ensures calls from the method copy_questionnaire_details
    it 'allowing calls from copy_questionnaire_details' do
      allow(Questionnaire).to receive(:find).with('1').and_return(questionnaire)
      allow(Question).to receive(:where).with(questionnaire_id: '1').and_return([Question])
    end


    # Test ensures creation of a copy of given questionnaire
[[File:Copy_questionnaire_test.png]]<br><br>
    # Combined two tests into one to avoid performing the same functionalities again to test another feature.
The tests for this method were implemented correctly and they covered most of the functionalities of this particular function.
    it 'creates a copy of the questionnaire' do
 
      instructor.save!
The first set of tests checks the method calls from copy_questionnaire_details. The first line stubs the 'find' method of the Questionnaire model. The second line stubs the 'where' method of the Questionnaire model. The first test stubs the find method to return a mock 'questionnaire' object when called with argument 1. The second test looks for an array containing a 'question' object when called with questionnaire ID as an argument.  
      questionnaire.save!
 
      question1.save!
The second set of tests checks the copying process of the copy_questionnaire_details method. In the original code, they had two different tests for it. One is to see if the new questionnaire is saved in the database and a separate test is used to check for proper associations of questions in the new questionnaire. We have combined these tests to avoid rerunning the same tests to retrieve migration data from the database. Our tests cover almost all the functionalities provided in the suggested skeleton for the test files.  
      question2.save!


      # Stub the where method to return the questions associated with the original questionnaire
The first line of the second set of tests checks if the method yields 'question1' and 'question2' when questions.each is called, checking the associations between the 'questionnaire' model and the 'question' model.  
      allow(questionnaire).to receive_message_chain(:questions, :each).and_yield(question1).and_yield(question2)


      # Call the copy_questionnaire_details method
We are checking the consistencies of the copied data by comparing instructor ID, copied questionnaire name, and time of creation/copying. We are also checking the associations of the copied questionnaire with the 'question' model by checking the count of these copied questions and each particular question file by comparing its file type and ID.  
      copied_questionnaire = Questionnaire.copy_questionnaire_details({ id: questionnaire.id })


      # Assertions
Overall, these tests ensure that the copy_questionnaire_details method correctly creates a copy of a given questionnaire, including its associated questions, and that certain method calls are made within the method.
      expect(copied_questionnaire.instructor_id).to eq(questionnaire.instructor_id)
      expect(copied_questionnaire.name).to eq("Copy of #{questionnaire.name}")
      expect(copied_questionnaire.created_at).to be_within(1.second).of(Time.zone.now)


      # Verify that the copied questionnaire has associated questions
      expect(copied_questionnaire.questions.count).to eq(2)
      expect(copied_questionnaire.questions.first.txt).to eq(question1.txt)
      expect(copied_questionnaire.questions.second.txt).to eq(question2.txt)
    end


  end


The below tests were redundant
=== The below tests were redundant ===
----
----
These two tests were part of the test skeleton but were redundant, so we did not include them.
   it "deletes the questionnaire" do
   it "deletes the questionnaire" do
         #Test scenario 1
         #Test scenario 1
Line 792: Line 602:
         #Then: The questionnaire is deleted
         #Then: The questionnaire is deleted
   end
   end
=== SimpleCov Coverage Report ===
----
The SimpleCov report shows 96.23% relevant LoC for our questionnaire.rb model file.
*'''https://adryne01.github.io/Prog3-SimpleCov/Code%20coverage%20for%20Reimplementation-back-end.html'''
----
[[File:SimpleCov_Report.png]]<br><br>
== Authors ==
This project was completed by Aiden Bartlett, Brandon Devine, and Aditya Karthikeyan.

Latest revision as of 09:11, 27 March 2024

Problem Description

The project includes re-implementation of the questionnaire.rb found in Expertiza to the Reimplementation-back-end . The current implementation includes various functionalities such as validation checks and methods. The goal is to implement these ensuring the code follows SOLID principles and is DRY in nature.

Important points

  1. The new model should use rails in built active record validation and eliminate the Validate_questionnaire method.
  2. The model should check for any overlapping functionalities from the controller and ensure all business logic is present in the model only.
  3. Eliminate methods and fields from this model that are exclusively utilized in test files so that only the required changes to the model are merged into the main branch.
  4. Ruby naming conventions should be followed for each method.
  5. Tests should be performed using Rspec covering all necessary functionalities.
  6. Comments should be present for every new functionality that is added and ensure to avoid all mistakes mentioned in the checklist document.
  7. Simplification of code is possible in various functions such as get_weighted_score and compute_weighted_score. For more details check the PR which has changes and ideas used for the implementation by the last team working on it.
  8. There is already an existing model in the Reimplementation back end . However this is essentially a copy paste from expertiza. This was done by the team working on the controller last spring in 2023. All changes should be made to the same file. Starting from scratch in your fork will be the quickest way to ensure no mix up from the existing implementation.

Methods Reimplemented

Validate(s)


Before refactoring

 def validate_questionnaire
   errors.add(:max_question_score, 'The maximum question score must be a positive integer.') if max_question_score < 1
   errors.add(:min_question_score, 'The minimum question score must be a positive integer.') if min_question_score < 0
   errors.add(:min_question_score, 'The minimum question score must be less than the maximum.') if min_question_score >= max_question_score
   results = Questionnaire.where('id <> ? and name = ? and instructor_id = ?', id, name, instructor_id)
   errors.add(:name, 'Questionnaire names must be unique.') if results.present?
 end
end

After refactoring

 validates :name, presence: true
 validate :name_is_unique
 validates :max_question_score, numericality: { only_integer: true, greater_than: 0 }, presence: true
 validates :min_question_score, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, presence: true
 validate :min_less_than_max
 def name_is_unique
   # return if we do not have all the values to check
   return unless id && name && instructor_id
   # check for existing named questionnaire for this instructor that is not this questionnaire
   existing = Questionnaire.where('name = ? and instructor_id = ? and id <> ?', name, instructor_id, id)
   errors.add(:name, 'must be unique') if existing.present?
 end
 def min_less_than_max
   # do we have values if not then do not attempt to validate this
   return unless min_question_score && max_question_score
   errors.add(:min_question_score, 'must be less than max question score') if min_question_score >= max_question_score
 end

The validate_questionnaire method was changed in order to follow the Single Responsibility Principle in the SOLID guidelines. Before refactoring, validate_questionnaire was responsible for checking max, min, and if the min was less than max. So, after refactoring, the methods are split up to accomplish one check in each method. This also helps the readability of the code as the checks are very clear.

get_weighted_score


Before refactoring

 def get_weighted_score(assignment, scores)
   # create symbol for "varying rubrics" feature -Yang
   round = AssignmentQuestionnaire.find_by(assignment_id: assignment.id, questionnaire_id: id).used_in_round
   questionnaire_symbol = if round.nil?
                            symbol
                          else
                            (symbol.to_s + round.to_s).to_sym
                          end
   compute_weighted_score(questionnaire_symbol, assignment, scores)
 end
 def compute_weighted_score(symbol, assignment, scores)
   aq = assignment_questionnaires.find_by(assignment_id: assignment.id)
   if scores[symbol][:scores][:avg].nil?
     0
   else
     scores[symbol][:scores][:avg] * aq.questionnaire_weight / 100.0
   end
 end

After refactoring

 def get_weighted_score(assignment, scores)
   compute_weighted_score(questionnaire_symbol(assignment), assignment, scores)
 end
 def compute_weighted_score(symbol, assignment, scores)
   aq = assignment_questionnaires.find_by(assignment_id: assignment.id)
   scores[symbol][:scores][:avg].nil? ? 0 : scores[symbol][:scores][:avg] * aq.questionnaire_weight / 100.0
 end
 def questionnaire_symbol(assignment)
   # create symbol for "varying rubrics" feature
   # Credit ChatGPT to help me get from the inline below to the used inline, the yield self allowed me the work I wanted to do
   # AssignmentQuestionnaire.find_by(assignment_id: assignment.id, questionnaire_id: id)&.used_in_round ? "#{symbol}#{round}".to_sym : symbol
   AssignmentQuestionnaire.find_by(assignment_id: assignment.id, questionnaire_id: id)&.used_in_round&.yield_self { |round| "#{symbol}#{round}".to_sym } || symbol
 end

I decided to change this into two public methods and one private method. Since the original get_weighted_score got the score and looked up the symbol to use I felt it violated the single use principle. I created a private method to do the lookup of the symbol to use on its own. I created an elegant line of code to attempt to find an assignment questionnaire and if not null and used in a round it yields the result to be used in the return value. If not then it will just use this symbol. I had this line but without the yield as I was attempting to get it doing what I wanted it to do. I asked chatGPT and it presented me with the yield statement which I then used. Now each method only does what it should, gets the results, computes the result, or evaluates the symbol. 

true_false_questions?


 def true_false_questions?
   questions.each { |question| return true if question.type == 'Checkbox' }
   false
 end

This method was doing the job very succinctly already. No direct changes were made to this method and the method was left as is.

delete


Before refactoring

 def delete
   assignments.each do |assignment|
     raise "The assignment #{assignment.name} uses this questionnaire.
           Do you want to <A href='../assignment/delete/#{assignment.id}'>delete</A> the assignment?"
   end
   questions.each(&:delete)
   node = QuestionnaireNode.find_by(node_object_id: id)
   node.destroy if node
   destroy
 end

After refactoring

 def delete
   # Check to see if we go further? we cannot proceed if there are any assignments
   assignment = assignments.first
   if assignment
     raise "The assignment #{assignment.name} uses this questionnaire. Do you want to <A href='../assignment/delete/#{assignment.id}'>delete</A> the assignment?"
   end
   questions.each(&:delete)
   node = QuestionnaireNode.find_by(node_object_id: id)
   node&.destroy
   destroy
 end

There was no reason to do an each on the list if the first one was going to throw an exception already so that was altered to just get first. Also just doing a null check on the dereference to the destroy on questionnaire node was more succinct than doing an if.

max_possible_score

Before refactoring

 def max_possible_score
   results = Questionnaire.joins('INNER JOIN questions ON questions.questionnaire_id = questionnaires.id')
                          .select('SUM(questions.weight) * questionnaires.max_question_score as max_score')
                          .where('questionnaires.id = ?', id)
   results[0].max_score
 end

After refactoring

 def max_possible_score
   ## Just return 0 if there are no questions; don't throw an error.
   return 0 if questions.empty?
   ## Sums up the weight of all the questions. This is not necessarily 1.
   sum_of_weights = questions.sum{ | question| quesitons.weight}
   max_possible_score = sum_of_weights * max_possible_score
 end

This refactoring is similar to E2319. Reimplement questionnaire.rb. Instead of using the complicated SQL query, this should be easier to understand and is rather straightforward. Additionally, comments were added to improve the readability of the code. Adding the error check at the beginning should make the code slightly more robust in the case of questionnaires with no questions, and the variable names have been changed for clarity.

copy_questionnaire_details


Before refactoring

 def self.copy_questionnaire_details(params)
   orig_questionnaire = Questionnaire.find(params[:id])
   questions = Question.where(questionnaire_id: params[:id])
   questionnaire = orig_questionnaire.dup
   questionnaire.name = 'Copy of ' + orig_questionnaire.name
   questionnaire.created_at = Time.zone.now
   questionnaire.updated_at = Time.zone.now
   questionnaire.save!
   questions.each do |question|
     new_question = question.dup
     new_question.questionnaire_id = questionnaire.id
     new_question.save!
   end
   questionnaire
 end

After refactoring

def self.copy_questionnaire_details(params)
   orig_questionnaire = find(params[:id])
   questionnaire = orig_questionnaire.dup
   questionnaire.name = "Copy of #{orig_questionnaire.name}"
   questionnaire.created_at = Time.zone.now
   questionnaire.updated_at = Time.zone.now
   ActiveRecord::Base.transaction do
     questionnaire.save!
     orig_questionnaire.questions.each do |question|
       new_question = question.dup
       new_question.questionnaire_id = questionnaire.id
       new_question.save!
     end
   end
   questionnaire
 end

This method handles copying a questionnaire and its attributes and dependencies in the model structure. We find the questionnaire to copy from the database and assign it to orig_questionnaire. In the original code, they used a questions variable to access different questions associated with the original questionnaire. However, we can directly access the questions associated with a questionnaire with how the models are set up. So, it has been eliminated when reimplementing the method. String interpolation was used instead of string concatenation to improve readability and simplicity. It is also less error-prone when compared to concatenation. A big change in the implementation of this method is wrapping the copying process in a transaction. This is done to ensure data consistency. Active Record Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action. (Ref. https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html) We started an ActiveRecord transaction to save this new questionnaire and to duplicate the questions associated with the original questionnaire.

Required migrations

The following migrations were required to bring the current reimplementation fork up to par with what was needed to match the Expertiza class.

class AddUsedInRoundToAssignmentQuestionnaire < ActiveRecord::Migration[7.0]

 def self.up
   add_column 'assignment_questionnaires', 'used_in_round', :integer
 end
 def self.down
   remove_column 'assignment_questionnaires', 'used_in_round'
 end

end

class AddQuestionnarieWeightToAssignmentQuestionnaire < ActiveRecord::Migration[7.0]

 def self.up
   add_column 'assignment_questionnaires', 'questionnaire_weight', :integer, default: 0, null: false
 end
 def self.down
   remove_column 'assignment_questionnaires', 'questionnaire_weight'
 end

end

class CreateNodes < ActiveRecord::Migration[7.0]

 def self.up
   create_table :nodes do |t|
     t.column :parent_id, :integer
     t.column :node_object_id, :integer
     t.column :type, :string
   end
 end
 def self.down
   drop_table :nodes
 end

end

Rspec Testing

Suggested test skeleton


  # Here are the skeleton rspec tests to be implemented as well, or to replace existing duplicate tests
  describe "#get_weighted_score" do
    context "when the assignment has a round" do
      it "computes the weighted score using the questionnaire symbol with the round appended" do
        # Test case 1
        # Given an assignment with an ID and a questionnaire with a symbol
        # And the questionnaire is used in a round
        # And a set of scores
        # When the get_weighted_score method is called with the assignment and scores
        # Then it should compute the weighted score using the questionnaire symbol with the round appended
        # Test case 2
        # Given an assignment with an ID and a questionnaire with a symbol
        # And the questionnaire is used in a round
        # And a different set of scores
        # When the get_weighted_score method is called with the assignment and scores
        # Then it should compute the weighted score using the questionnaire symbol with the round appended
      end
    end
    context "when the assignment does not have a round" do
      it "computes the weighted score using the questionnaire symbol" do
        # Test case 3
        # Given an assignment with an ID and a questionnaire with a symbol
        # And the questionnaire is not used in a round
        # And a set of scores
        # When the get_weighted_score method is called with the assignment and scores
        # Then it should compute the weighted score using the questionnaire symbol
        # Test case 4
        # Given an assignment with an ID and a questionnaire with a symbol
        # And the questionnaire is not used in a round
        # And a different set of scores
        # When the get_weighted_score method is called with the assignment and scores
        # Then it should compute the weighted score using the questionnaire symbol
      end
    end
  end
  describe "#compute_weighted_score" do
    context "when the average score is nil" do
      it "returns 0" do
        # Test scenario
      end
    end
    context "when the average score is not nil" do
      it "calculates the weighted score based on the questionnaire weight" do
        # Test scenario
      end
    end
  end
  describe "#true_false_questions?" do
    context "when there are true/false questions" do
      it "returns true" do
        # Test scenario 1: Multiple questions with type 'Checkbox'
        # Test scenario 2: Single question with type 'Checkbox'
      end
    end
    context "when there are no true/false questions" do
      it "returns false" do
        # Test scenario 1: Multiple questions with no 'Checkbox' type
        # Test scenario 2: Single question with no 'Checkbox' type
      end
    end
  end
  describe "#delete" do
    context "when there are assignments using the questionnaire" do
      it "raises an error with a message asking if the user wants to delete the assignment" do
        # Test scenario 1
        # Given: There are assignments using the questionnaire
        # When: The delete method is called
        # Then: An error is raised with a message asking if the user wants to delete the assignment
        # Test scenario 2
        # Given: There are multiple assignments using the questionnaire
        # When: The delete method is called
        # Then: An error is raised for each assignment with a message asking if the user wants to delete the assignment
      end
    end
    context "when there are no assignments using the questionnaire" do
      it "deletes all the questions associated with the questionnaire" do
        # Test scenario 1
        # Given: There are no assignments using the questionnaire
        # When: The delete method is called
        # Then: All the questions associated with the questionnaire are deleted
        # Test scenario 2
        # Given: There are no assignments using the questionnaire and there are multiple questions
        # When: The delete method is called
        # Then: All the questions associated with the questionnaire are deleted
      end
      it "deletes the questionnaire node if it exists" do
        # Test scenario 1
        # Given: There are no assignments using the questionnaire and the questionnaire node exists
        # When: The delete method is called
        # Then: The questionnaire node is deleted
        # Test scenario 2
        # Given: There are no assignments using the questionnaire and the questionnaire node does not exist
        # When: The delete method is called
        # Then: No error is raised and the method completes successfully
      end
      it "deletes the questionnaire" do
        # Test scenario 1
        # Given: There are no assignments using the questionnaire
        # When: The delete method is called
        # Then: The questionnaire is deleted
        # Test scenario 2
        # Given: There are no assignments using the questionnaire and there are multiple questionnaires
        # When: The delete method is called
        # Then: The questionnaire is deleted
      end
    end
  end
  describe "#max_possible_score" do
    context "when the questionnaire has no questions" do
      it "returns 0 as the maximum possible score" do
        # test code
      end
    end
    context "when the questionnaire has questions with different weights" do
      it "returns the correct maximum possible score based on the weights and max_question_score" do
        # test code
      end
    end
    context "when the questionnaire has questions with the same weight" do
      it "returns the correct maximum possible score based on the weight and max_question_score" do
        # test code
      end
    end
    context "when the questionnaire ID does not exist" do
      it "returns nil as the maximum possible score" do
        # test code
      end
    end
  end
  describe '.copy_questionnaire_details' do
    context 'when given valid parameters' do
      it 'creates a copy of the questionnaire with the instructor_id' do
        # Test body
      end
      it 'sets the name of the copied questionnaire as "Copy of [original name]"' do
        # Test body
      end
      it 'sets the created_at timestamp of the copied questionnaire to the current time' do
        # Test body
      end
      it 'saves the copied questionnaire' do
        # Test body
      end
      it 'creates copies of all the questions from the original questionnaire' do
        # Test body
      end
      it 'sets the questionnaire_id of the copied questions to the id of the copied questionnaire' do
        # Test body
      end
      it 'sets the size of the copied criterion and text response questions to "50,3" if size is nil' do
        # Test body
      end
      it 'saves the copied questions' do
        # Test body
      end
      it 'creates copies of all the question advices associated with the original questions' do
        # Test body
      end
      it 'sets the question_id of the copied question advices to the id of the copied question' do
        # Test body
      end
      it 'saves the copied question advices' do
        # Test body
      end
      it 'returns the copied questionnaire' do
        # Test body
      end
    end
  end
  describe "#validate_questionnaire" do
    context "when the maximum question score is less than 1" do
      it "should add an error message" do
        # test code
      end
    end
    context "when the minimum question score is less than 0" do
      it "should add an error message" do
        # test code
      end
    end
    context "when the minimum question score is greater than or equal to the maximum question score" do
      it "should add an error message" do
        # test code
      end
    end
    context "when a questionnaire with the same name and instructor already exists" do
      it "should add an error message" do
        # test code
      end
    end
  end

Questionnaire model


Some of the variables that we will need in all or nearly all tests we have declared at the top within the whole describe for the Questionnaire class tests.

Validate(s)


The following list of tests are to assert all the validations for the class. Using the predefined factory variables we test validations. Are all names what we expect. Validate we cannot have a valid questionnaire without a name. Create two questionnaires with the same name and instructor to ensure we cannot save the second one. Also validate that the questionnaire returns the assigned instructors id.




There are a few tests to run which can help us test the maximum_score method. We can make sure it matches what is expected, we can assert that alpha characters and floats are not valid. We can follow that up with tests to ensure it is a positive integer, and that the max is larger than the minimum.




The final validations are minimum score and associations. The few tests for minimum score are to check that it is what it should be, that it is positive, and cannot be an alpha character.




get_weighted_score


 describe '#get_weighted_score' do
   before :each do
     @questionnaire = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
     @assignment = FactoryBot.create(:assignment)
   end
   context 'when the assignment has a round' do
     it 'computes the weighted score using the questionnaire symbol with the round appended' do
       # Test case 1
       # Arrange
       FactoryBot.create(:assignment_questionnaire, { assignment_id: @assignment.id, questionnaire_id: @questionnaire.id, used_in_round: 1 })
       scores = { "#{@questionnaire.symbol}#{1}".to_sym => { scores: { avg: 100 } } }
       # Act Assert
       expect(@questionnaire.get_weighted_score(@assignment, scores)).to eq(100)
       # Test case 2
       # Arrange
       scores = { "#{@questionnaire.symbol}#{1}".to_sym => { scores: { avg: 75 } } }
       # Act Assert
       expect(@questionnaire.get_weighted_score(@assignment, scores)).to eq(75)
     end
   end
   context 'when the assignment does not have a round' do
     it 'computes the weighted score using the questionnaire symbol' do
       # Test case 3
       # Arrange
       FactoryBot.create(:assignment_questionnaire, { assignment_id: @assignment.id, questionnaire_id: @questionnaire.id })
       scores = { @questionnaire.symbol => { scores: { avg: 100 } } }
       # Act Assert
       expect(@questionnaire.get_weighted_score(@assignment, scores)).to eq(100)
       # Test case 4
       # Arrange
       scores = { @questionnaire.symbol => { scores: { avg: 75 } } }
       # Act Assert
       expect(@questionnaire.get_weighted_score(@assignment, scores)).to eq(75)
     end
   end
   describe "#compute_weighted_score" do
     before :each do
       @questionnaire = FactoryBot.create(:review_questionnaire, { instructor_id: instructor.id })
       @assignment = FactoryBot.create(:assignment)
       FactoryBot.create(:assignment_questionnaire, { assignment_id: @assignment.id, questionnaire_id: @questionnaire.id, questionnaire_weight: 50 })
     end
     context "when the average score is nil" do
       it "returns 0" do
         # Test scenario
         # Arrange
         scores = { @questionnaire.symbol => { scores: { avg: nil } } }
         # Act Assert
         expect(@questionnaire.compute_weighted_score(@questionnaire.symbol, @assignment, scores)).to eq(0)
       end
     end
     context "when the average score is not nil" do
       it "calculates the weighted score based on the questionnaire weight" do
         # Test scenario
         # Arrange
         scores = { @questionnaire.symbol => { scores: { avg: 75 } } }
         # Act Assert
         expect(@questionnaire.compute_weighted_score(@questionnaire.symbol, @assignment, scores)).to eq(75 * 0.5)
       end
     end
   end
 end

true_false_questions?


We decided to use before each to setup the questionnaire that we can use for the duration of this describe. For these the test the skeleton provided was used and the scenarios felt like complete tests. Testing both a single and multiple valid checkbox questions assert to true. We also want to validate that single or multiple non checkbox questions assert to false.


delete




It is important to test that deleting a questionnaire also deletes all the questions associated with that questionnaire. In this case we test two situations, first we need to test just one question on the questionnaire, and then multiple questions on the questionnaire.  



For the last parts of delete testing I used 2 more of the test skeletons. There are two scenarios to test in relation to the question node associated to a questionnaire. We should assert that questions are deleted with and without an associated node, and that the questionnaire node does not exist any longer in the case where there was an associated node. 




copy_questionnaire_details




The tests for this method were implemented correctly and they covered most of the functionalities of this particular function.

The first set of tests checks the method calls from copy_questionnaire_details. The first line stubs the 'find' method of the Questionnaire model. The second line stubs the 'where' method of the Questionnaire model. The first test stubs the find method to return a mock 'questionnaire' object when called with argument 1. The second test looks for an array containing a 'question' object when called with questionnaire ID as an argument.

The second set of tests checks the copying process of the copy_questionnaire_details method. In the original code, they had two different tests for it. One is to see if the new questionnaire is saved in the database and a separate test is used to check for proper associations of questions in the new questionnaire. We have combined these tests to avoid rerunning the same tests to retrieve migration data from the database. Our tests cover almost all the functionalities provided in the suggested skeleton for the test files.

The first line of the second set of tests checks if the method yields 'question1' and 'question2' when questions.each is called, checking the associations between the 'questionnaire' model and the 'question' model.

We are checking the consistencies of the copied data by comparing instructor ID, copied questionnaire name, and time of creation/copying. We are also checking the associations of the copied questionnaire with the 'question' model by checking the count of these copied questions and each particular question file by comparing its file type and ID.

Overall, these tests ensure that the copy_questionnaire_details method correctly creates a copy of a given questionnaire, including its associated questions, and that certain method calls are made within the method.


The below tests were redundant


These two tests were part of the test skeleton but were redundant, so we did not include them.

 it "deletes the questionnaire" do
       #Test scenario 1
       #Given: There are no assignments using the questionnaire
       #When: The delete method is called
       #Then: The questionnaire is deleted
       #Test scenario 2
       #Given: There are no assignments using the questionnaire and there are multiple questionnaires
       #When: The delete method is called
       #Then: The questionnaire is deleted
 end


SimpleCov Coverage Report


The SimpleCov report shows 96.23% relevant LoC for our questionnaire.rb model file.





Authors

This project was completed by Aiden Bartlett, Brandon Devine, and Aditya Karthikeyan.