Move submission scoring from controller concern to submission model
Localization and markdown formatting is now done in a controller spec in order to bring this logic away from the data and towards the view.
This commit is contained in:

committed by
Sebastian Serth

parent
b847daf823
commit
b6bc578aea
54
spec/concerns/scoring_result_formatting_spec.rb
Normal file
54
spec/concerns/scoring_result_formatting_spec.rb
Normal file
@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
class Controller < AnonymousController
|
||||
include ScoringResultFormatting
|
||||
end
|
||||
|
||||
describe ScoringResultFormatting do
|
||||
let(:controller) { Controller.new }
|
||||
let(:filename) { 'exercise.py' }
|
||||
let(:feedback_message) { '**good work**' }
|
||||
let(:outputs) { [{filename: filename, message: feedback_message}] }
|
||||
|
||||
describe 'feedback message' do
|
||||
let(:new_feedback_message) { controller.format_scoring_results(outputs).first[:message] }
|
||||
|
||||
context 'when the feedback message is not a path to a locale' do
|
||||
let(:feedback_message) { '**good work**' }
|
||||
|
||||
it 'renders the feedback message as markdown' do
|
||||
expect(new_feedback_message).to match('<p><strong>good work</strong></p>')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the feedback message is a valid path to a locale' do
|
||||
let(:feedback_message) { 'exercises.implement.default_test_feedback' }
|
||||
|
||||
it 'replaces the feedback message with the locale' do
|
||||
expect(new_feedback_message).to eq(I18n.t(feedback_message))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'filename' do
|
||||
let(:new_filename) { controller.format_scoring_results(outputs).first[:filename] }
|
||||
|
||||
context 'when the filename is not a path to a locale' do
|
||||
let(:filename) { 'exercise.py' }
|
||||
|
||||
it 'does not alter the filename' do
|
||||
expect(new_filename).to eq(filename)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the filename is a valid path to a locale' do
|
||||
let(:filename) { 'exercises.implement.not_graded' }
|
||||
|
||||
it 'replaces the filename with the locale' do
|
||||
expect(new_filename).to eq(I18n.t(filename))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,38 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe SubmissionScoring do
|
||||
let(:submission) { FactoryBot.create(:submission, cause: 'submit') }
|
||||
|
||||
describe '#collect_test_results' do
|
||||
let(:runner) { FactoryBot.create :runner }
|
||||
|
||||
before do
|
||||
allow(Runner).to receive(:for).and_return(runner)
|
||||
allow(runner).to receive(:copy_files)
|
||||
allow(runner).to receive(:attach_to_execution).and_return(1.0)
|
||||
end
|
||||
|
||||
after { submission.calculate_score }
|
||||
|
||||
it 'executes every teacher-defined test file' do
|
||||
allow(submission).to receive(:score_submission)
|
||||
submission.collect_files.select(&:teacher_defined_assessment?).each do |file|
|
||||
allow(submission).to receive(:test_result).with(any_args, file).and_return({})
|
||||
end
|
||||
end
|
||||
|
||||
it 'scores the submission' do
|
||||
allow(submission).to receive(:score_submission).and_return([])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#score_submission', cleaning_strategy: :truncation do
|
||||
after { submission.score_submission([]) }
|
||||
|
||||
it 'assigns a score to the submissions' do
|
||||
expect(submission).to receive(:update).with(score: anything)
|
||||
end
|
||||
end
|
||||
end
|
@ -242,7 +242,7 @@ describe ExercisesController do
|
||||
let(:user) { FactoryBot.create(:external_user) }
|
||||
let(:scoring_response) do
|
||||
[{
|
||||
status: 'ok',
|
||||
status: :ok,
|
||||
stdout: '',
|
||||
stderr: '',
|
||||
waiting_for_container_time: 0,
|
||||
@ -263,7 +263,7 @@ describe ExercisesController do
|
||||
FactoryBot.create(:lti_parameter, external_user: user, exercise: exercise)
|
||||
submission = FactoryBot.build(:submission, exercise: exercise, user: user)
|
||||
allow(submission).to receive(:normalized_score).and_return(1)
|
||||
allow(submission).to receive(:calculate_score).and_return(JSON.dump(scoring_response))
|
||||
allow(submission).to receive(:calculate_score).and_return(scoring_response)
|
||||
allow(Submission).to receive(:create).and_return(submission)
|
||||
end
|
||||
|
||||
|
@ -6,7 +6,7 @@ describe 'Editor', js: true do
|
||||
let(:exercise) { FactoryBot.create(:audio_video, description: Forgery(:lorem_ipsum).sentence) }
|
||||
let(:scoring_response) do
|
||||
[{
|
||||
status: 'ok',
|
||||
status: :ok,
|
||||
stdout: '',
|
||||
stderr: '',
|
||||
waiting_for_container_time: 0,
|
||||
@ -95,7 +95,7 @@ describe 'Editor', js: true do
|
||||
|
||||
it 'contains a button for submitting the exercise' do
|
||||
submission = FactoryBot.build(:submission, user: user, exercise: exercise)
|
||||
allow(submission).to receive(:calculate_score).and_return(JSON.dump(scoring_response))
|
||||
allow(submission).to receive(:calculate_score).and_return(scoring_response)
|
||||
allow(Submission).to receive(:find).and_return(submission)
|
||||
click_button(I18n.t('exercises.editor.score'))
|
||||
expect(page).not_to have_css('#submit_outdated')
|
||||
|
@ -141,4 +141,35 @@ describe Submission do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#calculate_score' do
|
||||
let(:runner) { FactoryBot.create :runner }
|
||||
|
||||
before do
|
||||
allow(Runner).to receive(:for).and_return(runner)
|
||||
allow(runner).to receive(:copy_files)
|
||||
allow(runner).to receive(:attach_to_execution).and_return(1.0)
|
||||
end
|
||||
|
||||
after { submission.calculate_score }
|
||||
|
||||
it 'executes every teacher-defined test file' do
|
||||
allow(submission).to receive(:combine_file_scores)
|
||||
submission.collect_files.select(&:teacher_defined_assessment?).each do |file|
|
||||
expect(submission).to receive(:score_file).with(any_args, file)
|
||||
end
|
||||
end
|
||||
|
||||
it 'scores the submission' do
|
||||
expect(submission).to receive(:combine_file_scores)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#combine_file_scores' do
|
||||
after { submission.send(:combine_file_scores, []) }
|
||||
|
||||
it 'assigns a score to the submissions' do
|
||||
expect(submission).to receive(:update).with(score: anything)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user