diff options
Diffstat (limited to 'spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb')
-rw-r--r-- | spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb b/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb index bcc39c0c3db..22ff66ff55e 100644 --- a/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb +++ b/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: false do +RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, feature_category: :database, query_analyzers: false do # We keep only the QueryRecorder analyzer running around do |example| described_class.with_suppressed(false) do @@ -11,7 +11,6 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: end context 'with query analyzer' do - let(:query) { 'SELECT 1 FROM projects' } let(:log_path) { Rails.root.join(described_class::LOG_PATH) } let(:log_file) { described_class.log_file } @@ -20,14 +19,44 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: end shared_examples_for 'an enabled query recorder' do - it 'logs queries to a file' do - allow(FileUtils).to receive(:mkdir_p) - .with(log_path) - expect(File).to receive(:write) - .with(log_file, /^{"sql":"#{query}/, mode: 'a') - expect(described_class).to receive(:analyze).with(/^#{query}/).and_call_original - - expect { ApplicationRecord.connection.execute(query) }.not_to raise_error + using RSpec::Parameterized::TableSyntax + + normalized_query = <<~SQL.strip.tr("\n", ' ') + SELECT \\\\"projects\\\\".\\\\"id\\\\" + FROM \\\\"projects\\\\" + WHERE \\\\"projects\\\\".\\\\"namespace_id\\\\" = \\? + AND \\\\"projects\\\\".\\\\"id\\\\" IN \\(\\?,\\?,\\?\\); + SQL + + where(:list_parameter, :bind_parameters) do + '$2, $3' | [1, 2, 3] + '$2, $3, $4' | [1, 2, 3, 4] + '$2 ,$3 ,$4 ,$5' | [1, 2, 3, 4, 5] + '$2 , $3 , $4 , $5, $6' | [1, 2, 3, 4, 5, 6] + '$2, $3 ,$4 , $5,$6,$7' | [1, 2, 3, 4, 5, 6, 7] + '$2,$3,$4,$5,$6,$7,$8' | [1, 2, 3, 4, 5, 6, 7, 8] + end + + with_them do + before do + allow(described_class).to receive(:analyze).and_call_original + allow(FileUtils).to receive(:mkdir_p) + .with(log_path) + end + + it 'logs normalized queries to a file' do + expect(File).to receive(:write) + .with(log_file, /^{"normalized":"#{normalized_query}/, mode: 'a') + + expect do + ApplicationRecord.connection.exec_query(<<~SQL.strip.tr("\n", ' '), 'SQL', bind_parameters) + SELECT "projects"."id" + FROM "projects" + WHERE "projects"."namespace_id" = $1 + AND "projects"."id" IN (#{list_parameter}); + SQL + end.not_to raise_error + end end end |