Skip to content

Commit

Permalink
FIX: Grace credits calculation problem.
Browse files Browse the repository at this point in the history
Unit tests added to grouping_test.rb
  • Loading branch information
lakeskysea authored and jerboaa committed Mar 12, 2012
1 parent b2bb3f2 commit 9bed3de
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/submissions_controller.rb
Expand Up @@ -78,7 +78,7 @@ class SubmissionsController < ApplicationController
return a.current_submission_used.result.total_mark <=> b.current_submission_used.result.total_mark
},
'grace_credits_used' => lambda { |a,b|
return a.grace_period_deduction_sum <=> b.grace_period_deduction_sum
return a.grace_period_deduction_single <=> b.grace_period_deduction_single
},
'section' => lambda { |a,b|
return -1 if !a.section
Expand Down
15 changes: 10 additions & 5 deletions app/models/grouping.rb
Expand Up @@ -285,12 +285,17 @@ def available_grace_credits
return total.min
end

def grace_period_deduction_sum
total = 0
grace_period_deductions.each do |grace_period_deduction|
total += grace_period_deduction.deduction
# The grace credits deducted (of one student) for this specific submission
# in the grouping
def grace_period_deduction_single
single = 0
# Since for an instance of a grouping all members of the group will get
# deducted the same amount (for a specific assignment), it is safe to pick
# any deduction
if !grace_period_deductions.nil? && !grace_period_deductions.first.nil?
single = grace_period_deductions.first.deduction
end
return total
return single
end

# Submission Functions
Expand Down
Expand Up @@ -111,7 +111,7 @@
<% end %>
</td>
<td>
<%= h(grouping.grace_period_deduction_sum) %>
<%= h(grouping.grace_period_deduction_single) %>
</td>
<td>
<% if !grouping.has_submission? %>
Expand Down
176 changes: 176 additions & 0 deletions test/unit/grouping_test.rb
Expand Up @@ -501,4 +501,180 @@ class GroupingTest < ActiveSupport::TestCase
assert !@grouping.can_invite?(@student_cannot_invite)
end
end

context "Assignment has a grace period of 24 hours after due date" do
setup do
@assignment = Assignment.make
@group = Group.make
grace_period_submission_rule = GracePeriodSubmissionRule.new
@assignment.replace_submission_rule(grace_period_submission_rule)
GracePeriodDeduction.destroy_all
grace_period_submission_rule.save

# On July 1 at 1PM, the instructor sets up the course...
pretend_now_is(Time.parse("July 1 2009 1:00PM")) do
# Due date is July 23 @ 5PM
@assignment.due_date = Time.parse("July 23 2009 5:00PM")
# Overtime begins at July 23 @ 5PM
# Add a 24 hour grace period
period = Period.new
period.submission_rule = @assignment.submission_rule
period.hours = 24
period.save
# Collect date is now after July 24 @ 5PM
@assignment.save
end
end

teardown do
destroy_repos
end

context "A grouping of one student submitting an assignment" do
setup do
# grouping of only one student
@grouping = Grouping.make(:assignment => @assignment, :group => @group)
@inviter_membership = StudentMembership.make(:user => Student.make(:user_name => "student1"),
:grouping => @grouping,
:membership_status => StudentMembership::STATUSES[:inviter])
@inviter = @inviter_membership.user

# On July 15, the Student logs in, triggering repository folder creation
pretend_now_is(Time.parse("July 15 2009 6:00PM")) do
@grouping.create_grouping_repository_folder
end
end

should "not deduct grace credits because submission is on time" do

# Check the number of member in this grouping
assert_equal 1, @grouping.student_membership_number

submit_files_before_due_date

# An Instructor or Grader decides to begin grading
pretend_now_is(Time.parse("July 28 2009 1:00PM")) do
submission = Submission.create_by_timestamp(@grouping, @assignment.submission_rule.calculate_collection_time)
submission = @assignment.submission_rule.apply_submission_rule(submission)

@grouping.reload
# Should be no deduction because submitting on time
assert_equal 0, @grouping.grace_period_deduction_single
end
end

should "deduct one grace credit" do

# Check the number of member in this grouping
assert_equal 1, @grouping.student_membership_number
# Make sure the available grace credits are enough
assert @grouping.available_grace_credits >= 1

submit_files_after_due_date("July 24 2009 9:00AM", "LateSubmission.java", "Some overtime contents")

# An Instructor or Grader decides to begin grading
pretend_now_is(Time.parse("July 28 2009 1:00PM")) do
submission = Submission.create_by_timestamp(@grouping, @assignment.submission_rule.calculate_collection_time)
submission = @assignment.submission_rule.apply_submission_rule(submission)

@grouping.reload
# Should display 1 credit deduction because of one-day late submission
assert_equal 1, @grouping.grace_period_deduction_single
end
end

end # end of context "A grouping of one student submitting an assignment"

context "A grouping of two students submitting an assignment" do
setup do
# grouping of two students
@grouping = Grouping.make(:assignment => @assignment, :group => @group)
# should consist of inviter and another student
@membership = StudentMembership.make(:user => Student.make(:user_name => "student1"),
:grouping => @grouping,
:membership_status => StudentMembership::STATUSES[:accepted])

@inviter_membership = StudentMembership.make(:user => Student.make(:user_name => "student2"),
:grouping => @grouping,
:membership_status => StudentMembership::STATUSES[:inviter])
@inviter = @inviter_membership.user

# On July 15, the Student logs in, triggering repository folder creation
pretend_now_is(Time.parse("July 15 2009 6:00PM")) do
@grouping.create_grouping_repository_folder
end
end

should "not deduct grace credits because submission is on time" do

# Check the number of member in this grouping
assert_equal 2, @grouping.student_membership_number

submit_files_before_due_date

# An Instructor or Grader decides to begin grading
pretend_now_is(Time.parse("July 28 2009 1:00PM")) do
submission = Submission.create_by_timestamp(@grouping, @assignment.submission_rule.calculate_collection_time)
submission = @assignment.submission_rule.apply_submission_rule(submission)

@grouping.reload
# Should be no deduction because submitting on time
assert_equal 0, @grouping.grace_period_deduction_single
end
end

should "deduct one grace credit" do

# Check the number of member in this grouping
assert_equal 2, @grouping.student_membership_number
# Make sure the available grace credits are enough
assert @grouping.available_grace_credits >= 1

submit_files_after_due_date("July 24 2009 9:00AM", "LateSubmission.java", "Some overtime contents")

# An Instructor or Grader decides to begin grading
pretend_now_is(Time.parse("July 28 2009 1:00PM")) do
submission = Submission.create_by_timestamp(@grouping, @assignment.submission_rule.calculate_collection_time)
submission = @assignment.submission_rule.apply_submission_rule(submission)

@grouping.reload
# Should display 1 credit deduction because of one-day late submission
assert_equal 1, @grouping.grace_period_deduction_single
end
end

end # end of context "A grouping of two students submitting an assignment"

end # end of context "Assignment has a grace period of 24 hours after due date"

def submit_files_before_due_date
pretend_now_is(Time.parse("July 20 2009 5:00PM")) do
assert Time.now < @assignment.due_date
assert Time.now < @assignment.submission_rule.calculate_collection_time
@group.access_repo do |repo|
txn = repo.get_transaction("test")
txn = add_file_helper(txn, 'TestFile.java', 'Some contents for TestFile.java')
repo.commit(txn)
end
end
end

def submit_files_after_due_date(time, filename, text)
pretend_now_is(Time.parse(time)) do
assert Time.now > @assignment.due_date
assert Time.now < @assignment.submission_rule.calculate_collection_time
@group.access_repo do |repo|
txn = repo.get_transaction("test")
txn = add_file_helper(txn, filename, text)
repo.commit(txn)
end
end
end

def add_file_helper(txn, file_name, file_contents)
path = File.join(@assignment.repository_folder, file_name)
txn.add(path, file_contents, '')
return txn
end

end

0 comments on commit 9bed3de

Please sign in to comment.