Commit 8596ca29434aeb652776a58f65217783fd540e80

Authored by Rodrigo Souto
1 parent 73f72960

Fix tests with delayed_job differences

Testing the difference of the exact job instead of all jobs.
Also including class methods on Delayed::Job to fetch jobs by_handler
and with handler_like (using sql LIKE).
app/models/person_notifier.rb
@@ -36,7 +36,7 @@ class PersonNotifier @@ -36,7 +36,7 @@ class PersonNotifier
36 36
37 class NotifyAllJob 37 class NotifyAllJob
38 def self.exists? 38 def self.exists?
39 - Delayed::Job.where(:handler => "--- !ruby/object:PersonNotifier::NotifyAllJob {}\n").count > 0 39 + Delayed::Job.by_handler("--- !ruby/object:PersonNotifier::NotifyAllJob {}\n").count > 0
40 end 40 end
41 41
42 def perform 42 def perform
@@ -51,7 +51,7 @@ class PersonNotifier @@ -51,7 +51,7 @@ class PersonNotifier
51 end 51 end
52 52
53 def self.find(person_id) 53 def self.find(person_id)
54 - Delayed::Job.where(:handler => "--- !ruby/struct:PersonNotifier::NotifyJob\nperson_id: #{person_id}\n") 54 + Delayed::Job.by_handler("--- !ruby/struct:PersonNotifier::NotifyJob\nperson_id: #{person_id}\n")
55 end 55 end
56 56
57 def perform 57 def perform
config/initializers/delayed_job_config.rb
@@ -2,6 +2,16 @@ require 'delayed_job' @@ -2,6 +2,16 @@ require 'delayed_job'
2 Delayed::Worker.backend = :active_record 2 Delayed::Worker.backend = :active_record
3 Delayed::Worker.max_attempts = 2 3 Delayed::Worker.max_attempts = 2
4 4
  5 +class Delayed::Job
  6 + def self.handler_like(handler)
  7 + Delayed::Job.where("handler LIKE '%#{handler}%'")
  8 + end
  9 +
  10 + def self.by_handler(handler)
  11 + Delayed::Job.where(:handler => handler)
  12 + end
  13 +end
  14 +
5 # TODO This is consuming ton of space on development with a postgres connection 15 # TODO This is consuming ton of space on development with a postgres connection
6 # error on the jobs. This must be verified before going into production. 16 # error on the jobs. This must be verified before going into production.
7 # Logging jobs backtraces 17 # Logging jobs backtraces
test/functional/invite_controller_test.rb
@@ -231,7 +231,7 @@ class InviteControllerTest < ActionController::TestCase @@ -231,7 +231,7 @@ class InviteControllerTest < ActionController::TestCase
231 231
232 contact_list = ContactList.create 232 contact_list = ContactList.create
233 post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id 233 post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
234 - job = Delayed::Job.where("handler LIKE '%InvitationJob%'").first 234 + job = Delayed::Job.handler_like(InvitationJob.name).first
235 assert_equal 'pt', job.payload_object.locale 235 assert_equal 'pt', job.payload_object.locale
236 end 236 end
237 237
test/unit/person_notifier_test.rb
@@ -34,14 +34,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -34,14 +34,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
34 should 'do not send mail if do not have notifications' do 34 should 'do not send mail if do not have notifications' do
35 @community.add_member(@member) 35 @community.add_member(@member)
36 ActionTracker::Record.delete_all 36 ActionTracker::Record.delete_all
37 - assert_no_difference ActionMailer::Base.deliveries, :count do 37 + assert_no_difference 'ActionMailer::Base.deliveries.count' do
38 notify 38 notify
39 end 39 end
40 end 40 end
41 41
42 should 'do not send mail to people not joined to community' do 42 should 'do not send mail to people not joined to community' do
43 Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article) 43 Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article)
44 - assert_no_difference ActionMailer::Base.deliveries, :count do 44 + assert_no_difference 'ActionMailer::Base.deliveries.count' do
45 notify 45 notify
46 end 46 end
47 end 47 end
@@ -60,7 +60,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -60,7 +60,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
60 ActionTracker::Record.delete_all 60 ActionTracker::Record.delete_all
61 comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article ) 61 comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article )
62 @member.last_notification = DateTime.now + 1.day 62 @member.last_notification = DateTime.now + 1.day
63 - assert_no_difference ActionMailer::Base.deliveries, :count do 63 + assert_no_difference 'ActionMailer::Base.deliveries.count' do
64 notify 64 notify
65 end 65 end
66 end 66 end
@@ -85,7 +85,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -85,7 +85,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
85 @member.notification_time = 12 85 @member.notification_time = 12
86 time = Time.now 86 time = Time.now
87 @member.notifier.schedule_next_notification_mail 87 @member.notifier.schedule_next_notification_mail
88 - job = Delayed::Job.where("handler like '%PersonNotifier::NotifyJob%'").last 88 + job = Delayed::Job.handler_like(PersonNotifier::NotifyJob.name).last
89 assert job.run_at >= time + @member.notification_time.hours 89 assert job.run_at >= time + @member.notification_time.hours
90 assert job.run_at < time + (@member.notification_time+1).hours 90 assert job.run_at < time + (@member.notification_time+1).hours
91 end 91 end
@@ -93,14 +93,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -93,14 +93,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
93 should 'do not schedule duplicated notification mail' do 93 should 'do not schedule duplicated notification mail' do
94 @member.notification_time = 12 94 @member.notification_time = 12
95 @member.notifier.schedule_next_notification_mail 95 @member.notifier.schedule_next_notification_mail
96 - assert_no_difference 'Delayed::Job.count' do 96 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
97 @member.notifier.schedule_next_notification_mail 97 @member.notifier.schedule_next_notification_mail
98 end 98 end
99 end 99 end
100 100
101 should 'do not schedule next mail if notification time is zero' do 101 should 'do not schedule next mail if notification time is zero' do
102 @member.notification_time = 0 102 @member.notification_time = 0
103 - assert_no_difference 'Delayed::Job.count' do 103 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
104 @member.notifier.schedule_next_notification_mail 104 @member.notifier.schedule_next_notification_mail
105 end 105 end
106 end 106 end
@@ -118,17 +118,17 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -118,17 +118,17 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
118 118
119 should 'do not create duplicated job' do 119 should 'do not create duplicated job' do
120 PersonNotifier.schedule_all_next_notification_mail 120 PersonNotifier.schedule_all_next_notification_mail
121 - assert_no_difference 'Delayed::Job.count' do 121 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
122 PersonNotifier.schedule_all_next_notification_mail 122 PersonNotifier.schedule_all_next_notification_mail
123 end 123 end
124 end 124 end
125 125
126 should 'schedule after update and set a valid notification time' do 126 should 'schedule after update and set a valid notification time' do
127 - assert_no_difference 'Delayed::Job.count' do 127 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
128 @member.notification_time = 0 128 @member.notification_time = 0
129 @member.save! 129 @member.save!
130 end 130 end
131 - assert_difference 'Delayed::Job.count', 1 do 131 + assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do
132 @member.notification_time = 12 132 @member.notification_time = 12
133 @member.save! 133 @member.save!
134 end 134 end
@@ -136,16 +136,16 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -136,16 +136,16 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
136 136
137 should 'reschedule with changed notification time' do 137 should 'reschedule with changed notification time' do
138 time = Time.now 138 time = Time.now
139 - assert_difference Delayed::Job, :count, 1 do 139 + assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do
140 @member.notification_time = 2 140 @member.notification_time = 2
141 @member.save! 141 @member.save!
142 end 142 end
143 - assert_no_difference Delayed::Job, :count do 143 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
144 @member.notification_time = 12 144 @member.notification_time = 12
145 @member.save! 145 @member.save!
146 end 146 end
147 @member.notifier.schedule_next_notification_mail 147 @member.notifier.schedule_next_notification_mail
148 - job = Delayed::Job.where("handler like '%PersonNotifier::NotifyJob%'").last 148 + job = Delayed::Job.handler_like(PersonNotifier::NotifyJob.name).last
149 assert job.run_at >= time + @member.notification_time.hours 149 assert job.run_at >= time + @member.notification_time.hours
150 assert job.run_at < time + (@member.notification_time+1).hours 150 assert job.run_at < time + (@member.notification_time+1).hours
151 end 151 end
@@ -197,7 +197,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -197,7 +197,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
197 end 197 end
198 198
199 should 'perform create NotifyJob for all users with notification_time' do 199 should 'perform create NotifyJob for all users with notification_time' do
200 - assert_difference Delayed::Job, :count, 2 do 200 + assert_difference 'job_count(PersonNotifier::NotifyJob)', 2 do
201 Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new) 201 Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new)
202 process_delayed_job_queue 202 process_delayed_job_queue
203 end 203 end
@@ -225,8 +225,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase @@ -225,8 +225,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
225 assert !jobs.select {|j| !j.failed? && j.last_error.nil? }.empty? 225 assert !jobs.select {|j| !j.failed? && j.last_error.nil? }.empty?
226 end 226 end
227 227
  228 + private
  229 +
228 def notify 230 def notify
229 @member.notifier.notify 231 @member.notifier.notify
230 end 232 end
231 233
  234 + def job_count(job)
  235 + Delayed::Job.handler_like(job.name).count
  236 + end
  237 +
232 end 238 end