summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb
blob: a975af1cda2cc008acd1131d223ee0ea38e0da4f (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do
  subject { described_class.new }
  let(:query_string) { "abc" }
  let(:provided_variables) { { a: 1, b: 2, c: 3 } }
  let!(:now) { Gitlab::Metrics::System.monotonic_time }
  let(:complexity) { 4 }
  let(:depth) { 2 }
  let(:initial_values) do
    { time_started: now,
      query_string: query_string,
      variables: provided_variables,
      complexity: nil,
      depth: nil,
      duration: nil }
  end
  before do
    allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(now)
  end

  describe '#analyze?' do
    context 'feature flag disabled' do
      before do
        stub_feature_flags(graphql_logging: false)
      end

      specify do
        expect(subject.analyze?(anything)).to be_falsey
      end
    end

    context 'feature flag enabled by default' do
      specify do
        expect(subject.analyze?(anything)).to be_truthy
      end
    end
  end

  describe '#initial_value' do
    it 'assembles a hash with initial values' do
      query = OpenStruct.new(query_string: query_string, provided_variables: provided_variables)

      expect(subject.initial_value(query)).to eq initial_values
    end
  end

  describe '#call' do
    before do
      # some statements to fudge the complexity and depth
    end

    it 'sets the complexity and depth' do
      expected_hash = { time_started: now,
                        query_string: query_string,
                        variables: provided_variables,
                        complexity: nil,
                        depth: depth,
                        duration: complexity }

      expect(subject.call(initial_values, nil, nil)).to eq expected_hash
    end
  end
end