2019-11-27 22:06:32 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe DeploymentsFinder do
|
2021-02-08 07:09:20 -05:00
|
|
|
subject { described_class.new(params).execute }
|
2019-11-27 22:06:32 -05:00
|
|
|
|
2021-05-06 02:10:11 -04:00
|
|
|
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
|
|
|
|
|
2019-11-27 22:06:32 -05:00
|
|
|
describe "#execute" do
|
2021-02-16 13:09:24 -05:00
|
|
|
context 'when project or group is missing' do
|
2021-02-08 07:09:20 -05:00
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it 'returns nothing' do
|
|
|
|
is_expected.to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
context 'at project scope' do
|
|
|
|
let_it_be(:project) { create(:project, :public, :test_repo) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
let(:base_params) { { project: project } }
|
2019-11-27 22:06:32 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
describe 'filtering' do
|
|
|
|
context 'when updated_at filters are specified' do
|
2021-05-06 02:10:11 -04:00
|
|
|
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) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2021-05-06 02:10:11 -04:00
|
|
|
let(:params) { { **base_params, updated_before: 1.day.ago, updated_after: 3.days.ago, order_by: :updated_at } }
|
2020-01-17 16:08:29 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'returns deployments with matched updated_at' do
|
2021-05-06 02:10:11 -04:00
|
|
|
is_expected.to match_array([deployment_2, deployment_1])
|
|
|
|
end
|
2020-01-17 16:08:29 -05:00
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
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
|
2020-01-17 16:08:29 -05:00
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
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' } }
|
2020-01-17 16:08:29 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'returns deployments for the given environment' do
|
|
|
|
is_expected.to match_array([deployment1])
|
|
|
|
end
|
2020-01-17 16:08:29 -05:00
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
context 'when using an invalid deployment status' do
|
|
|
|
let(:params) { { **base_params, status: 'kittens' } }
|
2020-01-17 16:08:29 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'raises ArgumentError' do
|
|
|
|
expect { subject }.to raise_error(ArgumentError)
|
|
|
|
end
|
2020-01-17 16:08:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
describe 'ordering' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let(:params) { { **base_params, order_by: order_by, sort: sort } }
|
|
|
|
|
2021-05-06 02:10:11 -04:00
|
|
|
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) }
|
2021-02-16 13:09:24 -05:00
|
|
|
|
|
|
|
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]
|
2021-05-06 02:10:11 -04:00
|
|
|
'iid' | 'asc' | [:deployment_1, :deployment_2, :deployment_3]
|
|
|
|
'iid' | 'desc' | [:deployment_3, :deployment_2, :deployment_1]
|
2021-02-16 13:09:24 -05:00
|
|
|
'ref' | 'asc' | [:deployment_2, :deployment_1, :deployment_3]
|
|
|
|
'ref' | 'desc' | [:deployment_3, :deployment_1, :deployment_2]
|
2021-05-06 02:10:11 -04:00
|
|
|
'updated_at' | 'asc' | described_class::InefficientQueryError
|
|
|
|
'updated_at' | 'desc' | described_class::InefficientQueryError
|
|
|
|
'finished_at' | 'asc' | described_class::InefficientQueryError
|
|
|
|
'finished_at' | 'desc' | described_class::InefficientQueryError
|
2021-02-16 13:09:24 -05:00
|
|
|
'invalid' | 'asc' | [:deployment_1, :deployment_2, :deployment_3]
|
2021-05-06 02:10:11 -04:00
|
|
|
'iid' | 'err' | [:deployment_1, :deployment_2, :deployment_3]
|
2021-02-16 13:09:24 -05:00
|
|
|
end
|
2020-01-17 16:08:29 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
with_them do
|
|
|
|
it 'returns the deployments ordered' do
|
2021-05-06 02:10:11 -04:00
|
|
|
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
|
2021-02-16 13:09:24 -05:00
|
|
|
end
|
2020-01-17 16:08:29 -05:00
|
|
|
end
|
|
|
|
end
|
2021-02-08 07:09:20 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
describe 'transform `created_at` sorting to `id` sorting' do
|
|
|
|
let(:params) { { **base_params, order_by: 'created_at', sort: 'asc' } }
|
2021-02-08 07:09:20 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'sorts by only one column' do
|
|
|
|
expect(subject.order_values.size).to eq(1)
|
2021-02-08 07:09:20 -05:00
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'sorts by `id`' do
|
|
|
|
expect(subject.order_values.first.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
|
2021-02-08 07:09:20 -05:00
|
|
|
end
|
2021-02-16 13:09:24 -05:00
|
|
|
end
|
2021-02-08 07:09:20 -05:00
|
|
|
|
2021-05-06 02:10:11 -04:00
|
|
|
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' } }
|
2021-02-08 07:09:20 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'sorts by two columns' do
|
|
|
|
expect(subject.order_values.size).to eq(2)
|
2021-02-08 07:09:20 -05:00
|
|
|
end
|
2019-11-27 22:06:32 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it 'adds `id` sorting as the second order column' do
|
|
|
|
order_value = subject.order_values[1]
|
2019-11-27 22:06:32 -05:00
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
expect(order_value.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
|
2019-11-27 22:06:32 -05:00
|
|
|
end
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
it 'uses the `id ASC` as tie-breaker when ordering' do
|
2021-02-16 13:09:24 -05:00
|
|
|
updated_at = Time.now
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
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)
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
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
|
2021-02-16 13:09:24 -05:00
|
|
|
end
|
2019-12-05 22:08:02 -05:00
|
|
|
end
|
|
|
|
|
2021-05-06 02:10:11 -04:00
|
|
|
describe 'enforce sorting to `updated_at` sorting' do
|
2022-02-14 04:13:38 -05:00
|
|
|
let(:params) { { **base_params, updated_before: 1.day.ago, order_by: 'id', sort: 'asc', raise_for_inefficient_updated_at_query: false } }
|
2021-05-06 02:10:11 -04:00
|
|
|
|
|
|
|
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)
|
2021-05-12 05:10:19 -04:00
|
|
|
expect(subject.order_values.second.to_sql).to eq(Deployment.arel_table[:id].asc.to_sql)
|
2021-05-06 02:10:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
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) }
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
context 'when filtering by finished_after and finished_before' do
|
2021-05-06 02:10:11 -04:00
|
|
|
let(:params) { { **base_params, finished_after: 3.days.ago, finished_before: 1.day.ago, status: :success, order_by: :finished_at } }
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it { is_expected.to match_array([deployment_1]) }
|
|
|
|
end
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
context 'when the finished_before parameter is missing' do
|
2021-05-06 02:10:11 -04:00
|
|
|
let(:params) { { **base_params, finished_after: 3.days.ago, status: :success, order_by: :finished_at } }
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it { is_expected.to match_array([deployment_1, deployment_3]) }
|
|
|
|
end
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
context 'when finished_after is missing' do
|
2021-05-06 02:10:11 -04:00
|
|
|
let(:params) { { **base_params, finished_before: 3.days.ago, status: :success, order_by: :finished_at } }
|
2019-12-05 22:08:02 -05:00
|
|
|
|
2021-02-16 13:09:24 -05:00
|
|
|
it { is_expected.to match_array([deployment_2]) }
|
|
|
|
end
|
2019-12-05 22:08:02 -05:00
|
|
|
end
|
|
|
|
end
|
2021-04-27 23:09:58 -04:00
|
|
|
|
|
|
|
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) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2021-04-27 23:09:58 -04:00
|
|
|
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
|
2019-11-27 22:06:32 -05:00
|
|
|
end
|
|
|
|
end
|