summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/cycle_analytics/base_stage_shared_examples.rb
blob: d76089d56ddec35b07e10364534a36189f9b17a9 (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
69
70
71
72
73
74
# frozen_string_literal: true

require 'spec_helper'

ISSUES_MEDIAN = 30.minutes.to_i

RSpec.shared_examples 'base stage' do
  let(:stage) { described_class.new(options: { project: double }) }

  before do
    allow(stage).to receive(:project_median).and_return(1.12)
    allow_next_instance_of(Gitlab::CycleAnalytics::BaseEventFetcher) do |instance|
      allow(instance).to receive(:event_result).and_return({})
    end
  end

  it 'has the median data value' do
    expect(stage.as_json[:value]).not_to be_nil
  end

  it 'has the median data stage' do
    expect(stage.as_json[:title]).not_to be_nil
  end

  it 'has the median data description' do
    expect(stage.as_json[:description]).not_to be_nil
  end

  it 'has the title' do
    expect(stage.title).to eq(stage_name.to_s.capitalize)
  end

  it 'has the events' do
    expect(stage.events).not_to be_nil
  end
end

RSpec.shared_examples 'calculate #median with date range' do
  context 'when valid date range is given' do
    before do
      stage_options[:from] = 5.days.ago
      stage_options[:to] = 5.days.from_now
    end

    it { expect(stage.project_median).to eq(ISSUES_MEDIAN) }
  end

  context 'when records are out of the date range' do
    before do
      stage_options[:from] = 2.years.ago
      stage_options[:to] = 1.year.ago
    end

    it { expect(stage.project_median).to eq(nil) }
  end
end

RSpec.shared_examples 'Gitlab::Analytics::CycleAnalytics::DataCollector backend examples' do
  let(:stage_params) { Gitlab::Analytics::CycleAnalytics::DefaultStages.send("params_for_#{stage_name}_stage").merge(project: project) }
  let(:stage) { Analytics::CycleAnalytics::ProjectStage.new(stage_params) }
  let(:data_collector) { Gitlab::Analytics::CycleAnalytics::DataCollector.new(stage: stage, params: { from: stage_options[:from], current_user: project.creator }) }
  let(:attribute_to_verify) { :title }

  context 'provides the same results as the old implementation' do
    it 'for the median' do
      expect(data_collector.median.seconds).to be_within(0.5).of(ISSUES_MEDIAN)
    end

    it 'for the list of event records' do
      records = data_collector.records_fetcher.serialized_records
      expect(records.map { |event| event[attribute_to_verify] }).to eq(expected_ordered_attribute_values)
    end
  end
end