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
@@ -80,51 +80,55
80 80 def self.find_language_in_source(source)
81 81 langopt = find_option_in_source(/^LANG:/,source)
82 82 if language = Language.find_by_name(langopt)
83 83 return language
84 84 elsif language = Language.find_by_pretty_name(langopt)
85 85 return language
86 86 else
87 87 return nil
88 88 end
89 89 end
90 90
91 91 def self.find_problem_in_source(source)
92 92 prob_opt = find_option_in_source(/^TASK:/,source)
93 93 if problem = Problem.find_by_name(prob_opt)
94 94 return problem
95 95 else
96 96 return nil
97 97 end
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 - 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 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
124 128 def assign_latest_number_if_new_recond
125 129 return if !self.new_record?
126 130 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
127 131 self.number = (latest==nil) ? 1 : latest.number + 1;
128 132 end
129 133
130 134 end
@@ -2,103 +2,133
2 2 # A TestRequest is a composition of submission with user's testdata.
3 3 #
4 4 # Note about TestRequest#problem: Usually, A TestRequest has to be
5 5 # associated with a problem, so that execution environment can be
6 6 # determined. However, to be more flexible, we have to ensure that
7 7 # it works as well with problem=nil. In this case, we shall provide
8 8 # a "default" execution environment for it. This can be done
9 9 # seamlessly by using TestRequest#problem_name or
10 10 # TestRequest#name_of(problem) when retrieving the name of the
11 11 # problem: #name_of would return problem.name when problem!=nil and
12 12 # it would return "default" when problem=nil, #problem_name just
13 13 # call #name_of.
14 14 #
15 15
16 16 require 'fileutils'
17 17
18 18 class TestRequest < Task
19 19
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
32 35 end
33 36
34 37 def self.get_inqueue_and_change_status(status)
35 38 # since there will be only one grader grading TestRequest
36 39 # we do not need locking (hopefully)
37 40
38 41 test_request = TestRequest.find(:first,
39 42 :order => "created_at",
40 43 :conditions => {:status=> Task::STATUS_INQUEUE})
41 44 if test_request!=nil
42 45 test_request.status = status
43 46 test_request.save!
44 47 end
45 48
46 49 test_request
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
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 61 test_request.problem = problem
55 - test_request.submission =
56 - Submission.find_by_user_problem_number(user.id,
57 - problem.id,
58 - params[:submission_number])
59 - test_request.input_file_name = save_input_file(params[:input_file], user, problem)
60 - if test_request.input_file_name == nil
62 + if problem!=nil
63 + test_request.submission =
64 + Submission.find_by_user_problem_number(user.id,
65 + problem.id,
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]==""
61 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 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
69 87
70 88 def self.name_of(problem)
71 89 if problem!=nil
72 90 problem.name
73 91 else
74 92 "default"
75 93 end
76 94 end
77 95
78 96 def self.random_input_file_name(user,problem)
79 97 problem_name = TestRequest.name_of(problem)
80 98 begin
81 99 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
82 100 end while File.exists?(tmpname)
83 101 tmpname
84 102 end
85 103
86 104 def self.save_input_file(tempfile, user, problem)
87 105 new_file_name = random_input_file_name(user,problem)
88 106 dirname = File.dirname(new_file_name)
89 107 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
90 108
91 109 # when the user did not submit any file
92 110 return nil if tempfile==""
93 111
94 112 if tempfile.instance_of?(Tempfile)
95 113 tempfile.close
96 114 FileUtils.move(tempfile.path,new_file_name)
97 115 else
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.")
131 + end
132 + end
133 +
104 134 end
You need to be logged in to leave comments. Login now