Rcov C0 Coverage Information - RCov

app/controllers/api/test_results_controller.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
app/controllers/api/test_results_controller.rb 225 139
25.33%
6.47%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 module Api
2 
3   #=== Description
4   # Allows for pushing of test results into MarkUs (e.g. from automated test runs).
5   # Uses Rails' RESTful routes (check 'rake routes' for the configured routes)
6   class TestResultsController < MainApiController
7 
8     #=== Description
9     # Triggered by a HTTP POST request to /api/test_results(.:format).
10     # Creates a new TestResult instance. Requires the following parameters:
11     #   group_name:   Name of the group to which the test result should be associated to
12     #   assignment:   Short identifier of the assignment
13     #   filename:     Filename of the test result
14     #   file_content: Content of the test results
15     #=== Returns
16     # An XML response, indicating the success/failure for the request
17     def create
18       if !request.post?
19         # pretend this URL does not exist
20         render :file => "#{::Rails.root.to_s}/public/404.html", :status => 404
21         return
22       end
23       if !has_required_http_params_including_file_content?(params)
24         # incomplete/invalid HTTP params
25         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
26         return
27       end
28       # check if there's a valid submission
29       submission = Submission.get_submission_by_group_and_assignment(params[:group_name],
30                                                                       params[:assignment])
31       if submission.nil?
32         # no such submission
33         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
34         return
35       end
36       # Request seems good. Check if filename already exists.
37       # If it does, update it instead of creating a new one.
38       new_test_result = submission.test_results.find_by_filename(params[:filename])
39       if new_test_result.nil?
40         if TestResult.create(:filename => params[:filename],
41            :file_content => params[:file_content],
42            :submission_id => submission.id)
43           # All good, so return a success response
44           render :file => "#{::Rails.root.to_s}/public/200.xml", :status => 200
45           return
46         else
47           # Some other error occurred
48           render :file => "#{::Rails.root.to_s}/public/500.xml", :status => 500
49           return
50         end
51       else
52         new_test_result.file_content = params[:file_content]
53         if new_test_result.save
54           # All good, so return a success response
55           render :file => "#{::Rails.root.to_s}/public/200.xml", :status => 200
56           return
57         else
58           # Some other error occurred
59           render :file => "#{::Rails.root.to_s}/public/500.xml", :status => 500
60           return
61         end
62       end
63     end
64 
65     #=== Description
66     # Triggered by a HTTP DELETE request to /api/test_results(.:format).
67     # Deletes a TestResult instance. Requires the following parameters:
68     #   group_name:   Name of the group to which the test result should be associated to
69     #   assignment:   Short identifier of the assignment
70     #   filename:     Filename of the test result to be deleted
71     #=== Returns
72     # An XML response, indicating the success/failure for the request
73     def destroy
74       if !request.delete?
75         # pretend this URL does not exist
76         render :file => "#{::Rails.root.to_s}/public/404.html", :status => 404
77         return
78       end
79       if !has_required_http_params?(params)
80         # incomplete/invalid HTTP params
81         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
82         return
83       end
84       # check if there's a valid submission
85       submission = Submission.get_submission_by_group_and_assignment(params[:group_name],
86                                                                       params[:assignment])
87       if submission.nil?
88         # no such submission
89         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
90         return
91       end
92       # request seems good
93       test_result = submission.test_results.find_by_filename(params[:filename])
94       if !test_result.nil?
95         if test_result.destroy
96           # Everything went fine; report success
97           render :file => "#{::Rails.root.to_s}/public/200.xml", :status => 200
98           return
99         else
100           # Some other error occurred
101           render :file => "#{::Rails.root.to_s}/public/500.xml", :status => 500
102           return
103         end
104       end
105       # The test result in question does not exist
106       render :file => "#{::Rails.root.to_s}/public/404.xml", :status => 404
107       return
108     end
109 
110     #=== Description
111     # Triggered by a HTTP PUT request to /api/test_results(.:format).
112     # Updates (overwrites) a TestResult instance. Requires the following parameters:
113     #   group_name:   Name of the group to which the test result should be associated to
114     #   assignment:   Short identifier of the assignment
115     #   filename:     Filename of the test result, which content should be updated
116     #   file_content: New content of the test result
117     #=== Returns
118     # An XML response, indicating the success/failure for the request
119     def update
120       if !request.put?
121         # pretend this URL does not exist
122         render :file => "#{::Rails.root.to_s}/public/404.html", :status => 404
123         return
124       end
125       if !has_required_http_params_including_file_content?(params)
126         # incomplete/invalid HTTP params
127         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
128         return
129       end
130       # check if there's a valid submission
131       submission = Submission.get_submission_by_group_and_assignment(params[:group_name],
132                                                                       params[:assignment])
133       if submission.nil?
134         # no such submission
135         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
136         return
137       end
138       # request seems good
139       test_result = submission.test_results.find_by_filename(params[:filename])
140       if !test_result.nil?
141         if test_result.update_file_content(params[:file_content])
142           # Everything went fine; report success
143           render :file => "#{::Rails.root.to_s}/public/200.xml", :status => 200
144           return
145         else
146           # Some other error occurred
147           render :file => "#{::Rails.root.to_s}/public/500.xml", :status => 500
148           return
149         end
150       end
151       # The test result in question does not exist
152       render :file => "#{::Rails.root.to_s}/public/404.xml", :status => 404
153       return
154     end
155 
156     #=== Description
157     # Triggered by a HTTP GET request to /api/test_results(.:format).
158     # Shows a TestResult instance. Requires the following parameters:
159     #   group_name:   Name of the group to which the test result should be associated to
160     #   assignment:   Short identifier of the assignment
161     #   filename:     New filename of the test result
162     #=== Returns
163     # The content of the test result file in question
164     def show
165       if !request.get?
166         # pretend this URL does not exist
167         render :file => "#{::Rails.root.to_s}/public/404.html", :status => 404
168         return
169       end
170       if !has_required_http_params?(params)
171         # incomplete/invalid HTTP params
172         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
173         return
174       end
175       # check if there's a valid submission
176       submission = Submission.get_submission_by_group_and_assignment(params[:group_name],
177                                                                       params[:assignment])
178       if submission.nil?
179         # no such submission
180         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
181         return
182       end
183       # request seems good
184       test_result = submission.test_results.find_by_filename(params[:filename])
185       if !test_result.nil?
186         # Everything went fine; send file_content
187         send_data test_result.file_content, :disposition => 'inline',
188                                             :filename => test_result.filename
189         return
190       end
191       # The test result in question does not exist
192       render :file => "#{::Rails.root.to_s}/public/404.xml", :status => 404
193       return
194     end
195 
196     private
197 
198     # Helper method to check for required HTTP parameters
199     def has_required_http_params?(param_hash)
200       # Note: The blank? method is a Rails extension.
201       # Specific keys have to be present, and their values
202       # must not be blank.
203       if !param_hash[:filename].blank? &&
204    !param_hash[:assignment].blank? &&
205    !param_hash[:group_name].blank?
206   return true
207       else
208         return false
209       end
210     end
211 
212     # Helper method to check for required HTTP parameters including the
213     # file_content parameter
214     def has_required_http_params_including_file_content?(param_hash)
215       if has_required_http_params?(param_hash)
216         if !param_hash[:file_content].blank?
217           return true
218         end
219       end
220       return false
221     end
222 
223   end # end TestResultsController
224 
225 end

Generated on Tue Feb 07 00:07:35 -0500 2012 with rcov 0.9.10