summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/analytics/cycle_analytics/sorting_spec.rb
blob: 8f5be709a1113462f7b831e50e74fe008ada511d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Analytics::CycleAnalytics::Sorting do
  let(:stage) { build(:cycle_analytics_project_stage, start_event_identifier: :merge_request_created, end_event_identifier: :merge_request_merged) }

  subject(:order_values) { described_class.apply(MergeRequest.joins(:metrics), stage, sort, direction).order_values }

  context 'when invalid sorting params are given' do
    let(:sort) { :unknown_sort }
    let(:direction) { :unknown_direction }

    it 'falls back to end_event DESC sorting' do
      expect(order_values).to eq([stage.end_event.timestamp_projection.desc])
    end
  end

  context 'sorting end_event' do
    let(:sort) { :end_event }

    context 'direction desc' do
      let(:direction) { :desc }

      specify do
        expect(order_values).to eq([stage.end_event.timestamp_projection.desc])
      end
    end

    context 'direction asc' do
      let(:direction) { :asc }

      specify do
        expect(order_values).to eq([stage.end_event.timestamp_projection.asc])
      end
    end
  end

  context 'sorting duration' do
    let(:sort) { :duration }

    context 'direction desc' do
      let(:direction) { :desc }

      specify do
        expect(order_values).to eq([Arel::Nodes::Subtraction.new(stage.end_event.timestamp_projection, stage.start_event.timestamp_projection).desc])
      end
    end

    context 'direction asc' do
      let(:direction) { :asc }

      specify do
        expect(order_values).to eq([Arel::Nodes::Subtraction.new(stage.end_event.timestamp_projection, stage.start_event.timestamp_projection).asc])
      end
    end
  end
end