2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-15 15:21:04 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::QueryLimiting::ActiveSupportSubscriber do
|
2021-03-08 00:09:10 +00:00
|
|
|
let(:transaction) { instance_double(Gitlab::QueryLimiting::Transaction, executed_sql: true, increment: true) }
|
2018-02-21 05:27:05 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::QueryLimiting::Transaction)
|
|
|
|
.to receive(:current)
|
|
|
|
.and_return(transaction)
|
|
|
|
end
|
|
|
|
|
2018-01-15 15:21:04 +00:00
|
|
|
describe '#sql' do
|
|
|
|
it 'increments the number of executed SQL queries' do
|
2018-02-21 05:27:05 +00:00
|
|
|
User.count
|
2018-01-15 15:21:04 +00:00
|
|
|
|
|
|
|
expect(transaction)
|
2018-02-21 05:27:05 +00:00
|
|
|
.to have_received(:increment)
|
|
|
|
.once
|
2021-03-08 00:09:10 +00:00
|
|
|
|
|
|
|
expect(transaction)
|
|
|
|
.to have_received(:executed_sql)
|
|
|
|
.once
|
|
|
|
.with(String)
|
2018-02-21 05:27:05 +00:00
|
|
|
end
|
2018-01-15 15:21:04 +00:00
|
|
|
|
2018-02-21 05:27:05 +00:00
|
|
|
context 'when the query is actually a rails cache hit' do
|
|
|
|
it 'does not increment the number of executed SQL queries' do
|
|
|
|
ActiveRecord::Base.connection.cache do
|
|
|
|
User.count
|
|
|
|
User.count
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(transaction)
|
|
|
|
.to have_received(:increment)
|
|
|
|
.once
|
2021-03-08 00:09:10 +00:00
|
|
|
|
|
|
|
expect(transaction)
|
|
|
|
.to have_received(:executed_sql)
|
|
|
|
.once
|
|
|
|
.with(String)
|
2018-02-21 05:27:05 +00:00
|
|
|
end
|
2018-01-15 15:21:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|