CSC/ECE 517 Spring 2023 - E2345. Reimplement QuestionnairesController and QuestionsController: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 207: Line 207:
       render json: $ERROR_INFO, status: :unprocessable_entity
       render json: $ERROR_INFO, status: :unprocessable_entity
     end
     end
  end
</pre>
:*Refactor questionnaire_params and create sanitize_display_type to make the create and update endpoints more modular.
<pre>
def questionnaire_params
    params.require(:questionnaire).permit(:name, :questionnaire_type, :private, :min_question_score, :max_question_score, :instructor_id)
  end
</pre>
<pre>
  def sanitize_display_type(type)
    display_type = type.split('Questionnaire')[0]
    if %w[AuthorFeedback CourseSurvey TeammateReview GlobalSurvey AssignmentSurvey BookmarkRating].include?(display_type)
      display_type = (display_type.split(/(?=[A-Z])/)).join('%')
    end
    display_type
   end
   end
</pre>
</pre>

Revision as of 02:14, 24 April 2023

Introduction

This page gives a description of the changes made for the questions_controller.rb, questionnaire_controller.rb, question.rb & questionnaire.rb of Expertiza based OSS project.

Problem Statement

What is needed: This project builds on E2321, and the main goal is to get the endpoints of QuestionsController and QuestionnairesController running in reimplementation-backend repository. Detailed goals of the project are as follows:

  1. Improving the clarity and conciseness of code: The code should be written in a clean and concise manner. Methods with identical names that perform different functionalities should be renamed for clarity. Functions or functionality that are not clear should be commented on or removed. Any loops or methods that can be refactored for better performance should be addressed.
  2. Discarding unused functionality: Any unused functionality should be removed from the controllers. This will help to reduce complexity and make the code easier to maintain.
  3. Reimplement Question and Questionnaire models: Reimplement the two models to facilitate the endpoints of the respective controllers.
  4. Writing tests for the two models: Tests should be written for both the Questionnaires and the Questions models.
  5. Writing tests for the two controllers: Tests should be written for both the QuestionnairesController and the QuestionsController. The tests should written in the RSwag test format.

General Design Goals

  • Ensure all prior/expected functionality is working correctly, and if not, make any repairs or changes necessary.
  • Ensure that the each of the models and controllers have loose coupling and tight cohesion.
  • DRY out prior implementation's controller and model methods which have functionality that already exists within the expertiza system.
  • Ensure all present test cases still work properly after making the above changes and create additional test cases.

Current Implementation

Plan for current implementation for the code can be found here E2321.

After seperation of the responsibilities of the QuestionnairesController and QuestionsController, fixing bugs, discarding unused functionality, we reimplemented the code to create basic CRUD operations. Then the methods were converted to basic CRUD operations which are described below. (Information related to separating responsibilities, bugs fixed, current testing and unused functionality can be found on the above link for plan of current implementation.)

  • Reimplement CRUD functionality and create JSON endpoints in QuestionsController: We reimplemented the following methods to create the required endpoints in questions_controller.rb.
  1. index: This is a GET endpoint that responds with a list of all questions with their attributes.
  2. show: This is a GET endpoint that accepts a question id and responds with the attributes of that question if it exists.
  3. create: This is a POST endpoint that accepts the question parameters and creates a new question.
  4. destroy: This is a DELETE endpoint that accepts a question ID and deletes that question.
  5. update: This is a PUT endpoint that accepts a question ID and parameters, and, updates that question with the given parameters.
  6. types: This is a GET endpoint that responds with the list of question types.
  • Reimplement CRUD functionality and create JSON endpoints in QuestionnairesController: We reimplemented the following methods to create the required endpoints in questionnaires_controller.rb.
  1. index: This is a GET endpoint that responds with a list of all questionnaires with their attributes.
  2. show: This is a GET endpoint that accepts a questionnaire id and responds with the attributes of that questionnaire if it exists.
  3. create: This is a POST endpoint that accepts the questionnaire parameters and creates a new questionnaire.
  4. destroy: This is a DELETE endpoint that accepts a questionnaire ID and deletes that questionnaire.
  5. update: This is a PUT endpoint that accepts a questionnaire ID and parameters, and, updates that questionnaire with the given parameters.
  6. copy: This is a POST endpoint that accepts a questionnaire ID and creates a new questionnaire (copy) with the same attributes of that questionnaire.
  7. toggle_access: This is a GET endpoint that changes the access from public to private and vice versa.

The current implementation of the Controllers include only manual testing via Postman. In E2345, we plan to add automated RSpec tests using the Swagger API to test each endpoint.

Implementation

We did not have models for Questionnaires and Questions, along with other primary models on which the Questionnaire and Question model depend on in the reimplementation-back-end repository, so we ran it on expertiza repository. To get the questions and questionnaires module running in the reimplementation-back-end repository, we plan to follow these steps:

  1. Reimplement basic version of question.rb and questionnaire.rb such that it facilitates the controllers' CRUD operations.
    • Add required associations for user.rb, role.rb, question.rb, and questionnaire.rb only.
    • The below code snippets represent current implementation of question.rb:
    class Question < ApplicationRecord
        belongs_to :questionnaire # each question belongs to a specific questionnaire
        
        validates :seq, presence: true, numericality: true # sequence must be numeric
        validates :txt, length: { minimum: 0, allow_nil: false, message: "can't be nil" } # user must define text content for a question
        validates :question_type, presence: true # user must define type for a question
        validates :break_before, presence: true
      
        
        def as_json(options = {})
            super(options.merge({
                                  only: %i[questionnaire_id txt weight seq question_type size alternatives break_before min_label max_label created_at updated_at],
                                  include: {
                                    questionnaire: 
                                          { only: %i[name private min_question_score max_question_score instructor_id created_at updated_at questionnaire_type] }
                                  }
                                })).tap do |hash|
              hash['questionnaire'] ||= { id: nil, name: nil }
            end
        end
      
      end
    

    In the above code, the as_json method is used to decorate the JSON object that is returned as a part of the response body from the controller endpoint. It is used to replace the questionnaire_id with the questionnaire details within the question object in JSON format.

    • The below code snippets represent current implementation of questionnaire.rb:
    class Questionnaire < ApplicationRecord
        has_many :questions, dependent: :restrict_with_error
        belongs_to :instructor
      
        before_destroy :check_for_question_associations
    
        validate :validate_questionnaire
        validates :name, presence: true
        validates :max_question_score, :min_question_score, numericality: true
      
        # clones the contents of a questionnaire, including the questions and associated advice
        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
      
        # validate the entries for this questionnaire
        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
    
        # Check_for_question_associations checks if questionnaire has associated questions or not
        def check_for_question_associations
          if questions.any?
            raise ActiveRecord::DeleteRestrictionError.new(:base, "Cannot delete record because dependent questions exist")
          end
        end
    
        def as_json(options = {})
            super(options.merge({
                                  only: %i[id name private min_question_score max_question_score created_at updated_at questionnaire_type],
                                  include: {
                                    instructor: { only: %i[name email fullname password role] }
                                  }
                                })).tap do |hash|
              hash['instructor'] ||= { id: nil, name: nil }
            end
        end
      end
    

    In the above code,

    • The as_json method is used to decorate the JSON object that is returned as a part of the response body from the controller endpoint. It is used to replace the questionnaire_id with the questionnaire details within the question object in JSON format.
    • The check_for_question_associations method is used to raise an Error if a Questionnaire that has associated questions is tried to be deleted.
    • The copy_questionnaire_details method is used to create a new object with the same attributes of the questionnaire and save it to the database. It is called when the copy endpoint is requested.
  2. Reimplement questions_controller.rb and questionnaires_controller.rb to incorporate the feedback received in E2321. The feedback is as follows:
    • Refactor the Create endpoint in questionnaires_controller.rb such that individual fields does not need to be set and validated. The below code represents the revised implementation of the method. A Questionnaire object is created using the new method instead of manual assignment of attributes.
    def create
        begin
          @questionnaire = Questionnaire.new(questionnaire_params)
          @questionnaire.display_type = sanitize_display_type(@questionnaire.questionnaire_type)
          @questionnaire.save!
          render json: @questionnaire, status: :created and return
        rescue ActiveRecord::RecordInvalid
          render json: $ERROR_INFO.to_s, status: :unprocessable_entity
        end
      end
    
    • Refactor the Create endpoint in questions_controller.rb such that individual fields does not need to be set and validated. The below code represents the revised implementation of the method. A Question object is created using the new method instead of manual assignment of attributes.
    def create
        begin
          questionnaire_id = params[:questionnaire_id] unless params[:questionnaire_id].nil?
          num_of_existed_questions = Questionnaire.find(questionnaire_id).questions.size
          question = Question.create(
            txt: params[:txt],
            questionnaire_id: questionnaire_id,
            seq: num_of_existed_questions + 1,
            question_type: params[:question_type],
            break_before: true)
          # question = Object.const_get(params[:question][:type])
    .create(txt: '', questionnaire_id: questionnaire_id, seq: num_of_existed_questions + 1, question_type:params[:question][:type], break_before: true)
          case question.question_type
            when 'Scale'
              question.weight = params[:weight]
              question.max_label = 'Strongly agree'
              question.min_label = 'Strongly disagree'
            when 'Cake', 'Criterion'
              question.weight = params[:weight]
              question.max_label = 'Strongly agree'
              question.min_label = 'Strongly disagree'
              question.size = '50, 3'
            when 'Dropdown'
              question.alternatives = '0|1|2|3|4|5'
              question.size = nil
            when 'TextArea'
              question.size = '60, 5'
            when 'TextField'
              question.size = '30'
          end
        
          question.save!
          render json: question, status: :created
        rescue ActiveRecord::RecordNotFound
          render json: $ERROR_INFO.to_s, status: :not_found and return  
        rescue ActiveRecord::RecordInvalid
          render json: $ERROR_INFO.to_s, status: :unprocessable_entity
        end
      end
    
    • Create delete_all endpoint in QuestionsController that takes questionnaire_id as the path parameter and deletes all questions associated to that questionnaire. It returns status 404 if the questionnaire does not exist, status 422 if no questions are associated to that questionnaire and status 200 if all the questions are successfully deleted.
    def delete_all
        begin
          @questionnaire = Questionnaire.find(params[:id])
          if @questionnaire.questions.size > 0 
            @questionnaire.questions.destroy_all
            msg = "All questions for Questionnaire ID:" + params[:id].to_s + " has been successfully deleted!"
            render json: msg, status: :ok
          else
            render json: "No questions associated to Questionnaire ID:" + params[:id].to_s, status: :unprocessable_entity and return
          end
        rescue ActiveRecord::RecordNotFound
          render json: $ERROR_INFO.to_s, status: :not_found and return
        rescue ActiveRecord::RecordInvalid
          render json: $ERROR_INFO, status: :unprocessable_entity
        end
      end
    
    • Refactor questionnaire_params and create sanitize_display_type to make the create and update endpoints more modular.
    def questionnaire_params
        params.require(:questionnaire).permit(:name, :questionnaire_type, :private, :min_question_score, :max_question_score, :instructor_id)
      end
    
      def sanitize_display_type(type)
        display_type = type.split('Questionnaire')[0]
        if %w[AuthorFeedback CourseSurvey TeammateReview GlobalSurvey AssignmentSurvey BookmarkRating].include?(display_type)
          display_type = (display_type.split(/(?=[A-Z])/)).join('%')
        end
        display_type
      end
    

Testing Methodology

  • Rspec tests for QuestionnairesController and QuestionsController had been added in the previous project E2321 but had not been executed due to missing dependencies. This project focuses on implementing them and getting automated test cases running as expected.

In our previous project we implemented the CRUD endpoints but since all the models were not present in reimplementation backend repository, we could not test our controller tests. In this project, we focus to create the models required to implement CRUD operations in the reimplementation backend repository and then run and test the API tests for the CRUD operations.

Controller Tests

To run the controller tests:

1. git clone

2. cd reimplementation-back-end/

3. bundle install

4. bundle exec rspec spec/requests/api/v1/questionnaires_controller_spec.rb

Sr No Test Description
1 Test to create a new Questionnaire with valid parameters.
2 Test to create a new Questionnaire with invalid parameters.
3 Test to create a copy of Questionnaire.
4 Test to list the Questionnaires.
5 Test to update the Questionnaire with correct parameters.
6 Test to update the Questionnaire with incorrect parameters.
7 Test to delete Questionnaire with existing id.
8 Test to delete Questionnaire with the non-existing id.
9 Test to show a Questionnaire with a valid id.
10 Test to show a Questionnaire with an invalid id.
11 Test to toggle private boolean flag of a Questionnaire with a valid id.
12 Test to toggle private boolean flag of a Questionnaire with a invalid id.

bundle exec rspec spec/requests/api/v1/questions_controller_spec.rb

Sr No Test Description
1 Test to create a new Question with valid parameters.
2 Test to create a new Question with invalid parameters.
3 Test to list the Questions.
4 Test to update the Question with correct parameters.
5 Test to update the Question with incorrect parameters.
6 Test to delete Question with existing id.
7 Test to delete Question with the non-existing id.
8 Test to show a Question with a valid id.
9 Test to show a Question with an invalid id.
10 Test to get a type of a Question with valid id.
11 Test to get a type of a Question with invalid id.
12 Test to delete all Questions of a Questionnaire with a valid id.
13 Test to delete all Questions of a Questionnaire with an invalid id.
  • Rspec unit and functional tests for Questionnaire and Question model will be created. (Since, we are implementing required models for reimplementation backend repository in this project, we plan to write unit and functional tests to test the methods written in the model.)

Model Tests

To run the model tests:

1. git clone

2. cd reimplementation-back-end/

3. bundle install

4. bundle exec rspec spec/models/question_spec.rb

Sr No Test Description
1 Test that Question has valid attributes defined.
2 Test ensures that a question is not valid without seq field.
3 Test that Question sequence number must be numerical.
4 Test ensures that a question is not valid without txt field.
5 Test ensures that a question is not valid without question_type field.
6 Test ensures that a question is not valid without break_before field.
7 Test ensures that a question does not exist without a questionnaire.
8 Test ensures that a question object is deleted properly taking all its association into consideration.

5. bundle exec rspec spec/models/questionnaire_spec.rb

Sr No Test Description
1 Test that Questionnaire requires a name.
2 Test validates the name of the questionnaire.
3 Test validates the instructor id in the questionnaire .
4 Test validates the maximum score in the questionnaire.
5 Test ensures maximum score of the questionnaire is an integer.
6 Test ensures maximum score is positive.
7 Test ensures maximum score is greater than the minimum score.
8 Test validates minimum score of a questionnaire.
  • Demonstrating working of API endpoints on Postman. Given below are the endpoints which we tested successfully on Postman.

Questionnaires Controller

  1. index: GET method - http://152.7.178.189:8080/api/v1/questionnaires
  2. show: GET method - http://152.7.178.189:8080/api/v1/questionnaires/1
  3. create: POST method - http://152.7.178.189:8080/api/v1/questionnaires/
  4. destroy: DELETE method - http://152.7.178.189:8080/api/v1/questionnaires/1
  5. update: .PUT method - http://152.7.178.189:8080/api/v1/questionnaires/1
  6. copy: POST method - http://152.7.178.189:8080/api/v1/questionnaires/copy/1
  7. toggle_access: GET method - http://152.7.178.189:8080/api/v1/questionnaires/toggle_access/1

Questions Controller

  1. index: GET method - http://152.7.178.189:8080/api/v1/questions/
  2. show: GET method - http://152.7.178.189:8080/api/v1/questions/1
  3. create: POST method - http://152.7.178.189:8080/api/v1/questions/
  4. destroy: DELETE method - http://152.7.178.189:8080/api/v1/questions/2
  5. update: PUT method - http://152.7.178.189:8080/api/v1/questions/2
  6. delete all: DELETE method - http://152.7.178.189:8080/api/v1/questions/delete_all/2
  7. types: GET method - http://152.7.178.189:8080/api/v1/questions/types

Relevant Links

Contributors

This feature was created as part of Dr. Edward Gehringer's "CSC/ECE 517: Object-Oriented Design and Development" class, Spring 2023. The contributors were: Vineet Vimal Chheda, Rohan Jigarbhai Shah, and Aditya Srivastava. Our project mentor was Ankur Mundra (amundra@ncsu.edu)