Commit 00c7d88ca44175c80fc5f70adbe17912ca84f726

Authored by Dhruv Kapadia
1 parent 4efdb207

More api consistency checks

Showing 1 changed file with 46 additions and 15 deletions   Show diff stats
lib/tasks/test_api.rake
@@ -45,9 +45,7 @@ namespace :test_api do @@ -45,9 +45,7 @@ namespace :test_api do
45 #this is not elegant, but should only be run once, so quick and dirty wins 45 #this is not elegant, but should only be run once, so quick and dirty wins
46 46
47 start_date = Vote.find(:all, :conditions => 'loser_choice_id IS NOT NULL', :order => :created_at, :limit => 1).first.created_at.to_date 47 start_date = Vote.find(:all, :conditions => 'loser_choice_id IS NOT NULL', :order => :created_at, :limit => 1).first.created_at.to_date
48 - end_date = Appearance.first.created_at.to_date  
49 -  
50 - start_date.upto(end_date-1) do |the_date| 48 + start_date.upto(Date.today) do |the_date|
51 questions = Question.find(:all) 49 questions = Question.find(:all)
52 50
53 print the_date.to_s 51 print the_date.to_s
@@ -120,7 +118,6 @@ namespace :test_api do @@ -120,7 +118,6 @@ namespace :test_api do
120 118
121 desc "Should only need to be run once" 119 desc "Should only need to be run once"
122 task(:generate_all_possible_prompts => :environment) do 120 task(:generate_all_possible_prompts => :environment) do
123 - inserts = []  
124 Question.find(:all).each do |q| 121 Question.find(:all).each do |q|
125 choices = q.choices 122 choices = q.choices
126 if q.prompts.size > choices.size**2 - choices.size 123 if q.prompts.size > choices.size**2 - choices.size
@@ -133,8 +130,10 @@ namespace :test_api do @@ -133,8 +130,10 @@ namespace :test_api do
133 print "#{q.id} should add #{(choices.size ** 2 - choices.size) - q.prompts.size}\n" 130 print "#{q.id} should add #{(choices.size ** 2 - choices.size) - q.prompts.size}\n"
134 131
135 end 132 end
136 - timestring = Time.now.to_s(:db) #isn't rails awesome? 133 + created_timestring = q.created_at.to_s(:db)
  134 + updated_timestring = Time.now.to_s(:db) #isn't rails awesome?
137 promptscount=0 135 promptscount=0
  136 + inserts = []
138 the_prompts = Prompt.find(:all, :select => 'id, left_choice_id, right_choice_id', :conditions => {:question_id => q.id}) 137 the_prompts = Prompt.find(:all, :select => 'id, left_choice_id, right_choice_id', :conditions => {:question_id => q.id})
139 138
140 the_prompts_hash = {} 139 the_prompts_hash = {}
@@ -151,8 +150,7 @@ namespace :test_api do @@ -151,8 +150,7 @@ namespace :test_api do
151 keystring = "#{l.id},#{r.id}" 150 keystring = "#{l.id},#{r.id}"
152 p = the_prompts_hash[keystring] 151 p = the_prompts_hash[keystring]
153 if p.nil? 152 if p.nil?
154 - print "."  
155 - inserts.push("(NULL, #{q.id}, NULL, #{l.id}, '#{timestring}', '#{timestring}', NULL, 0, #{r.id}, NULL, NULL)") 153 + inserts.push("(NULL, #{q.id}, NULL, #{l.id}, '#{created_timestring}', '#{updated_timestring}', NULL, 0, #{r.id}, NULL, NULL)")
156 promptscount+=1 154 promptscount+=1
157 end 155 end
158 156
@@ -162,16 +160,17 @@ namespace :test_api do @@ -162,16 +160,17 @@ namespace :test_api do
162 end 160 end
163 161
164 print "Added #{promptscount} to #{q.id}\n" 162 print "Added #{promptscount} to #{q.id}\n"
  163 + sql = "INSERT INTO `prompts` (`algorithm_id`, `question_id`, `voter_id`, `left_choice_id`, `created_at`, `updated_at`, `tracking`, `votes_count`, `right_choice_id`, `active`, `randomkey`) VALUES #{inserts.join(', ')}"
  164 + unless inserts.empty?
  165 + ActiveRecord::Base.connection.execute(sql)
  166 + end
  167 +
  168 + Question.update_counters(q.id, :prompts_count => promptscount)
165 169
166 - Question.update_counters(q.id, :prompts_count => promptscount)  
167 170
168 end 171 end
169 172
170 - sql = "INSERT INTO `prompts` (`algorithm_id`, `question_id`, `voter_id`, `left_choice_id`, `created_at`, `updated_at`, `tracking`, `votes_count`, `right_choice_id`, `active`, `randomkey`) VALUES #{inserts.join(', ')}"  
171 173
172 - unless inserts.empty?  
173 - ActiveRecord::Base.connection.execute(sql)  
174 - end  
175 174
176 end 175 end
177 176
@@ -242,10 +241,12 @@ namespace :test_api do @@ -242,10 +241,12 @@ namespace :test_api do
242 total_generated_prompts_on_left += choice.prompts_on_the_left.size 241 total_generated_prompts_on_left += choice.prompts_on_the_left.size
243 total_generated_prompts_on_right += choice.prompts_on_the_right.size 242 total_generated_prompts_on_right += choice.prompts_on_the_right.size
244 243
245 - cached_score = choice.score  
246 - generated_score = choice.compute_score 244 + cached_score = choice.score.to_f
  245 + generated_score = choice.compute_score.to_f
  246 +
  247 + delta = 0.001
247 248
248 - if cached_score.round != generated_score.round 249 + if (cached_score - generated_score).abs >= delta
249 error_msg += "Error! The cached_score is not equal to the calculated score for choice #{choice.id}" 250 error_msg += "Error! The cached_score is not equal to the calculated score for choice #{choice.id}"
250 251
251 print "This score is wrong! #{choice.id} , Question ID: #{question.id}, #{cached_score}, #{generated_score}, updated: #{choice.updated_at}\n" 252 print "This score is wrong! #{choice.id} , Question ID: #{question.id}, #{cached_score}, #{generated_score}, updated: #{choice.updated_at}\n"
@@ -267,6 +268,7 @@ namespace :test_api do @@ -267,6 +268,7 @@ namespace :test_api do
267 total_scores_lte_fifty +=1 268 total_scores_lte_fifty +=1
268 end 269 end
269 270
  271 +
270 end 272 end
271 273
272 if (2*total_wins != total_votes) 274 if (2*total_wins != total_votes)
@@ -323,6 +325,31 @@ namespace :test_api do @@ -323,6 +325,31 @@ namespace :test_api do
323 end 325 end
324 end 326 end
325 327
  328 + # Checks that counter_cache is working as expected
  329 + cached_prompts_size = question.prompts.size
  330 + actual_prompts_size = question.prompts.count
  331 +
  332 + if cached_prompts_size != actual_prompts_size
  333 + error_msg += "Error! Question #{question.id} has an inconsistent # of prompts! cached#: #{cached_prompts_size}, actual#: #{actual_prompts_size}\n"
  334 + end
  335 +
  336 + cached_votes_size = question.votes.size
  337 + actual_votes_size = question.votes.count
  338 +
  339 + if cached_votes_size != actual_votes_size
  340 + error_msg += "Error! Question #{question.id} has an inconsistent # of votes! cached#: #{cached_votes_size}, actual#: #{actual_votes_size}\n"
  341 + end
  342 +
  343 + cached_choices_size = question.choices.size
  344 + actual_choices_size = question.choices.count
  345 +
  346 + if cached_choices_size != actual_choices_size
  347 + error_msg += "Error! Question #{question.id} has an inconsistent # of choices! cached#: #{cached_choices_size}, actual#: #{actual_choices_size}\n"
  348 + end
  349 +
  350 + if cached_prompts_size != question.choices.size **2 - question.choices.size
  351 + error_msg += "Error! Question #{question.id} has an incorrect number of prompts! Expected #{question.choices.size **2 - question.choices.size}, Actual: #{cached_prompts_size}\n"
  352 + end
326 353
327 354
328 if error_bool 355 if error_bool
@@ -383,6 +410,10 @@ namespace :test_api do @@ -383,6 +410,10 @@ namespace :test_api do
383 " Each choice has appeared n times, where n falls within 6 stddevs of the mean number of appearances for a question\n" + 410 " Each choice has appeared n times, where n falls within 6 stddevs of the mean number of appearances for a question\n" +
384 " Note: this applies only to seed choices (not user submitted) and choices currently marked active\n" + 411 " Note: this applies only to seed choices (not user submitted) and choices currently marked active\n" +
385 " The cached score value matches the calculated score value for each choice\n" + 412 " The cached score value matches the calculated score value for each choice\n" +
  413 + " The cached vote count matches the actual number of votes for each question\n" +
  414 + " The cached choices count matches the actual number of choices for each question\n" +
  415 + " The cached prompt count matches the actual number of prompts for each question\n" +
  416 + " The prompt count matches the expected number of prompts ( num_choices ^2 - num choices) for each question\n" +
386 " All Vote objects have an associated appearance object\n" + 417 " All Vote objects have an associated appearance object\n" +
387 " All Vote objects have an client response time < calculated server roundtrip time\n" 418 " All Vote objects have an client response time < calculated server roundtrip time\n"
388 419