Welcome to mirror list, hosted at ThFree Co, Russian Federation.

query_recorder_spec.rb « query_analyzers « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec01ae623ae92a61c08dd9d4f347ba36c34746cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: false do
  # We keep only the QueryRecorder analyzer running
  around do |example|
    described_class.with_suppressed(false) do
      example.run
    end
  end

  context 'when analyzer is enabled for tests' do
    let(:query) { 'SELECT 1 FROM projects' }
    let(:log_path) { Rails.root.join(described_class::LOG_FILE) }

    before do
      stub_env('CI', 'true')

      # This is needed to be able to stub_env the CI variable
      ::Gitlab::Database::QueryAnalyzer.instance.begin!([described_class])
    end

    after do
      ::Gitlab::Database::QueryAnalyzer.instance.end!([described_class])
    end

    it 'logs queries to a file' do
      allow(FileUtils).to receive(:mkdir_p)
        .with(File.dirname(log_path))
      expect(File).to receive(:write)
        .with(log_path, /^{"sql":"#{query}/, mode: 'a')
      expect(described_class).to receive(:analyze).with(/^#{query}/).and_call_original

      expect { ApplicationRecord.connection.execute(query) }.not_to raise_error
    end
  end
end