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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe DeploymentsFinder do
subject { described_class.new(params).execute }
describe "validation" do
context 'when both updated_at and finished_at filters are specified' do
let(:params) { { updated_before: 1.day.ago, finished_before: 1.day.ago } }
it 'raises an error' do
expect { subject }.to raise_error(
described_class::InefficientQueryError,
'Both `updated_at` filter and `finished_at` filter can not be specified')
end
end
context 'when updated_at filter and id sorting' do
let(:params) { { updated_before: 1.day.ago, order_by: :id } }
it 'raises an error' do
expect { subject }.to raise_error(
described_class::InefficientQueryError,
'`updated_at` filter and `updated_at` sorting must be paired')
end
end
context 'when finished_at filter and id sorting' do
let(:params) { { finished_before: 1.day.ago, order_by: :id } }
it 'raises an error' do
expect { subject }.to raise_error(
described_class::InefficientQueryError,
'`finished_at` filter and `finished_at` sorting must be paired')
end
end
context 'when finished_at filter with failed status filter' do
let(:params) { { finished_before: 1.day.ago, order_by: :finished_at, status: :failed } }
it 'raises an error' do
expect { subject }.to raise_error(
described_class::InefficientQueryError,
'`finished_at` filter must be combined with `success` status filter.')
end
end
context 'when environment filter with non-project scope' do
let(:params) { { environment: 'production' } }
it 'raises an error' do
expect { subject }.to raise_error(
described_class::InefficientQueryError,
'`environment` filter must be combined with `project` scope.')
end
end
end
describe "#execute" do
context 'when project or group is missing' do
let(:params) { {} }
it 'returns nothing' do
is_expected.to eq([])
end
end
context 'at project scope' do
let_it_be(:project) { create(:project, :public, :test_repo) }
let(:base_params) { { project: project } }
describe 'filtering' do
context 'when updated_at filters are specified' do
let_it_be(:deployment_1) { create(:deployment, :success, project: project, updated_at: 48.hours.ago) }
let_it_be(:deployment_2) { create(:deployment, :success, project: project, updated_at: 47.hours.ago) }
let_it_be(:deployment_3) { create(:deployment, :success, project: project, updated_at: 4.days.ago) }
let_it_be(:deployment_4) { create(:deployment, :success, project: project, updated_at: 1.hour.ago) }
let(:params) { { **base_params, updated_before: 1.day.ago, updated_after: 3.days.ago, order_by: :updated_at } }
it 'returns deployments with matched updated_at' do
is_expected.to match_array([deployment_2, deployment_1])
end
end
context 'when the environment name is specified' do
let!(:environment1) { create(:environment, project: project) }
let!(:environment2) { create(:environment, project: project) }
let!(:deployment1) do
create(:deployment, project: project, environment: environment1)
end
let!(:deployment2) do
create(:deployment, project: project, environment: environment2)
end
let(:params) { { **base_params, environment: environment1.name } }
it 'returns deployments for the given environment' do
is_expected.to match_array([deployment1])
end
end
context 'when the deployment status is specified' do
let!(:deployment1) { create(:deployment, :success, project: project) }
let!(:deployment2) { create(:deployment, :failed, project: project) }
let(:params) { { **base_params, status: 'success' } }
it 'returns deployments for the given environment' do
is_expected.to match_array([deployment1])
end
end
context 'when using an invalid deployment status' do
let(:params) { { **base_params, status: 'kittens' } }
it 'raises ArgumentError' do
expect { subject }.to raise_error(ArgumentError)
end
end
end
describe 'ordering' do
using RSpec::Parameterized::TableSyntax
let(:params) { { **base_params, order_by: order_by, sort: sort } }
let!(:deployment_1) { create(:deployment, :success, project: project, ref: 'master', created_at: 2.days.ago, updated_at: Time.now, finished_at: Time.now) }
let!(:deployment_2) { create(:deployment, :success, project: project, ref: 'feature', created_at: 1.day.ago, updated_at: 2.hours.ago, finished_at: 2.hours.ago) }
let!(:deployment_3) { create(:deployment, :success, project: project, ref: 'video', created_at: Time.now, updated_at: 1.hour.ago, finished_at: 1.hour.ago) }
where(:order_by, :sort, :ordered_deployments) do
'created_at' | 'asc' | [:deployment_1, :deployment_2, :deployment_3]
'created_at' | 'desc' | [:deployment_3, :deployment_2, :deployment_1]
'id' | 'asc' | [:deployment_1, :deployment_2, :deployment_3]
'id' | 'desc' | [:deployment_3, :deployment_2, :deployment_1]
'iid' | 'asc' | [:deployment_1, :deployment_2, :deployment_3]
'iid' | 'desc' | [:deployment_3, :deployment_2, :deployment_1]
'ref' | 'asc' | [:deployment_2, :deployment_1, :deployment_3]
'ref' | 'desc' | [:deployment_3, :deployment_1, :deployment_2]
'updated_at' | 'asc' | described_class::InefficientQueryError
'updated_at' | 'desc' | described_class::InefficientQueryError
'finished_at' | 'asc' | described_class::InefficientQueryError
'finished_at' | 'desc' | described_class::InefficientQueryError
'invalid' | 'asc' | [:deployment_1, :deployment_2, :deployment_3]
'iid' | 'err' | [:deployment_1, :deployment_2, :deployment_3]
end
with_them do
it 'returns the deployments ordered' do
if ordered_deployments == described_class::InefficientQueryError
expect { subject }.to raise_error(described_class::InefficientQueryError)
else
expect(subject).to eq(ordered_deployments.map { |name| public_send(name) })
end
end
end
end
describe 'transform `created_at` sorting to `id` sorting' do
let(:params) { { **base_params, order_by: 'created_at', sort: 'asc' } }
it 'sorts by only one column' do
expect(subject.order_values.size).to eq(1)
end
it 'sorts by `id`' do
expect(subject.order_values.first.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
end
end
describe 'transform `iid` sorting to `id` sorting' do
let(:params) { { **base_params, order_by: 'iid', sort: 'asc' } }
it 'sorts by only one column' do
expect(subject.order_values.size).to eq(1)
end
it 'sorts by `id`' do
expect(subject.order_values.first.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
end
end
describe 'tie-breaker for `updated_at` sorting' do
let(:params) { { **base_params, updated_after: 1.day.ago, order_by: 'updated_at', sort: 'asc' } }
it 'sorts by two columns' do
expect(subject.order_values.size).to eq(2)
end
it 'adds `id` sorting as the second order column' do
order_value = subject.order_values[1]
expect(order_value.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
end
it 'uses the `id ASC` as tie-breaker when ordering' do
updated_at = Time.now
deployment_1 = create(:deployment, :success, project: project, updated_at: updated_at)
deployment_2 = create(:deployment, :success, project: project, updated_at: updated_at)
deployment_3 = create(:deployment, :success, project: project, updated_at: updated_at)
expect(subject).to eq([deployment_1, deployment_2, deployment_3])
end
context 'when sort direction is desc' do
let(:params) { { **base_params, updated_after: 1.day.ago, order_by: 'updated_at', sort: 'desc' } }
it 'uses the `id DESC` as tie-breaker when ordering' do
updated_at = Time.now
deployment_1 = create(:deployment, :success, project: project, updated_at: updated_at)
deployment_2 = create(:deployment, :success, project: project, updated_at: updated_at)
deployment_3 = create(:deployment, :success, project: project, updated_at: updated_at)
expect(subject).to eq([deployment_3, deployment_2, deployment_1])
end
end
end
describe 'enforce sorting to `updated_at` sorting' do
let(:params) { { **base_params, updated_before: 1.day.ago, order_by: 'id', sort: 'asc' } }
before do
allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
end
it 'sorts by only one column' do
expect(subject.order_values.size).to eq(2)
end
it 'sorts by `updated_at`' do
expect(subject.order_values.first.to_sql).to eq(Deployment.arel_table[:updated_at].asc.to_sql)
expect(subject.order_values.second.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
end
end
context 'when filtering by finished time' do
let!(:deployment_1) { create(:deployment, :success, project: project, finished_at: 2.days.ago) }
let!(:deployment_2) { create(:deployment, :success, project: project, finished_at: 4.days.ago) }
let!(:deployment_3) { create(:deployment, :success, project: project, finished_at: 5.hours.ago) }
context 'when filtering by finished_after and finished_before' do
let(:params) { { **base_params, finished_after: 3.days.ago, finished_before: 1.day.ago, status: :success, order_by: :finished_at } }
it { is_expected.to match_array([deployment_1]) }
end
context 'when the finished_before parameter is missing' do
let(:params) { { **base_params, finished_after: 3.days.ago, status: :success, order_by: :finished_at } }
it { is_expected.to match_array([deployment_1, deployment_3]) }
end
context 'when finished_after is missing' do
let(:params) { { **base_params, finished_before: 3.days.ago, status: :success, order_by: :finished_at } }
it { is_expected.to match_array([deployment_2]) }
end
end
end
context 'at group scope' do
let_it_be(:group) { create(:group) }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:group_project_1) { create(:project, :public, :test_repo, group: group) }
let_it_be(:group_project_2) { create(:project, :public, :test_repo, group: group) }
let_it_be(:subgroup_project_1) { create(:project, :public, :test_repo, group: subgroup) }
let(:base_params) { { group: group } }
describe 'ordering' do
using RSpec::Parameterized::TableSyntax
let(:params) { { **base_params, order_by: order_by, sort: sort } }
let!(:group_project_1_deployment) { create(:deployment, :success, project: group_project_1, iid: 11, ref: 'master', created_at: 2.days.ago, updated_at: Time.now, finished_at: Time.now) }
let!(:group_project_2_deployment) { create(:deployment, :success, project: group_project_2, iid: 12, ref: 'feature', created_at: 1.day.ago, updated_at: 2.hours.ago, finished_at: 2.hours.ago) }
let!(:subgroup_project_1_deployment) { create(:deployment, :success, project: subgroup_project_1, iid: 8, ref: 'video', created_at: Time.now, updated_at: 1.hour.ago, finished_at: 1.hour.ago) }
where(:order_by, :sort) do
'created_at' | 'asc'
'created_at' | 'desc'
'id' | 'asc'
'id' | 'desc'
'iid' | 'asc'
'iid' | 'desc'
'ref' | 'asc'
'ref' | 'desc'
'invalid' | 'asc'
'iid' | 'err'
end
with_them do
it 'returns the deployments unordered' do
expect(subject.to_a).to contain_exactly(group_project_1_deployment,
group_project_2_deployment,
subgroup_project_1_deployment)
end
end
end
it 'avoids N+1 queries' do
execute_queries = -> { described_class.new({ group: group }).execute.first }
control_count = ActiveRecord::QueryRecorder.new { execute_queries }.count
new_project = create(:project, :repository, group: group)
new_env = create(:environment, project: new_project, name: "production")
create_list(:deployment, 2, status: :success, project: new_project, environment: new_env)
group.reload
expect { execute_queries }.not_to exceed_query_limit(control_count)
end
end
end
end
|