summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/cycle_analytics/stage_summary_spec.rb
blob: b9e0132badbfd11f45f148c5363734debb6a56e7 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::CycleAnalytics::StageSummary do
  let(:project) { create(:project, :repository) }
  let(:options) { { from: 1.day.ago } }
  let(:args) { { options: options, current_user: user } }
  let(:user) { create(:user, :admin) }

  before do
    project.add_maintainer(user)
  end

  let(:stage_summary) { described_class.new(project, **args).data }

  describe "#new_issues" do
    subject { stage_summary.first }

    context 'when from date is given' do
      before do
        Timecop.freeze(5.days.ago) { create(:issue, project: project) }
        Timecop.freeze(5.days.from_now) { create(:issue, project: project) }
      end

      it "finds the number of issues created after the 'from date'" do
        expect(subject[:value]).to eq('1')
      end

      it 'returns the localized title' do
        Gitlab::I18n.with_locale(:ru) do
          expect(subject[:title]).to eq(n_('New Issue', 'New Issues', 1))
        end
      end
    end

    it "doesn't find issues from other projects" do
      Timecop.freeze(5.days.from_now) { create(:issue, project: create(:project)) }

      expect(subject[:value]).to eq('-')
    end

    context 'when `to` parameter is given' do
      before do
        Timecop.freeze(5.days.ago) { create(:issue, project: project) }
        Timecop.freeze(5.days.from_now) { create(:issue, project: project) }
      end

      it "doesn't find any record" do
        options[:to] = Time.now

        expect(subject[:value]).to eq('-')
      end

      it "finds records created between `from` and `to` range" do
        options[:from] = 10.days.ago
        options[:to] = 10.days.from_now

        expect(subject[:value]).to eq('2')
      end
    end
  end

  describe "#commits" do
    subject { stage_summary.second }

    context 'when from date is given' do
      before do
        Timecop.freeze(5.days.ago) { create_commit("Test message", project, user, 'master') }
        Timecop.freeze(5.days.from_now) { create_commit("Test message", project, user, 'master') }
      end

      it "finds the number of commits created after the 'from date'" do
        expect(subject[:value]).to eq('1')
      end

      it 'returns the localized title' do
        Gitlab::I18n.with_locale(:ru) do
          expect(subject[:title]).to eq(n_('Commit', 'Commits', 1))
        end
      end
    end

    it "doesn't find commits from other projects" do
      Timecop.freeze(5.days.from_now) { create_commit("Test message", create(:project, :repository), user, 'master') }

      expect(subject[:value]).to eq('-')
    end

    it "finds a large (> 100) number of commits if present" do
      Timecop.freeze(5.days.from_now) { create_commit("Test message", project, user, 'master', count: 100) }

      expect(subject[:value]).to eq('100')
    end

    context 'when `to` parameter is given' do
      before do
        Timecop.freeze(5.days.ago) { create_commit("Test message", project, user, 'master') }
        Timecop.freeze(5.days.from_now) { create_commit("Test message", project, user, 'master') }
      end

      it "doesn't find any record" do
        options[:to] = Time.now

        expect(subject[:value]).to eq('-')
      end

      it "finds records created between `from` and `to` range" do
        options[:from] = 10.days.ago
        options[:to] = 10.days.from_now

        expect(subject[:value]).to eq('2')
      end
    end

    context 'when a guest user is signed in' do
      let(:guest_user) { create(:user) }

      before do
        project.add_guest(guest_user)
        args.merge!({ current_user: guest_user })
      end

      it 'does not include commit stats' do
        data = described_class.new(project, **args).data
        expect(includes_commits?(data)).to be_falsy
      end

      def includes_commits?(data)
        data.any? { |h| h["title"] == 'Commits' }
      end
    end
  end

  describe "#deploys" do
    subject { stage_summary.third }

    context 'when from date is given' do
      before do
        Timecop.freeze(5.days.ago) { create(:deployment, :success, project: project) }
        Timecop.freeze(5.days.from_now) { create(:deployment, :success, project: project) }
      end

      it "finds the number of deploys made created after the 'from date'" do
        expect(subject[:value]).to eq('1')
      end

      it 'returns the localized title' do
        Gitlab::I18n.with_locale(:ru) do
          expect(subject[:title]).to eq(n_('Deploy', 'Deploys', 1))
        end
      end
    end

    it "doesn't find commits from other projects" do
      Timecop.freeze(5.days.from_now) do
        create(:deployment, :success, project: create(:project, :repository))
      end

      expect(subject[:value]).to eq('-')
    end

    context 'when `to` parameter is given' do
      before do
        Timecop.freeze(5.days.ago) { create(:deployment, :success, project: project) }
        Timecop.freeze(5.days.from_now) { create(:deployment, :success, project: project) }
      end

      it "doesn't find any record" do
        options[:to] = Time.now

        expect(subject[:value]).to eq('-')
      end

      it "finds records created between `from` and `to` range" do
        options[:from] = 10.days.ago
        options[:to] = 10.days.from_now

        expect(subject[:value]).to eq('2')
      end
    end
  end

  describe '#deployment_frequency' do
    subject { stage_summary.fourth[:value] }

    it 'includes the unit: `per day`' do
      expect(stage_summary.fourth[:unit]).to eq _('per day')
    end

    before do
      Timecop.freeze(5.days.ago) { create(:deployment, :success, project: project) }
    end

    it 'returns 0.0 when there were deploys but the frequency was too low' do
      options[:from] = 30.days.ago

      # 1 deployment over 30 days
      # frequency of 0.03, rounded off to 0.0
      expect(subject).to eq('0')
    end

    it 'returns `-` when there were no deploys' do
      options[:from] = 4.days.ago

      # 0 deployment in the last 4 days
      expect(subject).to eq('-')
    end

    context 'when `to` is nil' do
      it 'includes range until now' do
        options[:from] = 6.days.ago
        options[:to] = nil

        # 1 deployment over 7 days
        expect(subject).to eq('0.1')
      end
    end

    context 'when `to` is given' do
      before do
        Timecop.freeze(5.days.from_now) { create(:deployment, :success, project: project, finished_at: Time.zone.now) }
      end

      it 'finds records created between `from` and `to` range' do
        options[:from] = 10.days.ago
        options[:to] = 10.days.from_now

        # 2 deployments over 20 days
        expect(subject).to eq('0.1')
      end

      context 'when `from` and `to` are within a day' do
        it 'returns the number of deployments made on that day' do
          freeze_time do
            create(:deployment, :success, project: project, finished_at: Time.zone.now)
            options[:from] = Time.zone.now.at_beginning_of_day
            options[:to] = Time.zone.now.at_end_of_day

            expect(subject).to eq('1')
          end
        end
      end
    end
  end
end