docbrown/spec/models/application_record_spec.rb
Jamie Gaskins 820dd9b5a6
Allow Emails::RemoveOldEmailsWorker to run longer (#15085)
* Allow Emails::RemoveOldEmailsWorker to run longer

We set the SQL statement timeout based on the STATEMENT_TIMEOUT
environment variable (defaulting to 2.5 seconds in production), but not
all SQL queries are created equal. Since some may take longer out of
necessity, this PR introduces a `Model.with_statement_timeout` method
that allows you to change the SQL statement timeout only for a given
block.

* EmailMessage doesn't inherit ApplicationRecord

It inherits from `Ahoy::Message`, which inherits directly from
`ActiveRecord::Base`.

[3] pry(main)> EmailMessage.ancestors
=> [EmailMessage(id: integer, clicked_at: datetime, content: text, feedback_message_id: integer, mailer: string, sent_at: datetime, subject: text, to: text, token: string, user_id: integer, user_type: string, utm_campaign: string, utm_content: string, utm_medium: string, utm_source: string, utm_term: string),
 EmailMessage::GeneratedAssociationMethods,
 EmailMessage::GeneratedAttributeMethods,
 Ahoy::Message(id: integer, clicked_at: datetime, content: text, feedback_message_id: integer, mailer: string, sent_at: datetime, subject: text, to: text, token: string, user_id: integer, user_type: string, utm_campaign: string, utm_content: string, utm_medium: string, utm_source: string, utm_term: string),
 Kaminari::ConfigurationMethods,
 Kaminari::ActiveRecordModelExtension,
 Ahoy::Message::GeneratedAssociationMethods,
 Ahoy::Message::GeneratedAttributeMethods,
 Bullet::SaveWithBulletSupport,
 ActiveRecord::Base,
 ...

* Use milliseconds to match STATEMENT_TIMEOUT units
2021-10-15 11:53:55 -04:00

59 lines
1.7 KiB
Ruby

require "rails_helper"
# ApplicationRecord is an abstract class, tests will use one of the core models
RSpec.describe ApplicationRecord, type: :model do
describe ".estimated_count" do
it "does not raise errors if there are no rows" do
expect { User.estimated_count }.not_to raise_error
end
end
describe "#decorate" do
it "decorates an object that has a decorator" do
sponsorship = build(:sponsorship)
expect(sponsorship.decorate).to be_a(SponsorshipDecorator)
end
it "raises an error if an object has no decorator" do
badge = build(:badge)
expect { badge.decorate }.to raise_error(UninferrableDecoratorError)
end
end
describe "#decorated?" do
it "returns false" do
sponsorship = build(:sponsorship)
expect(sponsorship.decorated?).to be(false)
end
end
describe ".decorate" do
before do
create(:sponsorship, level: :gold)
end
it "decorates a relation" do
decorated_collection = Sponsorship.gold.decorate
expect(decorated_collection.size).to eq(Sponsorship.gold.size)
expect(decorated_collection.first).to be_a(SponsorshipDecorator)
end
end
describe ".with_statement_timeout" do
it "sets the SQL statement timeout to the specified duration" do
original_timeout = described_class.statement_timeout
described_class.with_statement_timeout 1.second do
expect(described_class.statement_timeout).to eq 1.second
described_class.with_statement_timeout 10.seconds do
expect(described_class.statement_timeout).to eq 10.seconds
end
expect(described_class.statement_timeout).to eq 1.second
end
expect(described_class.statement_timeout).to eq original_timeout
end
end
end