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 36  
37 37 class NotifyAllJob
38 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 40 end
41 41  
42 42 def perform
... ... @@ -51,7 +51,7 @@ class PersonNotifier
51 51 end
52 52  
53 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 55 end
56 56  
57 57 def perform
... ...
config/initializers/delayed_job_config.rb
... ... @@ -2,6 +2,16 @@ require 'delayed_job'
2 2 Delayed::Worker.backend = :active_record
3 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 15 # TODO This is consuming ton of space on development with a postgres connection
6 16 # error on the jobs. This must be verified before going into production.
7 17 # Logging jobs backtraces
... ...
test/functional/invite_controller_test.rb
... ... @@ -231,7 +231,7 @@ class InviteControllerTest < ActionController::TestCase
231 231  
232 232 contact_list = ContactList.create
233 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 235 assert_equal 'pt', job.payload_object.locale
236 236 end
237 237  
... ...
test/unit/person_notifier_test.rb
... ... @@ -34,14 +34,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
34 34 should 'do not send mail if do not have notifications' do
35 35 @community.add_member(@member)
36 36 ActionTracker::Record.delete_all
37   - assert_no_difference ActionMailer::Base.deliveries, :count do
  37 + assert_no_difference 'ActionMailer::Base.deliveries.count' do
38 38 notify
39 39 end
40 40 end
41 41  
42 42 should 'do not send mail to people not joined to community' do
43 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 45 notify
46 46 end
47 47 end
... ... @@ -60,7 +60,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
60 60 ActionTracker::Record.delete_all
61 61 comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article )
62 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 64 notify
65 65 end
66 66 end
... ... @@ -85,7 +85,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
85 85 @member.notification_time = 12
86 86 time = Time.now
87 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 89 assert job.run_at >= time + @member.notification_time.hours
90 90 assert job.run_at < time + (@member.notification_time+1).hours
91 91 end
... ... @@ -93,14 +93,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
93 93 should 'do not schedule duplicated notification mail' do
94 94 @member.notification_time = 12
95 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 97 @member.notifier.schedule_next_notification_mail
98 98 end
99 99 end
100 100  
101 101 should 'do not schedule next mail if notification time is zero' do
102 102 @member.notification_time = 0
103   - assert_no_difference 'Delayed::Job.count' do
  103 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
104 104 @member.notifier.schedule_next_notification_mail
105 105 end
106 106 end
... ... @@ -118,17 +118,17 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
118 118  
119 119 should 'do not create duplicated job' do
120 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 122 PersonNotifier.schedule_all_next_notification_mail
123 123 end
124 124 end
125 125  
126 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 128 @member.notification_time = 0
129 129 @member.save!
130 130 end
131   - assert_difference 'Delayed::Job.count', 1 do
  131 + assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do
132 132 @member.notification_time = 12
133 133 @member.save!
134 134 end
... ... @@ -136,16 +136,16 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
136 136  
137 137 should 'reschedule with changed notification time' do
138 138 time = Time.now
139   - assert_difference Delayed::Job, :count, 1 do
  139 + assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do
140 140 @member.notification_time = 2
141 141 @member.save!
142 142 end
143   - assert_no_difference Delayed::Job, :count do
  143 + assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
144 144 @member.notification_time = 12
145 145 @member.save!
146 146 end
147 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 149 assert job.run_at >= time + @member.notification_time.hours
150 150 assert job.run_at < time + (@member.notification_time+1).hours
151 151 end
... ... @@ -197,7 +197,7 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
197 197 end
198 198  
199 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 201 Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new)
202 202 process_delayed_job_queue
203 203 end
... ... @@ -225,8 +225,14 @@ class PersonNotifierTest &lt; ActiveSupport::TestCase
225 225 assert !jobs.select {|j| !j.failed? && j.last_error.nil? }.empty?
226 226 end
227 227  
  228 + private
  229 +
228 230 def notify
229 231 @member.notifier.notify
230 232 end
231 233  
  234 + def job_count(job)
  235 + Delayed::Job.handler_like(job.name).count
  236 + end
  237 +
232 238 end
... ...