BigW Consortium Gitlab

Commit f0bc9af3 by Shinya Maeda

Fixed the following.

- Fix inappropriate evaluation(casecmp) to regex - Fix missed boolean conversion - Improve spec
parent a114c988
......@@ -88,8 +88,9 @@ class PipelinesFinder
end
def by_yaml_errors(items)
if params[:yaml_errors].present?
if params[:yaml_errors]
flg = Gitlab::Utils.to_boolean(params[:yaml_errors])
if flg.present?
if flg
items.where("yaml_errors IS NOT NULL")
else
items.where("yaml_errors IS NULL")
......@@ -102,7 +103,7 @@ class PipelinesFinder
def order_and_sort(items)
if params[:order_by].present? && params[:sort].present? &&
items.column_names.include?(params[:order_by]) &&
(params[:sort].casecmp('ASC') || params[:sort].casecmp('DESC'))
params[:sort] =~ /\A(ASC|DESC)\Z/i
items.reorder(params[:order_by] => params[:sort])
else
items.reorder(id: :desc)
......
......@@ -13,6 +13,17 @@ describe PipelinesFinder do
let!(:canceled_pipeline) { create(:ci_pipeline, project: project, user: user, created_at: 2.minutes.ago, status: 'canceled') }
let!(:skipped_pipeline) { create(:ci_pipeline, project: project, user: user, created_at: 1.minute.ago, status: 'skipped') }
let!(:yaml_errors_pipeline) { create(:ci_pipeline, project: project, user: user, yaml_errors: 'Syntax error') }
let(:dummy_pipelines) do
[tag_pipeline,
created_pipeline,
pending_pipeline,
running_pipeline,
success_pipeline,
failed_pipeline,
canceled_pipeline,
skipped_pipeline,
yaml_errors_pipeline]
end
subject { described_class.new(project, params).execute }
......@@ -21,29 +32,12 @@ describe PipelinesFinder do
let(:params) { {} }
it 'selects all pipelines' do
expect(subject.count).to be 9
expect(subject).to include tag_pipeline
expect(subject).to include created_pipeline
expect(subject).to include pending_pipeline
expect(subject).to include running_pipeline
expect(subject).to include success_pipeline
expect(subject).to include failed_pipeline
expect(subject).to include canceled_pipeline
expect(subject).to include skipped_pipeline
expect(subject).to include yaml_errors_pipeline
expect(subject.count).to be dummy_pipelines.count
expect(subject).to match_array(dummy_pipelines)
end
it 'orders in descending order on ID' do
expected_ids = [tag_pipeline.id,
created_pipeline.id,
pending_pipeline.id,
running_pipeline.id,
success_pipeline.id,
failed_pipeline.id,
canceled_pipeline.id,
skipped_pipeline.id,
yaml_errors_pipeline.id].sort.reverse
expect(subject.map(&:id)).to eq expected_ids
expect(subject.map(&:id)).to eq dummy_pipelines.map(&:id).sort.reverse
end
end
......@@ -210,6 +204,15 @@ describe PipelinesFinder do
expect(subject).to include skipped_pipeline
end
end
context 'when an argument is invalid' do
let(:params) { { yaml_errors: "UnexpectedValue" } }
it 'selects all pipelines' do
expect(subject.count).to be dummy_pipelines.count
expect(subject).to match_array(dummy_pipelines)
end
end
end
context 'when a order_by and sort are passed' do
......@@ -230,6 +233,22 @@ describe PipelinesFinder do
expect(subject.last).to eq(tag_pipeline)
end
end
context 'when order_by does not exist' do
let(:params) { { order_by: 'abnormal_column', sort: 'desc' } }
it 'sorts by default' do
expect(subject.map(&:id)).to eq dummy_pipelines.map(&:id).sort.reverse
end
end
context 'when sort does not exist' do
let(:params) { { order_by: 'created_at', sort: 'abnormal_sort' } }
it 'sorts by default' do
expect(subject.map(&:id)).to eq dummy_pipelines.map(&:id).sort.reverse
end
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment