Fix Issue 2541 migration spec (prevent leaky test setup)

This commit is contained in:
Ryan Lue 2019-04-10 17:03:39 +08:00 committed by Thorsten Eckel
parent 00934ef4f4
commit cdf4eaf92a

View file

@ -1,61 +1,57 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Issue2541FixNotificationEmailWithoutBody, type: :db_migration do RSpec.describe Issue2541FixNotificationEmailWithoutBody, type: :db_migration do
describe '"body" attribute management' do
# We use #update_columns to bypass callbacks
# that would prevent the record from being saved with an empty body
before { subject.update_columns(perform: perform) }
context 'when migrating Triggers' do let(:perform) do
{
before(:all) { Trigger.skip_callback(:create, :before, :validate_perform) } type => {
it "updates empty perform['notification.email']['body'] attribute" do
trigger = create(:trigger,
perform: {
'notification.email' => {
'body' => '', 'body' => '',
'recipient' => 'article_last_sender', 'recipient' => 'article_last_sender',
'subject' => 'Thanks for your inquiry (#{ticket.title})', # rubocop:disable Lint/InterpolationCheck 'subject' => 'Thanks for your inquiry (#{ticket.title})', # rubocop:disable Lint/InterpolationCheck
}, },
}) }
expect { migrate }.to change { trigger.reload.perform['notification.email']['body'] }.from('').to('-')
end end
it "updates empty perform['notification.sms']['body'] attribute" do context 'when migrating Triggers' do
trigger = create(:trigger, subject(:trigger) { create(:trigger) }
perform: {
'notification.sms' => {
'body' => '',
'recipient' => 'article_last_sender',
},
})
context 'for email' do
let(:type) { 'notification.email' }
it "updates empty perform['notification.email']['body'] attribute" do
expect { migrate }.to change { trigger.reload.perform['notification.email']['body'] }.from('').to('-')
end
end
context 'for SMS' do
let(:type) { 'notification.sms' }
it "updates empty perform['notification.sms']['body'] attribute" do
expect { migrate }.to change { trigger.reload.perform['notification.sms']['body'] }.from('').to('-') expect { migrate }.to change { trigger.reload.perform['notification.sms']['body'] }.from('').to('-')
end end
end end
end
context 'when migrating Jobs' do context 'when migrating Jobs' do
subject(:job) { create(:job) }
before(:all) { Job.skip_callback(:create, :before, :validate_perform) } let(:type) { 'notification.email' }
it "updates empty perform['notification.email']['body'] attribute" do it "updates empty perform['notification.email']['body'] attribute" do
job = create(:job,
perform: {
'notification.email' => {
'body' => '',
'recipient' => 'article_last_sender',
'subject' => 'Thanks for your inquiry (#{ticket.title})', # rubocop:disable Lint/InterpolationCheck
},
},)
expect { migrate }.to change { job.reload.perform['notification.email']['body'] }.from('').to('-') expect { migrate }.to change { job.reload.perform['notification.email']['body'] }.from('').to('-')
end end
end end
end
describe 'scheduler management' do
let(:scheduler) { Scheduler.find_by(method: 'Job.run') }
before { scheduler.update!(active: false) }
it "re-enables 'Job.run' Scheduler" do it "re-enables 'Job.run' Scheduler" do
scheduler = Scheduler.find_by(method: 'Job.run')
scheduler.update!(active: false)
expect { migrate }.to change { scheduler.reload.active }.to(true) expect { migrate }.to change { scheduler.reload.active }.to(true)
end end
end
end end