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

require 'spec_helper'

RSpec.describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do
  let(:initial_value) { analyzer.initial_value(query) }
  let(:analyzer) { described_class.new }
  let(:query) { GraphQL::Query.new(GitlabSchema, document: document, context: {}, variables: { body: "some note" }) }
  let(:document) do
    GraphQL.parse <<-GRAPHQL
      mutation createNote($body: String!) {
        createNote(input: {noteableId: "1", body: $body}) {
          note {
            id
          }
        }
      }
    GRAPHQL
  end

  describe '#final_value' do
    let(:monotonic_time_before) { 42 }
    let(:monotonic_time_after) { 500 }
    let(:monotonic_time_duration) { monotonic_time_after - monotonic_time_before }
    let(:memo) { initial_value }

    subject(:final_value) { analyzer.final_value(memo) }

    before do
      RequestStore.store[:graphql_logs] = nil

      allow(GraphQL::Analysis).to receive(:analyze_query).and_return([4, 2, [[], []]])
      allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(monotonic_time_before, monotonic_time_after)
      allow(Gitlab::GraphqlLogger).to receive(:info)
    end

    it 'inserts duration in seconds to memo and sets request store' do
      expect { final_value }.to change { memo[:duration_s] }.to(monotonic_time_duration)
                            .and change { RequestStore.store[:graphql_logs] }.to([{
                              complexity: 4,
                              depth: 2,
                              operation_name: query.operation_name,
                              used_deprecated_fields: [],
                              used_fields: [],
                              variables: { body: "[FILTERED]" }.to_s
                            }])
    end
  end
end