Commit bdf659e8d550825a64e81d5e067a5ef45073474a

Authored by Luke Baker
1 parent 267fdfec

add global tasks hash

dynamically create global rake tasks
call global tasks from all
Showing 1 changed file with 76 additions and 75 deletions   Show diff stats
lib/tasks/test_api.rake
@@ -3,40 +3,6 @@ namespace :test_api do @@ -3,40 +3,6 @@ namespace :test_api do
3 desc "Run all API tests" 3 desc "Run all API tests"
4 task :all => [:question_vote_consistency] 4 task :all => [:question_vote_consistency]
5 5
6 - desc "Ensure all appearance and votes have matching prompt_ids"  
7 - task :verify_appearance_vote_prompt_ids => :environment do  
8 - puts verify_appearance_vote_prompt_ids().inspect  
9 - end  
10 -  
11 - def verify_appearance_vote_prompt_ids  
12 - bad_records = Vote.connection.select_all "  
13 - SELECT votes.id  
14 - FROM votes LEFT JOIN appearances  
15 - ON (votes.id = appearances.answerable_id  
16 - AND appearances.answerable_type = 'Vote')  
17 - WHERE votes.prompt_id <> appearances.prompt_id"  
18 - success_message = "Appearance and vote prompt_ids match"  
19 - error_message = bad_records.map do |record|  
20 - "Vote ##{record["id"]} has a different prompt_id than its appearance."  
21 - end  
22 - error_message = error_message.join "\n"  
23 - return error_message.blank? ? [success_message, false] : [error_message, true]  
24 - end  
25 -  
26 - desc "Ensure that all choices have 0 <= score <= 100"  
27 - task :verify_range_of_choices_scores => :environment do  
28 - puts verify_range_of_choices_scores().inspect  
29 - end  
30 -  
31 - def verify_range_of_choices_scores  
32 - bad_choices_count = Choice.count(:conditions => 'score < 0 OR score > 100')  
33 - success_message = "All choices have a score within 0-100"  
34 - if bad_choices_count > 0  
35 - error_message = "Some choices have a score less than 0 or greater than 100"  
36 - end  
37 - return error_message.blank? ? [success_message, false] : [error_message, true]  
38 - end  
39 -  
40 namespace :choice do 6 namespace :choice do
41 desc "Ensure that cached prompt counts are valid for a choice" 7 desc "Ensure that cached prompt counts are valid for a choice"
42 task :verify_cached_prompt_counts, [:choice_id] => :environment do |t, args| 8 task :verify_cached_prompt_counts, [:choice_id] => :environment do |t, args|
@@ -104,24 +70,13 @@ namespace :test_api do @@ -104,24 +70,13 @@ namespace :test_api do
104 70
105 end 71 end
106 72
107 - message, error_occurred = response_time_tests  
108 -  
109 - if error_occurred  
110 - errors << message  
111 - else  
112 - successes << message  
113 - end  
114 - message, error_occurred = verify_range_of_choices_scores  
115 - if error_occurred  
116 - errors << message  
117 - else  
118 - successes << message  
119 - end  
120 - message, error_occurred = verify_appearance_vote_prompt_ids  
121 - if error_occurred  
122 - errors << message  
123 - else  
124 - successes << message 73 + @global_tasks.each do |taskname, description|
  74 + message, error_occurred = send(taskname)
  75 + if error_occurred
  76 + errors << message
  77 + else
  78 + successes << message
  79 + end
125 end 80 end
126 81
127 email_text = "Conducted the following tests on API data and found the following results\n" + "For each of the #{questions.length} questions in the database: \n" 82 email_text = "Conducted the following tests on API data and found the following results\n" + "For each of the #{questions.length} questions in the database: \n"
@@ -436,44 +391,90 @@ namespace :test_api do @@ -436,44 +391,90 @@ namespace :test_api do
436 391
437 # END OF QUESTION NAMESPACE 392 # END OF QUESTION NAMESPACE
438 393
  394 + namespace :global do
  395 + @global_tasks = {
  396 + :response_time_tests => "Verify all vote objects have accurate response time",
  397 + :verify_appearance_vote_prompt_ids => "Ensure all appearance and votes have matching prompt_ids",
  398 + :verify_range_of_choices_scores => "Ensure that all choices have 0 <= score <= 100"
  399 + }
439 400
440 - def response_time_tests  
441 - error_message = ""  
442 - success_message = "All Vote objects have an client response time < calculated server roundtrip time\n"  
443 -  
444 - recording_client_time_start_date = Vote.find(:all, :conditions => 'time_viewed IS NOT NULL', :order => 'created_at', :limit => 1).first.created_at 401 + # dynamically create tasks for each global task
  402 + @global_tasks.each do |taskname, description|
  403 + desc description
  404 + task taskname => :environment do
  405 + # call task
  406 + puts send(taskname).inspect
  407 + end
  408 + end
  409 +
  410 + def verify_appearance_vote_prompt_ids
  411 + bad_records = Vote.connection.select_all "
  412 + SELECT votes.id
  413 + FROM votes LEFT JOIN appearances
  414 + ON (votes.id = appearances.answerable_id
  415 + AND appearances.answerable_type = 'Vote')
  416 + WHERE votes.prompt_id <> appearances.prompt_id"
  417 + success_message = "Appearance and vote prompt_ids match"
  418 + error_message = bad_records.map do |record|
  419 + "Vote ##{record["id"]} has a different prompt_id than its appearance."
  420 + end
  421 + error_message = error_message.join "\n"
  422 + return error_message.blank? ? [success_message, false] : [error_message, true]
  423 + end
  424 +
  425 + desc "Ensure that all choices have 0 <= score <= 100"
  426 + task :verify_range_of_choices_scores => :environment do
  427 + puts verify_range_of_choices_scores().inspect
  428 + end
  429 +
  430 + def verify_range_of_choices_scores
  431 + bad_choices_count = Choice.count(:conditions => 'score < 0 OR score > 100')
  432 + success_message = "All choices have a score within 0-100"
  433 + if bad_choices_count > 0
  434 + error_message = "Some choices have a score less than 0 or greater than 100"
  435 + end
  436 + return error_message.blank? ? [success_message, false] : [error_message, true]
  437 + end
  438 + def response_time_tests
  439 + error_message = ""
  440 + success_message = "All Vote objects have an client response time < calculated server roundtrip time\n"
445 441
446 - Vote.find_each(:batch_size => 1000, :include => :appearance) do |v| 442 + recording_client_time_start_date = Vote.find(:all, :conditions => 'time_viewed IS NOT NULL', :order => 'created_at', :limit => 1).first.created_at
447 443
448 - next if v.nil? || v.appearance.nil?  
449 - # Subtracting DateTime objects results in the difference in days  
450 - server_response_time = v.created_at.to_f - v.appearance.created_at.to_f  
451 - if server_response_time < 0  
452 - the_error_msg = "Error! Vote #{v.id} was created before the appearance associated with it: Appearance id: #{v.appearance.id}, Vote creation time: #{v.created_at.to_s}, Appearance creation time: #{v.appearance.created_at.to_s}\n" 444 + Vote.find_each(:batch_size => 1000, :include => :appearance) do |v|
453 445
454 - error_message += the_error_msg  
455 - print "Error!" + the_error_msg  
456 - end 446 + next if v.nil? || v.appearance.nil?
  447 + # Subtracting DateTime objects results in the difference in days
  448 + server_response_time = v.created_at.to_f - v.appearance.created_at.to_f
  449 + if server_response_time < 0
  450 + the_error_msg = "Error! Vote #{v.id} was created before the appearance associated with it: Appearance id: #{v.appearance.id}, Vote creation time: #{v.created_at.to_s}, Appearance creation time: #{v.appearance.created_at.to_s}\n"
457 451
458 - if v.time_viewed && v.time_viewed/1000 > server_response_time  
459 - the_error_msg = "Warning! Vote #{v.id} with Appearance #{v.appearance.id}, has a longer client response time than is possible. Server roundtrip time is: #{v.created_at.to_f - v.appearance.created_at.to_f} seconds, but client side response time is: #{v.time_viewed.to_f / 1000.0} seconds\n" 452 + error_message += the_error_msg
  453 + print "Error!" + the_error_msg
  454 + end
460 455
461 - error_message += the_error_msg  
462 - print the_error_msg 456 + if v.time_viewed && v.time_viewed/1000 > server_response_time
  457 + the_error_msg = "Warning! Vote #{v.id} with Appearance #{v.appearance.id}, has a longer client response time than is possible. Server roundtrip time is: #{v.created_at.to_f - v.appearance.created_at.to_f} seconds, but client side response time is: #{v.time_viewed.to_f / 1000.0} seconds\n"
463 458
464 - elsif v.time_viewed.nil?  
465 - if v.created_at > recording_client_time_start_date && v.missing_response_time_exp != 'invalid'  
466 - the_error_msg = "Error! Vote #{v.id} with Appearance #{v.appearance.id}, does not have a client response, even though it should! Vote creation time: #{v.created_at.to_s}, Appearance creation time: #{v.appearance.created_at.to_s}, Client side response time: #{v.time_viewed}\n"  
467 error_message += the_error_msg 459 error_message += the_error_msg
468 print the_error_msg 460 print the_error_msg
  461 +
  462 + elsif v.time_viewed.nil?
  463 + if v.created_at > recording_client_time_start_date && v.missing_response_time_exp != 'invalid'
  464 + the_error_msg = "Error! Vote #{v.id} with Appearance #{v.appearance.id}, does not have a client response, even though it should! Vote creation time: #{v.created_at.to_s}, Appearance creation time: #{v.appearance.created_at.to_s}, Client side response time: #{v.time_viewed}\n"
  465 + error_message += the_error_msg
  466 + print the_error_msg
  467 + end
  468 +
469 end 469 end
470 470
471 end 471 end
472 472
  473 + return error_message.blank? ? [success_message, false] : [error_message, true]
473 end 474 end
474 -  
475 - return error_message.blank? ? [success_message, false] : [error_message, true]  
476 end 475 end
  476 + # END OF GLOBAL NAMESPACE
  477 +
477 end 478 end
478 479
479 def cleanup_args(args) 480 def cleanup_args(args)