Commit 211b3882953e0a0fab9ec65ec743801a894b80fd

Authored by Dhruv Kapadia
1 parent 8fd2c40f

Added api consistency tests for left /right , appearances

Showing 1 changed file with 53 additions and 2 deletions   Show diff stats
lib/tasks/test_api.rake
... ... @@ -7,10 +7,13 @@ namespace :test_api do
7 7 questions = Question.find(:all)
8 8  
9 9 error_msg = ""
  10 +
10 11 questions.each do |question|
11 12  
12 13 total_wins =0
13 14 total_votes =0
  15 + total_generated_prompts_on_left = 0
  16 + total_generated_prompts_on_right = 0
14 17 error_bool = false
15 18 question.choices.each do |choice|
16 19  
... ... @@ -22,8 +25,11 @@ namespace :test_api do
22 25 if choice.losses
23 26 total_votes += choice.losses
24 27 end
25   - end
26 28  
  29 + total_generated_prompts_on_left += choice.prompts_on_the_left.size
  30 + total_generated_prompts_on_right += choice.prompts_on_the_right.size
  31 + end
  32 +
27 33 if (2*total_wins != total_votes)
28 34 error_msg += "Error 1: 2 x Total Wins != Total votes"
29 35 error_bool= true
... ... @@ -39,6 +45,40 @@ namespace :test_api do
39 45 error_bool = true
40 46 end
41 47  
  48 + if(total_generated_prompts_on_right != total_generated_prompts_on_right)
  49 + error_msg += "Error 4: Total generated prompts on left != Total generated prompts on right"
  50 + error_bool = true
  51 + end
  52 +
  53 + wins_by_choice_id = question.votes.count(:group => :choice_id)
  54 + losses_by_choice_id= question.votes.count(:conditions => "loser_choice_id IS NOT NULL", :group => :loser_choice_id)
  55 +
  56 + #Rails returns an ordered hash, which doesn't allow for blocks to change merging logic.
  57 + #A little hack to create a normal hash
  58 + wins_hash = {}
  59 + wins_hash.merge!(wins_by_choice_id)
  60 + losses_hash = {}
  61 + losses_hash.merge!(losses_by_choice_id)
  62 +
  63 +
  64 + appearances_by_choice_id = wins_hash.merge(losses_hash) do |key, oldval, newval| oldval + newval end
  65 +
  66 + sum = total_appearances = appearances_by_choice_id.values.inject(:+)
  67 + mean = average_appearances = total_appearances.to_f / appearances_by_choice_id.size.to_f
  68 +
  69 + if sum:
  70 + stddev = Math.sqrt( appearances_by_choice_id.values.inject(0) { |sum, e| sum + (e - mean) ** 2 } / appearances_by_choice_id.size.to_f )
  71 +
  72 + appearances_by_choice_id.each do |choice_id, n_i|
  73 + if (n_i < (mean - 6*stddev)) || (n_i > mean + 6 *stddev)
  74 + error_msg += "Choice #{choice_id} in Question ##{question.id} has an irregular number of appearances: #{n_i}, as compared to the mean: #{mean} and stddev #{stddev} for this question"
  75 + error_bool = true
  76 + end
  77 + end
  78 + end
  79 +
  80 +
  81 +
42 82 if error_bool
43 83 error_msg += "Question #{question.id}: 2*wins = #{2*total_wins}, total votes = #{total_votes}, vote_count = #{question.votes_count}\n"
44 84 end
... ... @@ -46,7 +86,18 @@ namespace :test_api do
46 86 end
47 87  
48 88 if error_msg.blank?
49   - CronMailer.deliver_info_message(CRON_EMAIL, "Test of API Vote Consistency passed", "Examined #{questions.length} questions and found no irregularities")
  89 +
  90 + success_msg = "Conducted the following tests on API data and found no inconsistencies.\n" +
  91 + "For each of the #{questions.length} questions in the database: \n" +
  92 + " 2 x Total Wins = Total Votes " +
  93 + " Total Votes (wins + losses) is Even" +
  94 + " Total Votes (wins + losses) = 2 x the number of vote objects that belong to the question" +
  95 + " Total generated prompts on left = Total generated prompts on right" +
  96 + " Each choice has appeared n times, where n falls within 6 stddevs of the mean number of appearances for a question"
  97 +
  98 + print success_msg
  99 +
  100 + CronMailer.deliver_info_message(CRON_EMAIL, "Test of API Vote Consistency passed", success_msg)
50 101 else
51 102 CronMailer.deliver_info_message("#{CRON_EMAIL},#{ERRORS_EMAIL}", "Error! Failure of API Vote Consistency " , error_msg)
52 103 end
... ...