diff --git a/app/models/submission.rb b/app/models/submission.rb --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -101,20 +101,24 @@ def must_specify_language return if self.source==nil self.language = Submission.find_language_in_source(self.source) - errors.add_to_base("must specify programming language") unless self.language!=nil + errors.add('source',"must specify programming language") unless self.language!=nil end def must_have_valid_problem return if self.source==nil if self.problem_id!=-1 - problem = Problem.find(self.problem_id) + begin + problem = Problem.find(self.problem_id) + rescue ActiveRecord::RecordNotFound + problem = nil + end else problem = Submission.find_problem_in_source(self.source) end if problem==nil - errors.add_to_base("must specify problem") + errors.add('problem',"must be specified.") elsif (!problem.available) and (self.new_record?) - errors.add_to_base("must specify valid problem") + errors.add('problem',"must be valid.") else self.problem = problem end diff --git a/app/models/test_request.rb b/app/models/test_request.rb --- a/app/models/test_request.rb +++ b/app/models/test_request.rb @@ -23,6 +23,9 @@ belongs_to :problem belongs_to :submission + validates_presence_of :submission + validate :must_have_valid_problem + def problem_name TestRequest.name_of(self.problem) end @@ -50,15 +53,30 @@ def self.new_from_form_params(user,params) test_request = TestRequest.new test_request.user = user - problem = Problem.find(params[:problem_id]) + begin + problem = Problem.find(params[:problem_id]) + rescue ActiveRecord::RecordNotFound + problem = nil + end test_request.problem = problem - test_request.submission = - Submission.find_by_user_problem_number(user.id, - problem.id, - params[:submission_number]) - test_request.input_file_name = save_input_file(params[:input_file], user, problem) - if test_request.input_file_name == nil + if problem!=nil + test_request.submission = + Submission.find_by_user_problem_number(user.id, + problem.id, + params[:submission_number]) + else + test_request.submission = nil + end + + # checks if the user submits any input file + if params[:input_file]==nil or params[:input_file]=="" test_request.errors.add_to_base("No input submitted.") + test_request.input_file_name = nil + else + test_request.input_file_name = save_input_file(params[:input_file], user, problem) + if test_request.input_file_name == nil + test_request.errors.add_to_base("No input submitted.") + end end test_request.submitted_at = Time.new test_request.status_inqueue @@ -101,4 +119,16 @@ end new_file_name end + + # + # validations + # + def must_have_valid_problem + if problem==nil + errors.add('problem',"must be specified.") + elsif (!problem.available) and (self.new_record?) + errors.add('problem',"must be valid.") + end + end + end diff --git a/spec/models/test_request_spec.rb b/spec/models/test_request_spec.rb new file mode 100644 --- /dev/null +++ b/spec/models/test_request_spec.rb @@ -0,0 +1,39 @@ + +require File.dirname(__FILE__) + '/../spec_helper' + +describe TestRequest do + + before(:each) do + @problem = mock_model(Problem) + @user = mock_model(User) + @submission = mock_model(Submission) + end + + it "should validates that problem exists" do + test_request = TestRequest.new(:user => @user, + :problem => nil, + :submission => @submission, + :input_file_name => "somefile") + test_request.save.should == false + test_request.errors['problem'].should_not be_nil + end + + it "should validates that problem is available" do + @problem.should_receive(:available).and_return(false) + test_request = TestRequest.new(:user => @user, + :problem => @problem, + :submission => @submission, + :input_file_name => "somefile") + test_request.save.should == false + end + + it "should validates valid submission" do + @problem.should_receive(:available).and_return(true) + test_request = TestRequest.new(:user_id => @user.id, + :problem => @problem, + :submission => nil, + :input_file_name => "somefile") + test_request.save.should == false + end + +end