summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/analytics/cycle_analytics/aggregated/base_query_builder_spec.rb
blob: bf2f8d8159ba11e0b64cc5cb9bc43a837e937bbf (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Analytics::CycleAnalytics::Aggregated::BaseQueryBuilder do
  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:milestone) { create(:milestone, project: project) }
  let_it_be(:user_1) { create(:user) }

  let_it_be(:label_1) { create(:label, project: project) }
  let_it_be(:label_2) { create(:label, project: project) }

  let_it_be(:issue_1) { create(:issue, project: project, author: project.creator, labels: [label_1, label_2]) }
  let_it_be(:issue_2) { create(:issue, project: project, milestone: milestone, assignees: [user_1]) }
  let_it_be(:issue_3) { create(:issue, project: project) }
  let_it_be(:issue_outside_project) { create(:issue) }

  let_it_be(:stage) do
    create(:cycle_analytics_project_stage,
           project: project,
           start_event_identifier: :issue_created,
           end_event_identifier: :issue_deployed_to_production
          )
  end

  let_it_be(:stage_event_1) do
    create(:cycle_analytics_issue_stage_event,
           stage_event_hash_id: stage.stage_event_hash_id,
           group_id: group.id,
           project_id: project.id,
           issue_id: issue_1.id,
           author_id: project.creator.id,
           milestone_id: nil,
           state_id: issue_1.state_id,
           end_event_timestamp: 8.months.ago
          )
  end

  let_it_be(:stage_event_2) do
    create(:cycle_analytics_issue_stage_event,
           stage_event_hash_id: stage.stage_event_hash_id,
           group_id: group.id,
           project_id: project.id,
           issue_id: issue_2.id,
           author_id: nil,
           milestone_id: milestone.id,
           state_id: issue_2.state_id
          )
  end

  let_it_be(:stage_event_3) do
    create(:cycle_analytics_issue_stage_event,
           stage_event_hash_id: stage.stage_event_hash_id,
           group_id: group.id,
           project_id: project.id,
           issue_id: issue_3.id,
           author_id: nil,
           milestone_id: milestone.id,
           state_id: issue_3.state_id,
           start_event_timestamp: 8.months.ago,
           end_event_timestamp: nil
          )
  end

  let(:params) do
    {
      from: 1.year.ago.to_date,
      to: Date.today
    }
  end

  subject(:issue_ids) { described_class.new(stage: stage, params: params).build.pluck(:issue_id) }

  it 'scopes the query for the given project' do
    expect(issue_ids).to match_array([issue_1.id, issue_2.id])
    expect(issue_ids).not_to include([issue_outside_project.id])
  end

  describe 'author_username param' do
    it 'returns stage events associated with the given author' do
      params[:author_username] = project.creator.username

      expect(issue_ids).to eq([issue_1.id])
    end

    it 'returns empty result when unknown author is given' do
      params[:author_username] = 'no one'

      expect(issue_ids).to be_empty
    end
  end

  describe 'milestone_title param' do
    it 'returns stage events associated with the milestone' do
      params[:milestone_title] = milestone.title

      expect(issue_ids).to eq([issue_2.id])
    end

    it 'returns empty result when unknown milestone is given' do
      params[:milestone_title] = 'unknown milestone'

      expect(issue_ids).to be_empty
    end
  end

  describe 'label_name param' do
    it 'returns stage events associated with multiple labels' do
      params[:label_name] = [label_1.name, label_2.name]

      expect(issue_ids).to eq([issue_1.id])
    end

    it 'does not include records with partial label match' do
      params[:label_name] = [label_1.name, 'other label']

      expect(issue_ids).to be_empty
    end
  end

  describe 'assignee_username param' do
    it 'returns stage events associated assignee' do
      params[:assignee_username] = [user_1.username]

      expect(issue_ids).to eq([issue_2.id])
    end
  end

  describe 'timestamp filtering' do
    before do
      params[:from] = 1.year.ago
      params[:to] = 6.months.ago
    end

    it 'filters by the end event time range' do
      expect(issue_ids).to eq([issue_1.id])
    end

    context 'when in_progress items are requested' do
      before do
        params[:end_event_filter] = :in_progress
      end

      it 'filters by the start event time range' do
        expect(issue_ids).to eq([issue_3.id])
      end
    end
  end
end