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:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

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
@@ -92,38 +92,42
92 prob_opt = find_option_in_source(/^TASK:/,source)
92 prob_opt = find_option_in_source(/^TASK:/,source)
93 if problem = Problem.find_by_name(prob_opt)
93 if problem = Problem.find_by_name(prob_opt)
94 return problem
94 return problem
95 else
95 else
96 return nil
96 return nil
97 end
97 end
98 end
98 end
99
99
100 # validation codes
100 # validation codes
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_to_base("must specify programming language") unless self.language!=nil
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_to_base("must specify problem")
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
121 end
125 end
122
126
123 # callbacks
127 # callbacks
124 def assign_latest_number_if_new_recond
128 def assign_latest_number_if_new_recond
125 return if !self.new_record?
129 return if !self.new_record?
126 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
130 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
127 self.number = (latest==nil) ? 1 : latest.number + 1;
131 self.number = (latest==nil) ? 1 : latest.number + 1;
128 end
132 end
129
133
@@ -14,24 +14,27
14 #
14 #
15
15
16 require 'fileutils'
16 require 'fileutils'
17
17
18 class TestRequest < Task
18 class TestRequest < Task
19
19
20 set_table_name "test_requests"
20 set_table_name "test_requests"
21
21
22 belongs_to :user
22 belongs_to :user
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
29
32
30 def language
33 def language
31 self.submission.language
34 self.submission.language
32 end
35 end
33
36
34 def self.get_inqueue_and_change_status(status)
37 def self.get_inqueue_and_change_status(status)
35 # since there will be only one grader grading TestRequest
38 # since there will be only one grader grading TestRequest
36 # we do not need locking (hopefully)
39 # we do not need locking (hopefully)
37
40
@@ -41,33 +44,48
41 if test_request!=nil
44 if test_request!=nil
42 test_request.status = status
45 test_request.status = status
43 test_request.save!
46 test_request.save!
44 end
47 end
45
48
46 test_request
49 test_request
47 end
50 end
48
51
49 # interfacing with form
52 # interfacing with form
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 - params[:submission_number])
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
65 test_request
83 test_request
66 end
84 end
67
85
68 protected
86 protected
69
87
70 def self.name_of(problem)
88 def self.name_of(problem)
71 if problem!=nil
89 if problem!=nil
72 problem.name
90 problem.name
73 else
91 else
@@ -92,13 +110,25
92 return nil if tempfile==""
110 return nil if tempfile==""
93
111
94 if tempfile.instance_of?(Tempfile)
112 if tempfile.instance_of?(Tempfile)
95 tempfile.close
113 tempfile.close
96 FileUtils.move(tempfile.path,new_file_name)
114 FileUtils.move(tempfile.path,new_file_name)
97 else
115 else
98 File.open(new_file_name, "wb") do |f|
116 File.open(new_file_name, "wb") do |f|
99 f.write(tempfile.read)
117 f.write(tempfile.read)
100 end
118 end
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