summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/requests/api/logging_application_context_shared_examples.rb
blob: 4a71b696d57d6c098674d01f54eb961d4eebd3a2 (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
# frozen_string_literal: true

RSpec.shared_examples 'storing arguments in the application context' do
  around do |example|
    Labkit::Context.with_context { example.run }
  end

  it 'places the expected params in the application context' do
    # Stub the clearing of the context so we can validate it later
    # The `around` block above makes sure we do clean it up later
    allow(Labkit::Context).to receive(:pop)

    subject

    Labkit::Context.with_context do |context|
      expect(context.to_h)
        .to include(log_hash(expected_params))
    end
  end

  def log_hash(hash)
    hash.transform_keys! { |key| "meta.#{key}" }
  end
end

RSpec.shared_examples 'not executing any extra queries for the application context' do |expected_extra_queries = 0|
  it 'does not execute more queries than without adding anything to the application context' do
    # Call the subject once to memoize all factories being used for the spec, so they won't
    # add any queries to the expectation.
    subject_proc.call

    expect do
      allow(Gitlab::ApplicationContext).to receive(:push).and_call_original
      subject_proc.call
    end.to issue_same_number_of_queries_as {
      allow(Gitlab::ApplicationContext).to receive(:push)
      subject_proc.call
    }.with_threshold(expected_extra_queries).ignoring_cached_queries
  end
end