mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Replace custom query counter with new gem rspec-sql
This commit is contained in:
@@ -889,7 +889,6 @@ Style/PreferredHashMethods:
|
||||
Style/RedundantArgument:
|
||||
Exclude:
|
||||
- 'engines/dfc_provider/app/services/authorization_control.rb'
|
||||
- 'spec/support/query_counter.rb'
|
||||
|
||||
# Offense count: 1
|
||||
# This cop supports safe autocorrection (--autocorrect).
|
||||
|
||||
@@ -132,9 +132,9 @@ module Admin
|
||||
end
|
||||
|
||||
it do
|
||||
query_counter = QueryCounter.new
|
||||
get :show, params: { id: order_cycle.id }, as: :json
|
||||
expect(query_counter.queries).to eq(
|
||||
expect {
|
||||
get :show, params: { id: order_cycle.id }, as: :json
|
||||
}.to query_database(
|
||||
{
|
||||
select: {
|
||||
enterprise_fees: 3,
|
||||
@@ -151,7 +151,6 @@ module Admin
|
||||
update: { spree_users: 1 }
|
||||
}
|
||||
)
|
||||
query_counter.stop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class QueryCounter
|
||||
QUERY_TYPES = [:delete, :insert, :select, :update].freeze
|
||||
|
||||
attr_reader :queries
|
||||
|
||||
def initialize
|
||||
@queries = {}
|
||||
@subscriber = ActiveSupport::Notifications.
|
||||
subscribe("sql.active_record") do |_name, _started, _finished, _unique_id, payload|
|
||||
type = get_type(payload[:sql])
|
||||
next if QUERY_TYPES.exclude?(type) || pg_query?(payload[:sql])
|
||||
|
||||
table = get_table(payload[:sql])
|
||||
@queries[type] ||= {}
|
||||
@queries[type][table] ||= 0
|
||||
@queries[type][table] += 1
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
ActiveSupport::Notifications.unsubscribe("sql.active_record")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_table(sql)
|
||||
sql_parts = sql.split(" ")
|
||||
case get_type(sql)
|
||||
when :insert
|
||||
sql_parts[3]
|
||||
when :update
|
||||
sql_parts[1]
|
||||
else
|
||||
table_index = sql_parts.index("FROM")
|
||||
sql_parts[table_index + 1]
|
||||
end.gsub(/(\\|")/, "").to_sym
|
||||
end
|
||||
|
||||
def get_type(sql)
|
||||
sql.split(" ")[0].downcase.to_sym
|
||||
end
|
||||
|
||||
def pg_query?(sql)
|
||||
sql.include?("SELECT a.attname") || sql.include?("pg_attribute")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user