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

query_statistics_spec.rb « observers « migrations « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9727a215d711711891bcd4b95a2deb9826fb4b73 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::Database::Migrations::Observers::QueryStatistics do
  subject { described_class.new(observation, double("unused path")) }

  let(:observation) { Gitlab::Database::Migrations::Observation.new }
  let(:connection) { ActiveRecord::Base.connection }

  def mock_pgss(enabled: true)
    if enabled
      allow(subject).to receive(:function_exists?).with(:pg_stat_statements_reset).and_return(true)
      allow(connection).to receive(:view_exists?).with(:pg_stat_statements).and_return(true)
    else
      allow(subject).to receive(:function_exists?).with(:pg_stat_statements_reset).and_return(false)
      allow(connection).to receive(:view_exists?).with(:pg_stat_statements).and_return(false)
    end
  end

  describe '#before' do
    context 'with pgss available' do
      it 'resets pg_stat_statements' do
        mock_pgss(enabled: true)
        expect(connection).to receive(:execute).with('select pg_stat_statements_reset()').once

        subject.before
      end
    end

    context 'without pgss available' do
      it 'executes nothing' do
        mock_pgss(enabled: false)
        expect(connection).not_to receive(:execute)

        subject.before
      end
    end
  end

  describe '#record' do
    let(:result) { double }
    let(:pgss_query) do
      <<~SQL
        SELECT query, calls, total_time, max_time, mean_time, rows
        FROM pg_stat_statements
        ORDER BY total_time DESC
      SQL
    end

    context 'with pgss available' do
      it 'fetches data from pg_stat_statements and stores on the observation' do
        mock_pgss(enabled: true)
        expect(connection).to receive(:execute).with(pgss_query).once.and_return(result)

        expect { subject.record }.to change { observation.query_statistics }.from(nil).to(result)
      end
    end

    context 'without pgss available' do
      it 'executes nothing' do
        mock_pgss(enabled: false)
        expect(connection).not_to receive(:execute)

        expect { subject.record }.not_to change { observation.query_statistics }
      end
    end
  end
end