Skip to content

Commit

Permalink
Add tests for score function
Browse files Browse the repository at this point in the history
  • Loading branch information
kiragrammel committed Aug 25, 2023
1 parent 032c55f commit 53ccbde
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
120 changes: 120 additions & 0 deletions spec/features/score_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'Score', js: true do
let(:exercise) { create(:hello_world) }
let(:user) { create(:external_user) }
let(:scoring_response_full_score) do
[{
status: :ok,
stdout: '',
stderr: '',
waiting_for_container_time: 0,
container_execution_time: 0,
file_role: 'teacher_defined_test',
count: 1,
failed: 0,
error_messages: [],
passed: 1,
score: 1.0,
filename: 'index.html_spec.rb',
message: 'Well done.',
weight: 2.0,
}]
end
let(:scoring_response_zero_points) do
[{
status: :ok,
stdout: '',
stderr: '',
waiting_for_container_time: 0,
container_execution_time: 0,
file_role: 'teacher_defined_test',
count: 1,
failed: 0,
error_messages: [],
passed: 1,
score: 0.0,
filename: 'index.html_spec.rb',
message: 'Well done.',
weight: 2.0,
}]
end

before do
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
end

context 'when LTI outcomes are supported' do
before do
allow_any_instance_of(LtiHelper).to receive(:lti_outcome_service?).and_return(true)
visit(implement_exercise_path(exercise))
allow(Submission).to receive(:find).and_return(submission)
allow(submission).to receive(:calculate_score).and_return(scoring_response)
allow(submission).to receive(:normalized_score).and_return(normalized_score)
allow_any_instance_of(SubmissionsController).to receive(:send_score).and_return(status:, score_sent: sent_score)
click_button(I18n.t('exercises.editor.score'))
end

context 'when the full score is reached' do
let(:submission) { create(:submission, exercise:, user:, score: 1.0) }
let(:scoring_response) { scoring_response_full_score }
let(:normalized_score) { 1 }
let(:sent_score) { 1 }

context 'when scoring is successful' do
let(:status) { 'success' }

it 'shows a full score notification' do
# Text needs to be split because it includes the embedded url in Html which is not shown in the notification
# We only compare the shown notification text
expect(page).to have_content(I18n.t('exercises.submit.full_score').split('.').first)
expect(page).to have_link(nil, href: redirect_after_submit_submission_path(submission))
end
end

context 'when scoring is to late' do
let(:normalized_score) { 0.7 }
let(:status) { 'success' }

it 'shows a scoring to late notification' do
expect(page).to have_content(I18n.t('exercises.submit.too_late'))
end
end

context 'when scoring fails' do
let(:status) { 'error' } # unsupported also works

it 'shows a scoring failure notification' do
expect(page).to have_content(I18n.t('exercises.submit.failure'))
end
end
end

context 'when full score is not reached' do
let(:submission) { create(:submission, exercise:, user:, score: 0.0) }
let(:scoring_response) { scoring_response_zero_points }
let(:normalized_score) { 0 }
let(:status) { 'success' }
let(:sent_score) { 0 }
end
end

context 'when LTI outcomes are not supported' do
before do
allow_any_instance_of(LtiHelper).to receive(:lti_outcome_service?).and_return(false)
visit(implement_exercise_path(exercise))
click_button(I18n.t('exercises.editor.score'))
sleep 10
end

it 'does not send scores' do
expect_any_instance_of(SubmissionsController).not_to receive(:send_score)
end

it 'shows execution environments full notification' do
expect(page).to have_content(I18n.t('exercises.editor.depleted'))
end
end
end
2 changes: 2 additions & 0 deletions spec/policies/exercise_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
end
end

# TODO: Remove?
permissions :submit? do
context 'when teacher-defined assessments are available' do
before { create(:test_file, context: exercise) }
Expand All @@ -197,6 +198,7 @@
end
end

# TODO: Remove?
context 'when teacher-defined assessments are not available' do
it 'does not grant access to anyone' do
%i[admin external_user teacher].each do |factory_name|
Expand Down

0 comments on commit 53ccbde

Please sign in to comment.