Commit f508ec83723e7a602fe26a08e23be54998d38c68

Authored by Luke Baker
1 parent bdf659e8

cleanup choice tasks

create atomic choice tasks
add has_many :losing_votes to choice
Showing 2 changed files with 80 additions and 58 deletions   Show diff stats
app/models/choice.rb
... ... @@ -9,6 +9,7 @@ class Choice < ActiveRecord::Base
9 9 #validates_length_of :item, :maximum => 140
10 10  
11 11 has_many :votes
  12 + has_many :losing_votes, :class_name => "Vote", :foreign_key => "loser_choice_id"
12 13 has_many :flags
13 14 has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id"
14 15 has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id"
... ...
lib/tasks/test_api.rake
1 1 namespace :test_api do
2 2  
3 3 desc "Run all API tests"
4   - task :all => [:question_vote_consistency]
  4 + task :all => [:question_ids_with_votes_before_2010_02_17, :question_vote_consistency]
5 5  
6 6 namespace :choice do
7   - desc "Ensure that cached prompt counts are valid for a choice"
8   - task :verify_cached_prompt_counts, [:choice_id] => :environment do |t, args|
9   - a = clean_up_args(args)
10   - choices = Choice.find(a[:choice_id])
11   - choices.each do |choice|
12   - puts verify_cached_prompt_counts(choice).inspect
  7 + @choice_tasks = {
  8 + :verify_cached_prompt_counts => "Ensure that cached prompt counts are valid for a choice",
  9 + :verify_choice_appearances_and_votes => "Ensure that an idea: appearances on left + appearances on right >= (wins + losses + skips)",
  10 + :verify_valid_cached_score => "Verify that cached score is valid",
  11 + :verify_cached_score_equals_computed_score => "Verify accurate cached score",
  12 + :verify_wins_equals_vote_wins => "Verify wins equals vote wins",
  13 + :verify_losses_equals_losing_votes => "Verify losses equals losing votes count"
  14 + }
  15 +
  16 + # dynamically create tasks for each choice task
  17 + @choice_tasks.each do |taskname, description|
  18 + desc description
  19 + task taskname, [:choice_id] => [:environment, :question_ids_with_votes_before_2010_02_17] do |t, args|
  20 + a = cleanup_args(args)
  21 + choices = Choice.find(a[:choice_id])
  22 + choices.each do |choice|
  23 + # call task
  24 + puts send(taskname, choice).inspect
  25 + end
  26 + end
  27 + end
  28 +
  29 + def verify_losses_equals_losing_votes(choice)
  30 + success_message = "Choice losses equals losing votes"
  31 + # votes before 2010-02-17 have null loser_choice_id
  32 + # therefore we want to ignore this test for any question with votes
  33 + # prior to 2010-02-17
  34 + return [success_message, false] if @question_ids_with_votes_before_2010_02_17.include?(choice.question_id)
  35 + losing_votes_count = choice.losing_votes.count
  36 + if (choice.losses != losing_votes_count)
  37 + error_message = "Error!: Cached choice losses != actual choice losses for choice #{choice.id}, #{choice.losses} != #{losing_votes_count}\n"
13 38 end
  39 + return error_message.blank? ? [success_message, false] : [error_message, true]
  40 + end
  41 +
  42 + def verify_wins_equals_vote_wins(choice)
  43 + success_message = "Choice wins equals vote wins"
  44 + choice_votes_count = choice.votes.count
  45 + if (choice.wins != choice_votes_count)
  46 + error_message = "Error!: Cached choice wins != actual choice wins for choice #{choice.id}, #{choice_wins} != #{choice_votes_count}\n"
  47 + end
  48 + return error_message.blank? ? [success_message, false] : [error_message, true]
  49 + end
  50 +
  51 + def verify_cached_score_equals_computed_score(choice)
  52 + success_message = "Choice has accurate cached score"
  53 + cached_score = choice.score.to_f
  54 + generated_score = choice.compute_score.to_f
  55 +
  56 + delta = 0.001
  57 +
  58 + if (cached_score - generated_score).abs >= delta
  59 + error_message = "Error! The cached_score is not equal to the calculated score for choice #{choice.id}, cached: #{cached_score}, computed: #{generated_score}\n"
  60 +
  61 + end
  62 + return error_message.blank? ? [success_message, false] : [error_message, true]
  63 + end
  64 +
  65 + def verify_valid_cached_score(choice)
  66 + success_message = "Choice has valid cached score"
  67 + cached_score = choice.score.to_f
  68 + if cached_score == 0.0 || cached_score == 100.0 || cached_score.nil?
  69 + error_message = "Error! The cached_score for choice #{choice.id} is exactly 0 or 100, the value: #{cached_score}"
  70 + end
  71 + return error_message.blank? ? [success_message, false] : [error_message, true]
14 72 end
15 73  
16 74 def verify_cached_prompt_counts(choice)
... ... @@ -21,17 +79,9 @@ namespace :test_api do
21 79 return error_message.blank? ? [success_message, false] : [error_message, true]
22 80 end
23 81  
24   - desc "Ensure that an idea: appearances on left + appearances on right >= (wins + losses + skips)"
25   - task :verify_choice_appearances_and_votes, [:choice_id] => :environment do |t, args|
26   - a = clean_up_args(args)
27   - choices = Choice.find(a[:choice_id])
28   - choices.each do |choice|
29   - puts verify_choice_appearances_and_votes(choice).inspect
30   - end
31   - end
32   -
33 82 def verify_choice_appearances_and_votes(choice)
34 83 success_message = "Choice has more appearances than votes and skips"
  84 + return [success_message, false] if @question_ids_with_votes_before_2010_02_17.include?(choice.question_id)
35 85 all_appearances = choice.appearances_on_the_left.count + choice.appearances_on_the_right.count
36 86 skips = choice.skips_on_the_left.count + choice.skips_on_the_right.count
37 87  
... ... @@ -134,36 +184,6 @@ namespace :test_api do
134 184 total_generated_prompts_on_right += choice.prompts_on_the_right.size
135 185  
136 186 cached_score = choice.score.to_f
137   - generated_score = choice.compute_score.to_f
138   -
139   - delta = 0.001
140   -
141   - if (cached_score - generated_score).abs >= delta
142   - error_message += "Error! The cached_score is not equal to the calculated score for choice #{choice.id}\n"
143   -
144   - print "This score is wrong! #{choice.id} , Question ID: #{question.id}, #{cached_score}, #{generated_score}, updated: #{choice.updated_at}\n"
145   -
146   -
147   - end
148   -
149   - if cached_score == 0.0 || cached_score == 100.0 || cached_score.nil?
150   - error_message += "Error! The cached_score for choice #{choice.id} is exactly 0 or 100, the value: #{cached_score}"
151   - print "Either 0 or 100 This score is wrong! #{choice.id} , Question ID: #{question.id}, #{cached_score}, #{generated_score}, updated: #{choice.updated_at}\n"
152   - end
153   -
154   - unless question_has_votes_before_2010_02_17
155   - message, error_occurred = verify_choice_appearances_and_votes(choice)
156   - if error_occurred
157   - error_message += message + "\n"
158   - end
159   - end
160   -
161   - message, error_occurred = verify_cached_prompt_counts(choice)
162   - if error_occurred
163   - error_message += message + "\n"
164   - end
165   -
166   -
167 187 if cached_score >= 50
168 188 total_scores_gte_fifty +=1
169 189 end
... ... @@ -171,18 +191,10 @@ namespace :test_api do
171 191 total_scores_lte_fifty +=1
172 192 end
173 193  
174   - if (choice.wins != choice.votes.count)
175   - error_message += "Error!: Cached choice wins != actual choice wins for choice #{choice.id}\n"
176   - error_bool= true
177   - end
178   -
179   - # votes before 2010-02-17 have null loser_choice_id
180   - # therefore we want to ignore this test for any question with votes
181   - # prior to 2010-02-17
182   - unless question_has_votes_before_2010_02_17
183   - if (choice.losses != question.votes.count(:conditions => {:loser_choice_id => choice.id}))
184   - error_message += "Error!: Cached choice losses != actual choice losses for choice #{choice.id}\n"
185   - error_bool= true
  194 + @choice_tasks.each do |taskname, description|
  195 + message, error_occurred = send(taskname, question)
  196 + if error_occurred
  197 + error_message += message + "\n"
186 198 end
187 199 end
188 200  
... ... @@ -475,6 +487,15 @@ namespace :test_api do
475 487 end
476 488 # END OF GLOBAL NAMESPACE
477 489  
  490 + # votes before 2010-02-17 have null loser_choice_id therefore we
  491 + # want to ignore some tests for any question with votes before 2010-02-17
  492 + desc "Get all question_ids before 2010_02_17"
  493 + task :question_ids_with_votes_before_2010_02_17 => :environment do
  494 + @question_ids_with_votes_before_2010_02_17 = Vote.find(:all, :select => "DISTINCT(question_id)", :conditions => ["created_at < ?", '2010-02-17']).map {|v| v.question_id}
  495 + puts @question_ids_with_votes_before_2010_02_17.inspect
  496 +
  497 + end
  498 +
478 499 end
479 500  
480 501 def cleanup_args(args)
... ...