sped up some tests

This commit is contained in:
Hauke Klement
2015-01-30 10:50:44 +01:00
parent 76ceae54b0
commit b08bc8aef2
14 changed files with 65 additions and 46 deletions

View File

@ -5,12 +5,15 @@ class Controller < AnonymousController
end end
describe SubmissionScoring do describe SubmissionScoring do
before(:all) do
@submission = FactoryGirl.create(:submission, cause: 'submit')
end
let(:controller) { Controller.new } let(:controller) { Controller.new }
let(:submission) { FactoryGirl.create(:submission, cause: 'submit') }
before(:each) { controller.instance_variable_set(:@current_user, FactoryGirl.create(:external_user)) } before(:each) { controller.instance_variable_set(:@current_user, FactoryGirl.create(:external_user)) }
describe '#score_submission' do describe '#score_submission' do
let(:score_submission) { Proc.new { controller.score_submission(submission) } } let(:score_submission) { Proc.new { controller.score_submission(@submission) } }
before(:each) { score_submission.call } before(:each) { score_submission.call }
it 'assigns @assessor' do it 'assigns @assessor' do
@ -22,14 +25,14 @@ describe SubmissionScoring do
end end
it 'executes the teacher-defined test cases' do it 'executes the teacher-defined test cases' do
submission.collect_files.select(&:teacher_defined_test?).each do |file| @submission.collect_files.select(&:teacher_defined_test?).each do |file|
expect_any_instance_of(DockerClient).to receive(:execute_test_command).with(submission, file.name_with_extension).and_return({}) expect_any_instance_of(DockerClient).to receive(:execute_test_command).with(@submission, file.name_with_extension).and_return({})
end end
score_submission.call score_submission.call
end end
it 'updates the submission' do it 'updates the submission' do
expect(submission).to receive(:update).with(score: anything) expect(@submission).to receive(:update).with(score: anything)
score_submission.call score_submission.call
end end
end end

View File

@ -61,7 +61,7 @@ describe ErrorsController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:errors) { FactoryGirl.create_pair(:error) } before(:all) { FactoryGirl.create_pair(:error) }
before(:each) { get :index, execution_environment_id: execution_environment.id } before(:each) { get :index, execution_environment_id: execution_environment.id }
expect_assigns(execution_environment: :execution_environment) expect_assigns(execution_environment: :execution_environment)

View File

@ -72,7 +72,7 @@ describe ExecutionEnvironmentsController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:execution_environments) { FactoryGirl.create_pair(:ruby) } before(:all) { FactoryGirl.create_pair(:ruby) }
before(:each) { get :index } before(:each) { get :index }
expect_assigns(execution_environments: ExecutionEnvironment.all) expect_assigns(execution_environments: ExecutionEnvironment.all)

View File

@ -1,12 +1,12 @@
require 'rails_helper' require 'rails_helper'
describe ExercisesController do describe ExercisesController do
let(:exercise) { FactoryGirl.create(:fibonacci) } let(:exercise) { FactoryGirl.create(:dummy) }
let(:user) { FactoryGirl.create(:admin) } let(:user) { FactoryGirl.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) } before(:each) { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do describe 'POST #create' do
let(:exercise_attributes) { FactoryGirl.build(:fibonacci).attributes } let(:exercise_attributes) { FactoryGirl.build(:dummy).attributes }
context 'with a valid exercise' do context 'with a valid exercise' do
let(:request) { Proc.new { post :create, exercise: exercise_attributes } } let(:request) { Proc.new { post :create, exercise: exercise_attributes } }
@ -61,7 +61,7 @@ describe ExercisesController do
expect_assigns(exercise: :exercise) expect_assigns(exercise: :exercise)
it 'destroys the exercise' do it 'destroys the exercise' do
exercise = FactoryGirl.create(:fibonacci) exercise = FactoryGirl.create(:dummy)
expect { delete :destroy, id: exercise.id }.to change(Exercise, :count).by(-1) expect { delete :destroy, id: exercise.id }.to change(Exercise, :count).by(-1)
end end
@ -102,8 +102,8 @@ describe ExercisesController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:exercises) { FactoryGirl.create_pair(:fibonacci) }
let(:scope) { Pundit.policy_scope!(user, Exercise) } let(:scope) { Pundit.policy_scope!(user, Exercise) }
before(:all) { FactoryGirl.create_pair(:dummy) }
before(:each) { get :index } before(:each) { get :index }
expect_assigns(exercises: :scope) expect_assigns(exercises: :scope)
@ -195,7 +195,7 @@ describe ExercisesController do
describe 'PUT #update' do describe 'PUT #update' do
context 'with a valid exercise' do context 'with a valid exercise' do
let(:exercise_attributes) { FactoryGirl.build(:fibonacci).attributes } let(:exercise_attributes) { FactoryGirl.build(:dummy).attributes }
before(:each) { put :update, exercise: exercise_attributes, id: exercise.id } before(:each) { put :update, exercise: exercise_attributes, id: exercise.id }
expect_assigns(exercise: Exercise) expect_assigns(exercise: Exercise)

View File

@ -53,7 +53,7 @@ describe FileTypesController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:file_types) { FactoryGirl.create_pair(:dot_rb) } before(:all) { FactoryGirl.create_pair(:dot_rb) }
before(:each) { get :index } before(:each) { get :index }
expect_assigns(file_types: FileType.all) expect_assigns(file_types: FileType.all)

View File

@ -55,7 +55,7 @@ describe HintsController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:hints) { FactoryGirl.create_pair(:ruby_syntax_error) } before(:all) { FactoryGirl.create_pair(:ruby_syntax_error) }
before(:each) { get :index, execution_environment_id: execution_environment.id } before(:each) { get :index, execution_environment_id: execution_environment.id }
expect_assigns(execution_environment: :execution_environment) expect_assigns(execution_environment: :execution_environment)

View File

@ -27,7 +27,7 @@ describe SessionsController do
end end
describe 'POST #create_through_lti' do describe 'POST #create_through_lti' do
let(:exercise) { FactoryGirl.create(:fibonacci) } let(:exercise) { FactoryGirl.create(:dummy) }
let(:nonce) { SecureRandom.hex } let(:nonce) { SecureRandom.hex }
context 'without OAuth parameters' do context 'without OAuth parameters' do
@ -125,7 +125,7 @@ describe SessionsController do
describe 'GET #destroy_through_lti' do describe 'GET #destroy_through_lti' do
let(:request) { Proc.new { get :destroy_through_lti, consumer_id: consumer.id, submission_id: submission.id } } let(:request) { Proc.new { get :destroy_through_lti, consumer_id: consumer.id, submission_id: submission.id } }
let(:submission) { FactoryGirl.create(:submission) } let(:submission) { FactoryGirl.create(:submission, exercise: FactoryGirl.create(:dummy)) }
before(:each) do before(:each) do
session[:consumer_id] = consumer.id session[:consumer_id] = consumer.id

View File

@ -57,7 +57,7 @@ describe SubmissionsController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:submissions) { FactoryGirl.create_pair(:submission) } before(:all) { FactoryGirl.create_pair(:submission) }
before(:each) { get :index } before(:each) { get :index }
expect_assigns(submissions: Submission.all) expect_assigns(submissions: Submission.all)

View File

@ -50,7 +50,7 @@ describe TeamsController do
end end
describe 'GET #index' do describe 'GET #index' do
let!(:teams) { FactoryGirl.create_pair(:team) } before(:all) { FactoryGirl.create_pair(:team) }
before(:each) { get :index } before(:each) { get :index }
expect_assigns(teams: Team.all) expect_assigns(teams: Team.all)

View File

@ -32,6 +32,14 @@ FactoryGirl.define do
end end
end end
factory :dummy, class: Exercise do
created_by_teacher
description 'Dummy'
association :execution_environment, factory: :ruby
instructions
title 'Dummy'
end
factory :even_odd, class: Exercise do factory :even_odd, class: Exercise do
created_by_teacher created_by_teacher
description 'Implement two methods even and odd which return whether a given number is even or odd, respectively.' description 'Implement two methods even and odd which return whether a given number is even or odd, respectively.'

View File

@ -1,29 +1,30 @@
require 'rails_helper' require 'rails_helper'
describe Submission do describe Submission do
let(:submission) { Submission.create } before(:all) do
@submission = FactoryGirl.create(:submission)
end
it 'validates the presence of a cause' do it 'validates the presence of a cause' do
expect(submission.errors[:cause]).to be_present expect(Submission.create.errors[:cause]).to be_present
end end
it 'validates the presence of an exercise' do it 'validates the presence of an exercise' do
expect(submission.errors[:exercise_id]).to be_present expect(Submission.create.errors[:exercise_id]).to be_present
end end
it 'validates the presence of a user' do it 'validates the presence of a user' do
expect(submission.errors[:user_id]).to be_present expect(Submission.create.errors[:user_id]).to be_present
expect(submission.errors[:user_type]).to be_present expect(Submission.create.errors[:user_type]).to be_present
end end
%w[download render run test].each do |action| %w[download render run test].each do |action|
describe "##{action}_url" do describe "##{action}_url" do
let(:submission) { FactoryGirl.create(:submission) } let(:url) { @submission.send(:"#{action}_url") }
let(:url) { submission.send(:"#{action}_url") }
it "starts like the #{action} path" do it "starts like the #{action} path" do
filename = File.basename(__FILE__) filename = File.basename(__FILE__)
expect(url).to start_with(Rails.application.routes.url_helpers.send(:"#{action}_submission_path", submission, filename).sub(filename, '')) expect(url).to start_with(Rails.application.routes.url_helpers.send(:"#{action}_submission_path", @submission, filename).sub(filename, ''))
end end
it 'ends with a placeholder' do it 'ends with a placeholder' do

View File

@ -3,39 +3,41 @@ require 'rails_helper'
describe CodeOcean::FilePolicy do describe CodeOcean::FilePolicy do
subject { CodeOcean::FilePolicy } subject { CodeOcean::FilePolicy }
let(:file) { FactoryGirl.build(:file) }
let(:exercise) { FactoryGirl.create(:fibonacci) }
let(:submission) { FactoryGirl.create(:submission) }
permissions :create? do permissions :create? do
context 'as part of an exercise' do context 'as part of an exercise' do
before(:each) { file.context = exercise } before(:all) do
@exercise = FactoryGirl.create(:fibonacci)
@file = @exercise.files.first
end
it 'grants access to admins' do it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), file) expect(subject).to permit(FactoryGirl.build(:admin), @file)
end end
it 'grants access to authors' do it 'grants access to authors' do
expect(subject).to permit(exercise.author, file) expect(subject).to permit(@exercise.author, @file)
end end
it 'does not grant access to all other users' do it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name| [:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file) expect(subject).not_to permit(FactoryGirl.build(factory_name), @file)
end end
end end
end end
context 'as part of a submission' do context 'as part of a submission' do
before(:each) { file.context = submission } before(:all) do
@submission = FactoryGirl.create(:submission)
@file = @submission.files.first
end
it 'grants access to authors' do it 'grants access to authors' do
expect(subject).to permit(submission.author, file) expect(subject).to permit(@submission.author, @file)
end end
it 'does not grant access to all other users' do it 'does not grant access to all other users' do
[:admin, :external_user, :teacher].each do |factory_name| [:admin, :external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file) expect(subject).not_to permit(FactoryGirl.build(factory_name), @file)
end end
end end
end end
@ -43,29 +45,34 @@ describe CodeOcean::FilePolicy do
permissions :destroy? do permissions :destroy? do
context 'as part of an exercise' do context 'as part of an exercise' do
before(:each) { file.context = exercise } before(:all) do
@exercise = FactoryGirl.create(:fibonacci)
@file = @exercise.files.first
end
it 'grants access to admins' do it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), file) expect(subject).to permit(FactoryGirl.build(:admin), @file)
end end
it 'grants access to authors' do it 'grants access to authors' do
expect(subject).to permit(exercise.author, file) expect(subject).to permit(@exercise.author, @file)
end end
it 'does not grant access to all other users' do it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name| [:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file) expect(subject).not_to permit(FactoryGirl.build(factory_name), @file)
end end
end end
end end
context 'as part of a submission' do context 'as part of a submission' do
before(:each) { file.context = submission } before(:all) do
@file = FactoryGirl.create(:submission).files.first
end
it 'does not grant access to anyone' do it 'does not grant access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name| [:admin, :external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file) expect(subject).not_to permit(FactoryGirl.build(factory_name), @file)
end end
end end
end end

View File

@ -4,7 +4,7 @@ describe ExercisePolicy do
subject { ExercisePolicy } subject { ExercisePolicy }
before(:all) do before(:all) do
@exercise = FactoryGirl.build(:fibonacci, team: FactoryGirl.create(:team)) @exercise = FactoryGirl.build(:dummy, team: FactoryGirl.create(:team))
end end
[:create?, :index?, :new?].each do |action| [:create?, :index?, :new?].each do |action|
@ -67,7 +67,7 @@ describe ExercisePolicy do
[@admin, @teacher].each do |user| [@admin, @teacher].each do |user|
[true, false].each do |public| [true, false].each do |public|
[@team, nil].each do |team| [@team, nil].each do |team|
FactoryGirl.create(:fibonacci, public: public, team: team, user_id: user.id, user_type: InternalUser.class.name) FactoryGirl.create(:dummy, public: public, team: team, user_id: user.id, user_type: InternalUser.class.name)
end end
end end
end end

View File

@ -19,7 +19,7 @@ describe SubmissionPolicy do
it 'grants access to authors' do it 'grants access to authors' do
user = FactoryGirl.create(:external_user) user = FactoryGirl.create(:external_user)
expect(subject).to permit(user, FactoryGirl.build(:submission, user_id: user.id, user_type: user.class.name)) expect(subject).to permit(user, FactoryGirl.build(:submission, exercise: Exercise.new, user_id: user.id, user_type: user.class.name))
end end
end end
end end