Jump to content

User:NarenThanikesh/sandbox

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Aagrawa6 (talk | contribs) at 19:36, 28 October 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Introduction

Expertiza

Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and word documents.Expertiza provides a dashboard for all the Assignments corresponding to a course and provides absolute control to the Instructors and Teaching Assistant. In addition to Assignments, it encompasses peer reviews where in participants are allowed to provide feedback anonymously about each other's work thereby providing scope for better outcome. The due_date.rb file is responsible for informing the users about the deadline for submission of the each assignment. Due date in Expertiza have their association with many other components like Assignments, Reviews etc.

Code Climate

A Chrome extension for Code Climate to integrate the static analysis results , as well as the test coverage feedback we generate directly into Github, without ever leaving GitHub. To see issues displayed directly inside GitHub's UI. To review which lines are covered in diffs, files, and more. And add repos and open tickets without changing your workflow.

Tasks Identified

To install Code climate Chrome Extension that highlights the duplicated code.

To refactor the following two files:

  • Due_date.rb
    • Ternary operators must not be nested. Prefer `if` or `else` constructs instead.
    • Useless assignment to variable - `sorted_deadlines`.
    • Prefer `each` over `for`.
    • Use `find_by` instead of `where.first`.
    • Correct the use of Time function
  • Deadline_helper.rb
    • Do not use `Time.now` without zone. Use one of `Time.zone.now`, `Time.current`, `Time.now.in_time_zone`, `Time.now.utc`, `Time.now.getlocal`, `Time.now.iso8601`, `Time.now.jisx0301`, `Time.now.rfc3339`, `Time.now.to_i`, `Time.now.to_f` instead.
    • Trailing whitespace detected.
    • Extra empty line detected at module body end.
  • Create respective RSpec files in /spec/models/ and /spec/helper folder and write unit tests for each method in due_date.rb and deadline_helper.rb.

Current Implementation

This is a Model class to manage the deadlines of an assignment. It has methods for setting due dates for an assignment, copying due dates from one assignment to a new assignment etc. It focuses on refactoring some of the complex methods, modifying the language to make it more Ruby-friendly. The goal of this project is to attempt to make this part of the application easier to read and check all the possible test cases that the application must pass. The testing part of the code is missing that is one of the tasks to be implemented by us in addition to refactoring the two given files.

Changed Implementation

Changes implemented involves refactoring the code and making it more understandable by adding comments in the code.

The modified files are

  • Due_date.rb (path: /app/models)
  • Deadline_helper.rb (path: /app/helpers)

Testing files

  • Due_date_spec.rb (path: /spec/models)
  • Deadline_helper_spec.rb (path: /spec/helpers)

Due_date.rb

  • Change for loop to for..each in each statement
  • sorted_deadlines corrected
  • Ternary operators have been changer to if..else
    • Due_date.rb
  • Changed where to find_by
  • Corrected the time functions by adding the correct zones to them such as Time.zone.new
    Due_date.rb
  • Missing braces have been corrected
    • Due_date.rb

Deadline_helper.rb

  • Time functions were changed to functions with zones
  • Extra line removed
Deadline_helper

RSpec testing

There were no existing tests for the functions in due_date.rb and deadline_helper.rb. We have added exhaustive set of RSpec tests to test all the code. We have added two new spec file 'due_date_spec.rb' and ‘deadline_helper_spec.rb’ which covers testing scenario for the functions of  ‘Due_date.rb’ and ‘deadline_helper.rb’.

These RSpec files have 100% code coverage visible at: /coverage/index.html

due_date_spec.rb

This file is located at spec/models tests the functionalities of the due_date.rb file(app/models). Some of the test cases are to check:

  • If the flag is set.
  • If the old_assignment copied to the new_assignment.
  • If the nil value of get_next_due_date throws an error. etc

To test this file run the following command:

rspec spec/models/due_date_spec.rb

The output of this RSpec file is present in below screenshot:

deadline_helper_spec.rb

This is a test file for testing the functionalities of Deadline_helper.rb file located at app/helpers. Different test cases present in this file are:

  • Check if the factory is valid:
it "has a valid factory" do
    factory = FactoryGirl.build(:topic_due_date)
    expect(factory).to be_valid
end
  • Fail if the due date is invalid:
it "should fail because of invalid due_date" do
      expect { DeadlineHelper.create_topic_deadline(nil, 0, 0)}.to raise_exception(NoMethodError)
end
  • If new due_date object is created:
    it "new due_date object created" do
          DeadlineHelper.create_topic_deadline(@topic_due_date, 0, 1)
          expect(TopicDueDate.count).to be == 2
    end
    
  • due_at should be same for 0 offset:
     it "due_at should be same for 0 offset" do
          DeadlineHelper.create_topic_deadline(@topic_due_date, 0, 10)
          new_due_date = TopicDueDate.find_by(parent_id: 10)
          expect(new_due_date).to be_valid
          expect(new_due_date.due_at.to_s).to be == @topic_due_date.due_at.to_s
    end
    
  • due_at is calculated correctly if offset is positive:
    it "due_at calculated correctly for positive offset" do
          DeadlineHelper.create_topic_deadline(@topic_due_date, 5000, 10)
          new_due_date = TopicDueDate.find_by(parent_id: 10)
          expect(new_due_date).to be_valid
          expect(new_due_date.due_at.to_s).to be == (Time.zone.parse(@topic_due_date.due_at.to_s) + 5000).to_s
    end
    
  • due_at is calculated correctly if offset is negative:
    it "due_at calculated correctly for negative offset" do
          DeadlineHelper.create_topic_deadline(@topic_due_date, -5000, 10)
          new_due_date = TopicDueDate.find_by(parent_id: 10)
          expect(new_due_date).to be_valid
          expect(new_due_date.due_at.to_s).to be == (Time.zone.parse(@topic_due_date.due_at.to_s) - 5000).to_s
    end
    
  • The offset is being converted to integer properly:
    it "offset converted to integer correctly" do
          DeadlineHelper.create_topic_deadline(@topic_due_date, 5000.15, 10)
          new_due_date = TopicDueDate.find_by(parent_id: 10)
          expect(new_due_date).to be_valid
          expect(new_due_date.due_at.to_s).to be == (Time.zone.parse(@topic_due_date.due_at.to_s) + 5000).to_s
    end
    

To test this file run the following command:

rspec spec/models/deadline_helper_spec.rb

The output of this RSpec file is present in below screenshot:

Code coverage details of the above RSpec files is present in below screenshot:

[[File:Coverage details of deadline helper.rb.png|Coverage details of deadline helper.rb]]

References