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: 76 inserted, 3 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
@@ -98,26 +98,30
98 98 end
99 99
100 100 # validation codes
101 101 def must_specify_language
102 102 return if self.source==nil
103 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 105 end
106 106
107 107 def must_have_valid_problem
108 108 return if self.source==nil
109 109 if self.problem_id!=-1
110 + begin
110 111 problem = Problem.find(self.problem_id)
112 + rescue ActiveRecord::RecordNotFound
113 + problem = nil
114 + end
111 115 else
112 116 problem = Submission.find_problem_in_source(self.source)
113 117 end
114 118 if problem==nil
115 - errors.add_to_base("must specify problem")
119 + errors.add('problem',"must be specified.")
116 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 122 else
119 123 self.problem = problem
120 124 end
121 125 end
122 126
123 127 # callbacks
@@ -20,12 +20,15
20 20 set_table_name "test_requests"
21 21
22 22 belongs_to :user
23 23 belongs_to :problem
24 24 belongs_to :submission
25 25
26 + validates_presence_of :submission
27 + validate :must_have_valid_problem
28 +
26 29 def problem_name
27 30 TestRequest.name_of(self.problem)
28 31 end
29 32
30 33 def language
31 34 self.submission.language
@@ -47,22 +50,37
47 50 end
48 51
49 52 # interfacing with form
50 53 def self.new_from_form_params(user,params)
51 54 test_request = TestRequest.new
52 55 test_request.user = user
56 + begin
53 57 problem = Problem.find(params[:problem_id])
58 + rescue ActiveRecord::RecordNotFound
59 + problem = nil
60 + end
54 61 test_request.problem = problem
62 + if problem!=nil
55 63 test_request.submission =
56 64 Submission.find_by_user_problem_number(user.id,
57 65 problem.id,
58 66 params[:submission_number])
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]==""
73 + test_request.errors.add_to_base("No input submitted.")
74 + test_request.input_file_name = nil
75 + else
59 76 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
60 77 if test_request.input_file_name == nil
61 78 test_request.errors.add_to_base("No input submitted.")
62 79 end
80 + end
63 81 test_request.submitted_at = Time.new
64 82 test_request.status_inqueue
65 83 test_request
66 84 end
67 85
68 86 protected
@@ -98,7 +116,19
98 116 File.open(new_file_name, "wb") do |f|
99 117 f.write(tempfile.read)
100 118 end
101 119 end
102 120 new_file_name
103 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.")
104 131 end
132 + end
133 +
134 + end
You need to be logged in to leave comments. Login now