diff --git a/app/models/feedback.rb b/app/models/feedback.rb index 3af18f1fa0..11f5625642 100644 --- a/app/models/feedback.rb +++ b/app/models/feedback.rb @@ -105,9 +105,9 @@ def uncomplete end def reset_feedback_after_submission_update - return unless will_save_change_to_submission_id? && submission_id_in_database.present? + return unless will_save_change_to_submission_id? - Submission.find(submission_id_in_database).annotations.where(evaluation_id: evaluation_id).destroy_all + Submission.find(submission_id_in_database).annotations.where(evaluation_id: evaluation_id).destroy_all if submission_id_in_database.present? scores.each(&:destroy) end diff --git a/test/controllers/feedbacks_controller_test.rb b/test/controllers/feedbacks_controller_test.rb index 68684e57ef..492531b58c 100644 --- a/test/controllers/feedbacks_controller_test.rb +++ b/test/controllers/feedbacks_controller_test.rb @@ -5,6 +5,7 @@ class FeedbacksControllerTest < ActionDispatch::IntegrationTest def setup @evaluation = create :evaluation, :with_submissions + @evaluation.update(users: @evaluation.users + [users(:student)]) exercise = @evaluation.evaluation_exercises.first @score_item1 = create :score_item, evaluation_exercise: exercise, description: 'First item', @@ -57,6 +58,22 @@ def setup assert_equal 0, @feedback.scores.count end + test 'scores are reset when a submission is changed from empty' do + feedback = @evaluation.feedbacks.find_by(submission_id: nil, evaluation_exercise: @evaluation.evaluation_exercises.first) + assert_not_empty feedback.scores + + s = create :submission, exercise: feedback.exercise, user: feedback.user, course: feedback.evaluation.series.course + + patch evaluation_feedback_path(@evaluation, feedback), params: { + feedback: { + submission_id: s.id + } + } + feedback.reload + assert_equal s.id, feedback.submission_id + assert_empty feedback.scores + end + test 'A lot of scores are reset when a submission is changed' do score_items = (1..10).map do |i| create :score_item, evaluation_exercise: @feedback.evaluation_exercise,