Skip to content

Commit

Permalink
Merge pull request #686 from coursemology-collab/lowjoel/handle-non-u…
Browse files Browse the repository at this point in the history
…nique-auto-gradings

Ensure that non-unique auto grading records are properly handled.
  • Loading branch information
WANG QIANG committed Dec 15, 2015
2 parents d789ae0 + 60a9531 commit e955ff4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/models/course/assessment/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ def validate_consistent_grade
#
# @return [Course::Assessment::Answer::AutoGrading]
def ensure_auto_grading!
create_auto_grading! unless auto_grading
rescue ActiveRecord::RecordNotUnique
auto_grading || create_auto_grading!
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique => e
raise e if e.is_a?(ActiveRecord::RecordInvalid) && e.record.errors[:answer_id].empty?
association(:auto_grading).reload
auto_grading
end

Expand Down
23 changes: 23 additions & 0 deletions spec/models/course/assessment/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,28 @@
end
end
end

describe '#ensure_auto_grading!' do
context 'when an existing auto grading already exists' do
let(:existing_record) do
# Duplicate the subject so the subject does not know about the grading.
create(:course_assessment_answer_auto_grading, answer: subject.class.find(subject.id))
end

it 'returns the existing grading' do
# Simulate a concurrent creation of an existing record.
expect(subject.auto_grading).to be_nil
existing_record

expect(subject.send(:ensure_auto_grading!)).to eq(existing_record)
end
end

context 'when no existing auto grading exists' do
it 'creates a new grading' do
expect(subject.send(:ensure_auto_grading!)).to be_persisted
end
end
end
end
end

0 comments on commit e955ff4

Please sign in to comment.