Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec for double query runtime increment #39

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add spec to ensure runtime isn't incremented twice
Adds a spec around `Lograge::ActiveRecordLogSubscriber#sql` to ensure
that when the config option `keep_default_active_record_log` is set, the
`ActiveRecord::LogSubscriber.runtime` isn't incremented. This keeps
lograge from double-counting the total duration for database calls.
  • Loading branch information
PJ Davis committed Oct 7, 2022
commit 4ad3cf7ac6a64ba5bfa598f79f1332b5adea4969
37 changes: 37 additions & 0 deletions spec/lograge/active_record_log_subscriber_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

RSpec.describe Lograge::ActiveRecordLogSubscriber do
describe '#sql' do
subject(:log_subscriber) { described_class.new }
let(:event) { instance_double('ActiveSupport::Notification::Event.new', duration: 20, payload: {}) }
let(:rails) { double('rails') }
let(:ar_log_subscriber) { double('ActiveRecord::LogSubscriber', runtime: 100) }

before do
stub_const('Rails', rails)
stub_const('ActiveRecord::LogSubscriber', ar_log_subscriber)
Lograge::Sql.extract_event = proc {}
end

context 'with keep_default_active_record_log not set' do
before do
allow(Rails).to receive_message_chain('application.config.lograge_sql.keep_default_active_record_log') { nil }
end
it 'adds duration to ActiveRecord::LogSubscriber runtime' do
expect(ar_log_subscriber).to receive(:runtime=).with(120)

log_subscriber.sql(event)
end
end
context 'with keep_default_active_record_log set to true' do
before do
allow(Rails).to receive_message_chain('application.config.lograge_sql.keep_default_active_record_log') { true }
end
it 'does not add duration to ActiveRecord::LogSubscriber runtime' do
expect(ar_log_subscriber).to_not receive(:runtime=)

log_subscriber.sql(event)
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'delegate'
require 'lograge'
require 'lograge/sql'
require 'lograge/active_record_log_subscriber'

RSpec.configure do |config| # rubocop:disable Style/SymbolProc
config.disable_monkey_patching!
Expand Down