Commit e25c11f0 authored by Nick Thomas's avatar Nick Thomas

Merge branch 'tancnle/remove-marginalia-traces-from-query-matcher' into 'master'

Remove marginalia traces in query matcher

See merge request gitlab-org/gitlab!26988
parents 92fbe822 3e1456b8
# frozen_string_literal: true
module ExceedQueryLimitHelpers
MARGINALIA_ANNOTATION_REGEX = %r{\s*\/\*.*\*\/}.freeze
def with_threshold(threshold)
@threshold = threshold
self
......@@ -41,8 +43,8 @@ module ExceedQueryLimitHelpers
def log_message
if expected.is_a?(ActiveRecord::QueryRecorder)
counts = count_queries(expected.log)
extra_queries = @recorder.log.reject { |query| counts[query] -= 1 unless counts[query].zero? }
counts = count_queries(strip_marginalia_annotations(expected.log))
extra_queries = strip_marginalia_annotations(@recorder.log).reject { |query| counts[query] -= 1 unless counts[query].zero? }
extra_queries_display = count_queries(extra_queries).map { |query, count| "[#{count}] #{query}" }
(['Extra queries:'] + extra_queries_display).join("\n\n")
......@@ -65,6 +67,10 @@ module ExceedQueryLimitHelpers
counts = "#{expected_count}#{threshold_message}"
"Expected a maximum of #{counts} queries, got #{actual_count}:\n\n#{log_message}"
end
def strip_marginalia_annotations(logs)
logs.map { |log| log.sub(MARGINALIA_ANNOTATION_REGEX, '') }
end
end
RSpec::Matchers.define :exceed_all_query_limit do |expected|
......
# frozen_string_literal: true
require 'spec_helper'
describe ExceedQueryLimitHelpers do
class TestQueries < ActiveRecord::Base
self.table_name = 'schema_migrations'
end
class TestMatcher
include ExceedQueryLimitHelpers
def expected
ActiveRecord::QueryRecorder.new do
2.times { TestQueries.count }
end
end
end
it 'does not contain marginalia annotations' do
test_matcher = TestMatcher.new
test_matcher.verify_count do
2.times { TestQueries.count }
TestQueries.first
end
aggregate_failures do
expect(test_matcher.log_message)
.to match(%r{ORDER BY.*#{TestQueries.table_name}.*LIMIT 1})
expect(test_matcher.log_message)
.not_to match(%r{\/\*.*correlation_id.*\*\/})
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment