Commit 3dd71e9aa6b44b6360cf97391f3823e9e97449e4

Authored by Luke Baker
1 parent f508ec83

convert 5 more tests into question tasks

Showing 1 changed file with 93 additions and 100 deletions   Show diff stats
lib/tasks/test_api.rake
... ... @@ -100,15 +100,6 @@ namespace :test_api do
100 100  
101 101 questions.each do |question|
102 102  
103   - message, error_occurred = check_basic_balanced_stats(question)
104   - # hack for now, get around to doing this with block/yield to
105   - # get rid of duplication
106   - if error_occurred
107   - errors << message
108   - else
109   - successes << message
110   - end
111   -
112 103 @question_tasks.each do |taskname, description|
113 104 message, error_occurred = send(taskname, question)
114 105 if error_occurred
... ... @@ -118,6 +109,17 @@ namespace :test_api do
118 109 end
119 110 end
120 111  
  112 + question.choices.each do |choice|
  113 + @choice_tasks.each do |taskname, description|
  114 + message, error_occurred = send(taskname, question)
  115 + if error_occurred
  116 + errors << message
  117 + else
  118 + successes << message
  119 + end
  120 + end
  121 + end
  122 +
121 123 end
122 124  
123 125 @global_tasks.each do |taskname, description|
... ... @@ -150,95 +152,6 @@ namespace :test_api do
150 152  
151 153 end
152 154  
153   - def check_basic_balanced_stats(question)
154   - error_message = ""
155   - success_message = "2 x Total Wins = Total Votes\n" +
156   - "Total Votes (wins + losses) is Even\n" +
157   - "Total Votes (wins + losses) = 2 x the number of vote objects that belong to the question\n" +
158   - "Total generated prompts on left = Total generated prompts on right"
159   - total_wins =0
160   - total_votes =0
161   - total_generated_prompts_on_left = 0
162   - total_generated_prompts_on_right = 0
163   - total_scores_gte_fifty= 0
164   - total_scores_lte_fifty= 0
165   - error_bool = false
166   - # votes before 2010-02-17 have null loser_choice_id therefore we
167   - # want to ignore some tests for any question with votes before 2010-02-17
168   - question_has_votes_before_2010_02_17 = question.votes.count(:conditions => ["created_at < ?", '2010-02-17']) > 0
169   -
170   - # reload question to make sure we have most recent data
171   - question.reload
172   - question.choices.each do |choice|
173   -
174   - if choice.wins
175   - total_wins += choice.wins
176   - total_votes += choice.wins
177   - end
178   -
179   - if choice.losses
180   - total_votes += choice.losses
181   - end
182   -
183   - total_generated_prompts_on_left += choice.prompts_on_the_left.size
184   - total_generated_prompts_on_right += choice.prompts_on_the_right.size
185   -
186   - cached_score = choice.score.to_f
187   - if cached_score >= 50
188   - total_scores_gte_fifty +=1
189   - end
190   - if cached_score <= 50
191   - total_scores_lte_fifty +=1
192   - end
193   -
194   - @choice_tasks.each do |taskname, description|
195   - message, error_occurred = send(taskname, question)
196   - if error_occurred
197   - error_message += message + "\n"
198   - end
199   - end
200   -
201   - end
202   -
203   -
204   - unless question_has_votes_before_2010_02_17
205   - if (2*total_wins != total_votes)
206   - error_message += "Error 1: 2 x Total Wins != Total votes\n"
207   - error_bool= true
208   - end
209   -
210   - if(total_votes % 2 != 0)
211   - error_message += "Error 2: Total votes is not Even!\n"
212   - error_bool= true
213   - end
214   -
215   - if(total_votes != 2* question.votes_count)
216   - error_message += "Error 3: Total votes != 2 x # vote objects\n"
217   - error_bool = true
218   - end
219   - end
220   -
221   - if(total_generated_prompts_on_right != total_generated_prompts_on_right)
222   - error_message += "Error 4: Total generated prompts on left != Total generated prompts on right\n"
223   - error_bool = true
224   - end
225   -
226   - unless question_has_votes_before_2010_02_17
227   - if(total_scores_lte_fifty == question.choices.size || total_scores_gte_fifty == question.choices.size) && (total_scores_lte_fifty != total_scores_gte_fifty)
228   - error_message += "Error: The scores of all choices are either all above 50, or all below 50. This is probably wrong\n"
229   - error_bool = true
230   - puts "Error score fifty: #{question.id}"
231   - end
232   - end
233   -
234   - if error_bool
235   - error_message += "Question #{question.id}: 2*wins = #{2*total_wins}, total votes = #{total_votes}, vote_count = #{question.votes_count}\n"
236   - end
237   - return error_message.blank? ? [success_message, false] : [error_message, true]
238   - end
239   -
240   -
241   -
242 155 namespace :question do
243 156 # use this to dynamically create rake task for each question test
244 157 @question_tasks = {
... ... @@ -246,13 +159,18 @@ namespace :test_api do
246 159 :check_each_choice_appears_within_n_stddevs => "Ensure each choice appears within 6 standard deviations",
247 160 :check_each_choice_equally_likely_to_appear_left_or_right => "Ensure each choice is equally likely to appear on left or right",
248 161 :check_prompt_cache_hit_rate => "Check prompt cache hit rate",
249   - :check_object_counter_cache_values_match_actual_values => "Check that object counter cache values match actual values"
  162 + :check_object_counter_cache_values_match_actual_values => "Check that object counter cache values match actual values",
  163 + :wins_and_losses_equals_two_times_wins => "Verifies that wins and losses are equal to 2 times the total number of wins",
  164 + :wins_and_losses_is_even => "Verify that sum of wins and losses is even",
  165 + :wins_and_losses_equals_two_times_vote_count => "Verify that sum of wins and losses equals two times the vote count",
  166 + :check_scores_over_above_fifty => "Check that there are some scores above fifty and some below",
  167 + :generated_prompts_on_each_side_are_equal => "Verify that count of generated prompts on each side is equal"
250 168 }
251 169  
252 170 # dynamically create tasks for each question task
253 171 @question_tasks.each do |taskname, description|
254 172 desc description
255   - task taskname, [:question_id] => :environment do |t, args|
  173 + task taskname, [:question_id] => [:environment, :question_ids_with_votes_before_2010_02_17] do |t, args|
256 174 a = cleanup_args(args)
257 175 questions = Question.find(a[:question_id])
258 176 questions.each do |question|
... ... @@ -262,6 +180,80 @@ namespace :test_api do
262 180 end
263 181 end
264 182  
  183 + def generated_prompts_on_each_side_are_equal(question)
  184 + success_message = "Number of generated prompts on left are equal to number generated on right"
  185 + generated_on_left = Choice.connection.select_one("
  186 + SELECT COUNT(*) AS total FROM prompts
  187 + WHERE question_id = #{question.id} AND left_choice_id IN (SELECT id from choices where question_id = #{question.id})")
  188 + generated_on_right = Choice.connection.select_one("
  189 + SELECT COUNT(*) AS total FROM prompts
  190 + WHERE question_id = #{question.id} AND right_choice_id IN (SELECT id from choices where question_id = #{question.id})")
  191 + if (generated_on_left["total"] != generated_on_right["total"])
  192 + error_message = "Error 4: Total generated prompts on left != Total generated prompts on right"
  193 + end
  194 + return error_message.blank? ? [success_message, false] : [error_message, true]
  195 + end
  196 +
  197 + def check_scores_over_above_fifty(question)
  198 + success_message = "Scores are distributed above and below 50"
  199 + return [success_message, false] if @question_ids_with_votes_before_2010_02_17.include?(question.id)
  200 + totals_lte_fifty = Choice.connection.select_one("
  201 + SELECT COUNT(*) AS total FROM choices
  202 + WHERE question_id = #{question.id} AND score <= 50")
  203 + totals_gte_fifty = Choice.connection.select_one("
  204 + SELECT COUNT(*) AS total FROM choices
  205 + WHERE question_id = #{question.id} AND score >= 50")
  206 + total_scores_lte_fifty = totals_lte_fifty["total"]
  207 + total_scores_gte_fifty = totals_gte_fifty["total"]
  208 + question_choices_count = question.choices.count
  209 + if (total_scores_lte_fifty == question_choices_count || total_scores_gte_fifty == question_choices_count) && (total_scores_lte_fifty != total_scores_gte_fifty)
  210 + error_message = "Error! Question #{question.id}: The scores of all choices are either all above 50, or all below 50. This is probably wrong"
  211 + end
  212 + return error_message.blank? ? [success_message, false] : [error_message, true]
  213 + end
  214 +
  215 + def wins_and_losses_equals_two_times_vote_count(question)
  216 + success_message = "Wins and losses equals 2 times vote count"
  217 + return [success_message, false] if @question_ids_with_votes_before_2010_02_17.include?(question.id)
  218 + totals = Question.connection.select_one("
  219 + SELECT SUM(wins + losses) AS total,
  220 + SUM(wins) AS total_wins,
  221 + SUM(losses) AS total_losses FROM choices
  222 + WHERE question_id = #{question.id}")
  223 + if(totals["total"] != 2* question.votes_count)
  224 + error_message = "Error: Total votes != 2 x # vote objects"
  225 + end
  226 + return error_message.blank? ? [success_message, false] : [error_message, true]
  227 + end
  228 +
  229 + def wins_and_losses_is_even(question)
  230 + success_message = "Total Votes is even"
  231 + return [success_message, false] if @question_ids_with_votes_before_2010_02_17.include?(question.id)
  232 + totals = Question.connection.select_one("
  233 + SELECT SUM(wins + losses) AS total,
  234 + SUM(wins) AS total_wins,
  235 + SUM(losses) AS total_losses FROM choices
  236 + WHERE question_id = #{question.id}")
  237 + if (totals["total"] % 2 != 0)
  238 + error_message = "Error: Total votes is not Even!"
  239 + end
  240 + return error_message.blank? ? [success_message, false] : [error_message, true]
  241 + end
  242 +
  243 + def wins_and_losses_equals_two_times_wins(question)
  244 + success_message = "2 x Total Wins == Total Votes"
  245 + return [success_message, false] if @question_ids_with_votes_before_2010_02_17.include?(question.id)
  246 + totals = Question.connection.select_one("
  247 + SELECT SUM(wins + losses) AS total,
  248 + SUM(wins) AS total_wins,
  249 + SUM(losses) AS total_losses FROM choices
  250 + WHERE question_id = #{question.id}")
  251 + if (2*totals["total_wins"] != totals["total"])
  252 + error_message = "Error: 2 x Total Wins != Total votes"
  253 + end
  254 + return error_message.blank? ? [success_message, false] : [error_message, true]
  255 + end
  256 +
265 257 def answered_appearances_equals_votes_and_skips(question)
266 258 error_message = ""
267 259 success_message = "All vote and skip objects have an associated appearance object"
... ... @@ -370,6 +362,7 @@ namespace :test_api do
370 362 end
371 363  
372 364 def check_object_counter_cache_values_match_actual_values(question)
  365 + # FIXME: split up into several tests
373 366 error_message = ""
374 367 success_message = "All cached object values match actual values within database"
375 368 # Checks that counter_cache is working as expected
... ...