Description:
[web] add validations to test_request and submission
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@173 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r87:4194731c391b - - 3 files changed: 84 inserted, 11 deleted
@@ -0,0 +1,39 | |||||
|
|
1 | + | ||
|
|
2 | + require File.dirname(__FILE__) + '/../spec_helper' | ||
|
|
3 | + | ||
|
|
4 | + describe TestRequest do | ||
|
|
5 | + | ||
|
|
6 | + before(:each) do | ||
|
|
7 | + @problem = mock_model(Problem) | ||
|
|
8 | + @user = mock_model(User) | ||
|
|
9 | + @submission = mock_model(Submission) | ||
|
|
10 | + end | ||
|
|
11 | + | ||
|
|
12 | + it "should validates that problem exists" do | ||
|
|
13 | + test_request = TestRequest.new(:user => @user, | ||
|
|
14 | + :problem => nil, | ||
|
|
15 | + :submission => @submission, | ||
|
|
16 | + :input_file_name => "somefile") | ||
|
|
17 | + test_request.save.should == false | ||
|
|
18 | + test_request.errors['problem'].should_not be_nil | ||
|
|
19 | + end | ||
|
|
20 | + | ||
|
|
21 | + it "should validates that problem is available" do | ||
|
|
22 | + @problem.should_receive(:available).and_return(false) | ||
|
|
23 | + test_request = TestRequest.new(:user => @user, | ||
|
|
24 | + :problem => @problem, | ||
|
|
25 | + :submission => @submission, | ||
|
|
26 | + :input_file_name => "somefile") | ||
|
|
27 | + test_request.save.should == false | ||
|
|
28 | + end | ||
|
|
29 | + | ||
|
|
30 | + it "should validates valid submission" do | ||
|
|
31 | + @problem.should_receive(:available).and_return(true) | ||
|
|
32 | + test_request = TestRequest.new(:user_id => @user.id, | ||
|
|
33 | + :problem => @problem, | ||
|
|
34 | + :submission => nil, | ||
|
|
35 | + :input_file_name => "somefile") | ||
|
|
36 | + test_request.save.should == false | ||
|
|
37 | + end | ||
|
|
38 | + | ||
|
|
39 | + end |
@@ -101,20 +101,24 | |||||
|
101 | def must_specify_language |
|
101 | def must_specify_language |
|
102 | return if self.source==nil |
|
102 | return if self.source==nil |
|
103 | self.language = Submission.find_language_in_source(self.source) |
|
103 | self.language = Submission.find_language_in_source(self.source) |
|
104 |
- errors.add |
|
104 | + errors.add('source',"must specify programming language") unless self.language!=nil |
|
105 | end |
|
105 | end |
|
106 |
|
106 | ||
|
107 | def must_have_valid_problem |
|
107 | def must_have_valid_problem |
|
108 | return if self.source==nil |
|
108 | return if self.source==nil |
|
109 | if self.problem_id!=-1 |
|
109 | if self.problem_id!=-1 |
|
110 | - problem = Problem.find(self.problem_id) |
|
110 | + begin |
|
|
111 | + problem = Problem.find(self.problem_id) | ||
|
|
112 | + rescue ActiveRecord::RecordNotFound | ||
|
|
113 | + problem = nil | ||
|
|
114 | + end | ||
|
111 | else |
|
115 | else |
|
112 | problem = Submission.find_problem_in_source(self.source) |
|
116 | problem = Submission.find_problem_in_source(self.source) |
|
113 | end |
|
117 | end |
|
114 | if problem==nil |
|
118 | if problem==nil |
|
115 |
- errors.add |
|
119 | + errors.add('problem',"must be specified.") |
|
116 | elsif (!problem.available) and (self.new_record?) |
|
120 | elsif (!problem.available) and (self.new_record?) |
|
117 | - errors.add_to_base("must specify valid problem") |
|
121 | + errors.add('problem',"must be valid.") |
|
118 | else |
|
122 | else |
|
119 | self.problem = problem |
|
123 | self.problem = problem |
|
120 | end |
|
124 | end |
@@ -23,6 +23,9 | |||||
|
23 | belongs_to :problem |
|
23 | belongs_to :problem |
|
24 | belongs_to :submission |
|
24 | belongs_to :submission |
|
25 |
|
25 | ||
|
|
26 | + validates_presence_of :submission | ||
|
|
27 | + validate :must_have_valid_problem | ||
|
|
28 | + | ||
|
26 | def problem_name |
|
29 | def problem_name |
|
27 | TestRequest.name_of(self.problem) |
|
30 | TestRequest.name_of(self.problem) |
|
28 | end |
|
31 | end |
@@ -50,15 +53,30 | |||||
|
50 | def self.new_from_form_params(user,params) |
|
53 | def self.new_from_form_params(user,params) |
|
51 | test_request = TestRequest.new |
|
54 | test_request = TestRequest.new |
|
52 | test_request.user = user |
|
55 | test_request.user = user |
|
53 | - problem = Problem.find(params[:problem_id]) |
|
56 | + begin |
|
|
57 | + problem = Problem.find(params[:problem_id]) | ||
|
|
58 | + rescue ActiveRecord::RecordNotFound | ||
|
|
59 | + problem = nil | ||
|
|
60 | + end | ||
|
54 | test_request.problem = problem |
|
61 | test_request.problem = problem |
|
55 | - test_request.submission = |
|
62 | + if problem!=nil |
|
56 | - Submission.find_by_user_problem_number(user.id, |
|
63 | + test_request.submission = |
|
57 | - problem.id, |
|
64 | + Submission.find_by_user_problem_number(user.id, |
|
58 |
- |
|
65 | + problem.id, |
|
59 | - test_request.input_file_name = save_input_file(params[:input_file], user, problem) |
|
66 | + params[:submission_number]) |
|
60 | - if test_request.input_file_name == nil |
|
67 | + else |
|
|
68 | + test_request.submission = nil | ||
|
|
69 | + end | ||
|
|
70 | + | ||
|
|
71 | + # checks if the user submits any input file | ||
|
|
72 | + if params[:input_file]==nil or params[:input_file]=="" | ||
|
61 | test_request.errors.add_to_base("No input submitted.") |
|
73 | test_request.errors.add_to_base("No input submitted.") |
|
|
74 | + test_request.input_file_name = nil | ||
|
|
75 | + else | ||
|
|
76 | + test_request.input_file_name = save_input_file(params[:input_file], user, problem) | ||
|
|
77 | + if test_request.input_file_name == nil | ||
|
|
78 | + test_request.errors.add_to_base("No input submitted.") | ||
|
|
79 | + end | ||
|
62 | end |
|
80 | end |
|
63 | test_request.submitted_at = Time.new |
|
81 | test_request.submitted_at = Time.new |
|
64 | test_request.status_inqueue |
|
82 | test_request.status_inqueue |
@@ -101,4 +119,16 | |||||
|
101 | end |
|
119 | end |
|
102 | new_file_name |
|
120 | new_file_name |
|
103 | end |
|
121 | end |
|
|
122 | + | ||
|
|
123 | + # | ||
|
|
124 | + # validations | ||
|
|
125 | + # | ||
|
|
126 | + def must_have_valid_problem | ||
|
|
127 | + if problem==nil | ||
|
|
128 | + errors.add('problem',"must be specified.") | ||
|
|
129 | + elsif (!problem.available) and (self.new_record?) | ||
|
|
130 | + errors.add('problem',"must be valid.") | ||
|
|
131 | + end | ||
|
|
132 | + end | ||
|
|
133 | + | ||
|
104 | end |
|
134 | end |
You need to be logged in to leave comments.
Login now